var __defProp = Object.defineProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __publicField = (obj, key, value) => {
  __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
  return value;
};
import { jsx } from "react/jsx-runtime";
import { SugaredField, useField } from "@contember/react-binding";
import { memo } from "react";
import { parseUrl } from "../../../utils/parseUrl.js";
class YouTubeEmbedHandler {
  constructor(options) {
    __publicField(this, "debugName", "YouTube");
    __publicField(this, "discriminateBy");
    this.options = options;
    this.discriminateBy = options.discriminateBy;
  }
  staticRender() {
    return /* @__PURE__ */ jsx(SugaredField, { field: this.options.youTubeIdField });
  }
  handleSource(source, url) {
    if (!url) {
      if (source.startsWith("youtu")) {
        source = `www.${source}`;
      }
      if (source.startsWith("www.")) {
        source = `https://${source}`;
      }
      url = parseUrl(source);
      if (!url) {
        return void 0;
      }
    }
    if (url.host.endsWith("youtube.com")) {
      const id = url.searchParams.get("v");
      return id ?? void 0;
    } else if (url.host.endsWith("youtu.be")) {
      return url.pathname.substr(1);
    }
    return void 0;
  }
  renderEmbed() {
    if (this.options.render) {
      return this.options.render();
    }
    return /* @__PURE__ */ jsx(YouTubeEmbedHandler.Renderer, { youTubeIdField: this.options.youTubeIdField });
  }
  populateEmbedData({ entity, embedArtifacts }) {
    entity.getField(this.options.youTubeIdField).updateValue(embedArtifacts);
  }
}
((YouTubeEmbedHandler2) => {
  YouTubeEmbedHandler2.Renderer = memo(function YouTubeRenderer(props) {
    const youTubeId = useField(props.youTubeIdField).value;
    if (youTubeId === null) {
      return null;
    }
    return /* @__PURE__ */ jsx(
      "iframe",
      {
        width: "560",
        height: "315",
        src: `https://www.youtube-nocookie.com/embed/${youTubeId}`,
        referrerPolicy: "no-referrer",
        allow: "accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture",
        loading: "lazy",
        frameBorder: "0",
        allowFullScreen: true
      }
    );
  });
})(YouTubeEmbedHandler || (YouTubeEmbedHandler = {}));
export {
  YouTubeEmbedHandler
};
//# sourceMappingURL=YouTubeEmbedHandler.js.map
