import * as E from "fp-ts-esm/Either";
import { flow, identity } from "fp-ts-esm/function";
import * as S from "fp-ts-esm/string";
import { Node_Type } from "./../../../../../ricos-schema/legacy/wix/rich_content/v1/index.js";
import { and } from "../../../../fp-utils.js";
import { createNode } from "../../../node-utils.js";
import { getAttributes, hasAttribute, hasTag } from "../core/parse5-utils.js";
const toURL = (str) => E.tryCatch(
  () => new URL(str),
  () => "invalid URL string"
);
const getVideoId = (url) => url.host.includes("youtube") && url.pathname.includes("/embed/") ? E.right(url.pathname.replace("/embed/", "")) : E.left("not a youtube source");
const toYoutubeUrl = flow(
  toURL,
  E.chain(getVideoId),
  E.fold(identity, (id) => `https://www.youtube.com/watch?v=${id}`)
);
const toVideoData = (urlResolver) => flow(getAttributes, ({ src }) => ({ video: { src: { url: urlResolver(src) } } }));
const iframeToYoutubeVideo = [
  and([hasTag("iframe"), hasAttribute("src", S.includes("youtube"))]),
  () => (node) => [
    createNode(Node_Type.VIDEO, {
      nodes: [],
      data: { ...toVideoData(toYoutubeUrl)(node) }
    })
  ]
];
const toVimeoSource = (url) => url.host.includes("vimeo") ? url.toString() : "unsupported video source";
const toVimeoUrl = flow(toURL, E.fold(identity, toVimeoSource));
const iframeToVimeoVideo = [
  and([hasTag("iframe"), hasAttribute("src", S.includes("vimeo"))]),
  () => (node) => [
    createNode(Node_Type.VIDEO, {
      nodes: [],
      data: { ...toVideoData(toVimeoUrl)(node) }
    })
  ]
];
export {
  iframeToVimeoVideo,
  iframeToYoutubeVideo
};
//# sourceMappingURL=iframe-to-video.js.map
