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/binding";
import { memo } from "react";
import { parseIframeSrc } from "../../../utils/parseIframeSrc.js";
import { parseUrl } from "../../../utils/parseUrl.js";
class GoogleFormEmbedHandler {
  constructor(options) {
    __publicField(this, "debugName", "GoogleForm");
    __publicField(this, "discriminateBy");
    this.options = options;
    this.discriminateBy = options.discriminateBy;
  }
  staticRender() {
    return /* @__PURE__ */ jsx(SugaredField, { field: this.options.googleFormIdField });
  }
  handleSource(source, url) {
    if (!url) {
      source = parseIframeSrc(source) ?? source;
      if (source.startsWith("docs.google.com")) {
        source = `https://${source}`;
      }
      url = parseUrl(source);
      if (!url) {
        return void 0;
      }
    }
    if (!url.host.endsWith("docs.google.com")) {
      return void 0;
    }
    const matches = url.pathname.match(/^\/forms\/d(\/e)?\/([^\/]+).*$/);
    if (!matches) {
      return void 0;
    }
    if (matches[1] === void 0) {
      alert(
        this.options.nonEmbedLinkWarning ?? "Detected a Google Form but the link supplied cannot be reliably embedded.\n\nIf you wish to embed the form, please return to Google Forms and use the 'Send' button in the top right corner to get a correct link."
      );
      return void 0;
    }
    return matches[2];
  }
  renderEmbed() {
    if (this.options.render) {
      return this.options.render();
    }
    return /* @__PURE__ */ jsx(GoogleFormEmbedHandler.Renderer, { googleFormIdField: this.options.googleFormIdField });
  }
  populateEmbedData({ entity, embedArtifacts }) {
    entity.getField(this.options.googleFormIdField).updateValue(embedArtifacts);
  }
}
((GoogleFormEmbedHandler2) => {
  GoogleFormEmbedHandler2.Renderer = memo(function GoogleFormRenderer(props) {
    const googleFormId = useField(props.googleFormIdField).value;
    if (googleFormId === null) {
      return null;
    }
    return /* @__PURE__ */ jsx(
      "iframe",
      {
        src: `https://docs.google.com/forms/d/e/${googleFormId}/viewform?embedded=true`,
        width: "640",
        height: "640",
        frameBorder: "0",
        loading: "lazy",
        allowFullScreen: true
      }
    );
  });
})(GoogleFormEmbedHandler || (GoogleFormEmbedHandler = {}));
export {
  GoogleFormEmbedHandler
};
//# sourceMappingURL=GoogleFormEmbedHandler.js.map
