---
import {
  isCircuitSimulatorAppletURL,
  isCodePenURL,
  isInstagramURL,
  isPinterestURL,
  isTikTokURL,
  isTweetURL,
} from '../../lib/blog-helpers.ts'
import * as interfaces from '../../lib/interfaces.ts'
import Bookmark from './Bookmark.astro'
import CircuitSimulatorAppletEmbed from './CircuitSimulatorAppletEmbed.astro'
import CodePenEmbed from './CodePenEmbed.astro'
import InstagramEmbed from './InstagramEmbed.astro'
import PinterestEmbed from './PinterestEmbed.astro'
import TikTokEmbed from './TikTokEmbed.astro'
import TweetEmbed from './TweetEmbed.astro'

export interface Props {
  block: interfaces.Block
  urlMap: { [key: string]: string }
}

const { block, urlMap }: Props = Astro.props

if (!block.Embed) {
  return null
}

let url: URL
try {
  url = new URL(block.Embed.Url)
} catch (err) {
  console.log(err)
  url = null
}
---

{
  url ? (
    isTweetURL(url) ? (
      <TweetEmbed url={url} />
    ) : isTikTokURL(url) ? (
      <TikTokEmbed url={url} />
    ) : isInstagramURL(url) ? (
      <InstagramEmbed url={url} />
    ) : isPinterestURL(url) ? (
      <PinterestEmbed url={url} />
    ) : isCodePenURL(url) ? (
      <CodePenEmbed url={url} />
    ) : isCircuitSimulatorAppletURL(url) ? (
      <CircuitSimulatorAppletEmbed url={url} />
    ) : (
      <Bookmark block={block} urlMap={urlMap} />
    )
  ) : null
}
