"use strict";
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;
};
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const jsxDevRuntime = require("react/jsx-dev-runtime");
const reactBinding = require("@contember/react-binding");
const react = require("react");
const parseUrl = require("../../../utils/parseUrl.cjs");
class YouTubeEmbedHandler {
  constructor(options) {
    __publicField(this, "debugName", "YouTube");
    __publicField(this, "discriminateBy");
    this.options = options;
    this.discriminateBy = options.discriminateBy;
  }
  staticRender() {
    return /* @__PURE__ */ jsxDevRuntime.jsxDEV(reactBinding.SugaredField, { field: this.options.youTubeIdField }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/richText/blockEditor/embed/handlers/YouTubeEmbedHandler.tsx",
      lineNumber: 16,
      columnNumber: 10
    }, this);
  }
  handleSource(source, url) {
    if (!url) {
      if (source.startsWith("youtu")) {
        source = `www.${source}`;
      }
      if (source.startsWith("www.")) {
        source = `https://${source}`;
      }
      url = parseUrl.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__ */ jsxDevRuntime.jsxDEV(YouTubeEmbedHandler.Renderer, { youTubeIdField: this.options.youTubeIdField }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/richText/blockEditor/embed/handlers/YouTubeEmbedHandler.tsx",
      lineNumber: 49,
      columnNumber: 10
    }, this);
  }
  populateEmbedData({ entity, embedArtifacts }) {
    entity.getField(this.options.youTubeIdField).updateValue(embedArtifacts);
  }
}
((YouTubeEmbedHandler2) => {
  YouTubeEmbedHandler2.Renderer = react.memo(function YouTubeRenderer(props) {
    const youTubeId = reactBinding.useField(props.youTubeIdField).value;
    if (youTubeId === null) {
      return null;
    }
    return /* @__PURE__ */ jsxDevRuntime.jsxDEV(
      "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
      },
      void 0,
      false,
      {
        fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/richText/blockEditor/embed/handlers/YouTubeEmbedHandler.tsx",
        lineNumber: 75,
        columnNumber: 4
      },
      this
    );
  });
})(YouTubeEmbedHandler || (YouTubeEmbedHandler = {}));
exports.YouTubeEmbedHandler = YouTubeEmbedHandler;
//# sourceMappingURL=YouTubeEmbedHandler.cjs.map
