"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var insert_modal_exports = {};
__export(insert_modal_exports, {
  InsertModal: () => InsertModal
});
module.exports = __toCommonJS(insert_modal_exports);
var import_design_system = require("@wix/design-system");
var import_react = __toESM(require("react"));
var import_ricos_content = require("./../../ricos-content/index.js");
var import_ricos_context = require("./../../ricos-context/index.js");
var import_bi_context_provider = require("./../../_shared/bi-context-provider/bi-context-provider.js");
var import_contains_url = require("./../../_shared/link/contains-url.js");
var import_wix_rich_content_common = require("./../../wix-rich-content-common/index.js");
var import_ui_components = require("./../../ui-components/index.js");
var import_consts = require("../consts.js");
var import_data_hooks = require("../data-hooks.js");
var import_defaults = require("../defaults.js");
var import_insert_modal_scss = __toESM(require("../statics/styles/insert-modal-scss.js"));
var import_types = require("../types.js");
const InsertModal = ({
  componentData,
  fetchData,
  nodeId,
  socialType
}) => {
  const { t } = (0, import_react.useContext)(import_ricos_context.RicosContext);
  const { getPublicCommands } = (0, import_react.useContext)(import_ricos_context.EditorContext);
  const modalService = (0, import_react.useContext)(import_ricos_context.ModalServiceContext) || {};
  const pluginsEvents = (0, import_react.useContext)(import_ricos_context.PluginsEventsContext);
  const logBi = (0, import_bi_context_provider.useBiNext)({
    pluginId: "linkPreview",
    modalId: import_consts.socialModals[socialType.toLowerCase()],
    nodeId
  });
  const [url, setUrl] = (0, import_react.useState)("");
  const [submittedInvalidUrl, setSubmittedInvalidUrl] = (0, import_react.useState)(false);
  const inputRef = (0, import_react.useRef)(null);
  const pluginEvents = {
    onPluginsPopOverClick: (data) => pluginsEvents.publishPluginPopoverClick(data)
  };
  const closeModal = () => {
    setSubmittedInvalidUrl(false);
    modalService.closeModal(import_consts.socialModals[socialType.toLowerCase()]);
  };
  const onConfirm = (embedData) => {
    const publicCommands = getPublicCommands();
    if (nodeId) {
      publicCommands?.setBlock(nodeId, import_ricos_content.EMBED_TYPE, embedData);
    } else {
      publicCommands?.insertBlockWithBlankLines(import_ricos_content.EMBED_TYPE, embedData);
    }
  };
  const handleOnConfirm = async () => {
    pluginEvents?.onPluginsPopOverClick?.({
      pluginId: import_types.LINK_PREVIEW_TYPE,
      buttonName: import_wix_rich_content_common.MEDIA_POPOVERS_BUTTONS_NAMES_BI.embed
    });
    logBi({ dataHook: import_data_hooks.SOCIAL_EMBED_MODAL_SUBMIT_BTN });
    if (url) {
      try {
        const { html } = await fetchData?.(url) || {};
        if (!html) {
          setSubmittedInvalidUrl(true);
          inputRef.current?.focus();
          return;
        }
        onConfirm({
          ...componentData,
          ...import_defaults.EMBED_DEFAULTS,
          src: url,
          oembed: {
            type: "rich",
            html
          }
        });
        closeModal();
      } catch {
        setSubmittedInvalidUrl(true);
        inputRef.current?.focus();
      }
    } else {
      setSubmittedInvalidUrl(true);
      inputRef.current?.focus();
    }
  };
  const onCloseRequested = () => {
    pluginEvents?.onPluginsPopOverClick?.({
      pluginId: import_types.LINK_PREVIEW_TYPE,
      buttonName: import_wix_rich_content_common.MEDIA_POPOVERS_BUTTONS_NAMES_BI.cancel
    });
    closeModal();
  };
  return /* @__PURE__ */ import_react.default.createElement(
    import_ui_components.Panel,
    {
      closeButtonProps: { onClick: onCloseRequested },
      dataHook: import_data_hooks.SOCIAL_EMBED_MODAL,
      height: "fit-content"
    },
    /* @__PURE__ */ import_react.default.createElement(import_ui_components.PanelHeader, { title: t(`EmbedURL_Social_${socialType}_Header_Title`) }),
    /* @__PURE__ */ import_react.default.createElement(import_design_system.Box, { direction: "vertical", gap: "SP3", padding: "SP3" }, /* @__PURE__ */ import_react.default.createElement(
      import_design_system.FormField,
      {
        label: t(`EmbedURL_Social_${socialType}_Title`),
        maxLines: 2,
        status: submittedInvalidUrl ? "error" : void 0,
        statusMessage: submittedInvalidUrl ? t("SoundCloudUploadModal_Input_InvalidUrl") : void 0
      },
      /* @__PURE__ */ import_react.default.createElement(
        import_design_system.Input,
        {
          inputRef: (ref) => {
            inputRef.current = ref;
          },
          className: import_insert_modal_scss.default.input,
          type: "url",
          id: "dropdown-text-input",
          autocomplete: "off",
          textOverflow: "ellipsis",
          value: url,
          dataHook: import_data_hooks.SOCIAL_EMBED_MODAL_INPUT,
          onChange: (e) => {
            setSubmittedInvalidUrl(false);
            logBi({ dataHook: import_data_hooks.SOCIAL_EMBED_MODAL_INPUT, value: e.target.value });
            setUrl(e.target.value);
          },
          placeholder: t(`EmbedURL_Social_${socialType}_Placeholder`),
          onBlur: () => {
            if (url && !(0, import_contains_url.containsUrl)(url)) {
              setSubmittedInvalidUrl(true);
            }
          },
          onKeyDown: (e) => {
            if (e.key === "Enter") {
              handleOnConfirm();
            }
          }
        }
      )
    ), /* @__PURE__ */ import_react.default.createElement(
      import_design_system.Button,
      {
        dataHook: import_data_hooks.SOCIAL_EMBED_MODAL_SUBMIT_BTN,
        onClick: () => {
          handleOnConfirm();
        },
        size: "small"
      },
      t("Embed_Add_Button_Label")
    ))
  );
};
//# sourceMappingURL=insert-modal.js.map
