"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 link_preview_exports = {};
__export(link_preview_exports, {
  LinkPreview: () => LinkPreview
});
module.exports = __toCommonJS(link_preview_exports);
var import_design_system = require("@wix/design-system");
var import_wix_ui_icons_common = require("@wix/wix-ui-icons-common");
var import_react = __toESM(require("react"));
var import_ricos_context = require("./../../ricos-context/ricos-context.js");
var import_utils = require("./../../plugin-link-preview/viewer/utils.js");
var import_link_preview_converter = require("../link-preview-converter.js");
var import_link_preview_scss = __toESM(require("../statics/styles/link-preview-scss.js"));
var import_link_preview_viewer = require("../viewer/link-preview-viewer.js");
const LinkPreview = ({ node, editor }) => {
  const ricosNode = import_react.default.useMemo(() => import_link_preview_converter.linkPreviewConverter.fromPm.convert(node.toJSON()), [node]);
  const { loading, error, link } = node.attrs;
  return /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, loading && /* @__PURE__ */ import_react.default.createElement(LinkPreviewLoading, null), error && /* @__PURE__ */ import_react.default.createElement(LinkPreviewError, { editor, link }), !loading && !error && /* @__PURE__ */ import_react.default.createElement(import_link_preview_viewer.LinkPreviewViewer, { node: ricosNode }));
};
const LinkPreviewLoading = () => {
  const linkPreviewStyles = import_link_preview_scss.default;
  const { loadingContainer } = linkPreviewStyles;
  return /* @__PURE__ */ import_react.default.createElement("div", { className: loadingContainer }, /* @__PURE__ */ import_react.default.createElement(import_design_system.Loader, { size: "small" }));
};
const LinkPreviewError = ({ editor, link }) => {
  const { t } = (0, import_react.useContext)(import_ricos_context.RicosContext);
  const showAsALink = () => {
    const url = link?.url;
    if (!url) {
      return;
    }
    const { state } = editor;
    const { selection } = state;
    const from = selection.from;
    editor.chain().focus().setMeta("allowTextReplace", true).insertContent(url + " ").setTextSelection({ from, to: from + url.length + 1 }).run();
  };
  return /* @__PURE__ */ import_react.default.createElement(
    import_design_system.MediaOverlay,
    {
      skin: "dark",
      media: /* @__PURE__ */ import_react.default.createElement("div", { className: import_link_preview_scss.default.linkPreviewContainer }, /* @__PURE__ */ import_react.default.createElement("figure", { className: import_link_preview_scss.default.linkPreview }, /* @__PURE__ */ import_react.default.createElement("div", { className: import_link_preview_scss.default.linkPreviewPlaceholder }), /* @__PURE__ */ import_react.default.createElement("section", { className: import_link_preview_scss.default.linkPreviewInfo }, /* @__PURE__ */ import_react.default.createElement("div", { className: import_link_preview_scss.default.linkPreviewUrl }, (0, import_utils.getUrlForDisplay)((0, import_utils.getHost)(link?.url) || link?.url || "")))))
    },
    /* @__PURE__ */ import_react.default.createElement(import_design_system.MediaOverlay.Content, { visible: "always" }, /* @__PURE__ */ import_react.default.createElement("div", { className: import_link_preview_scss.default.overlay }, /* @__PURE__ */ import_react.default.createElement("div", { className: import_link_preview_scss.default.errorContainer }, /* @__PURE__ */ import_react.default.createElement("div", { className: import_link_preview_scss.default.errorInfo }, /* @__PURE__ */ import_react.default.createElement("div", { className: import_link_preview_scss.default.errorIcon }, /* @__PURE__ */ import_react.default.createElement(import_wix_ui_icons_common.StatusAlertFilled, { color: "white" })), /* @__PURE__ */ import_react.default.createElement("div", null, /* @__PURE__ */ import_react.default.createElement(import_design_system.Text, { light: true, weight: "bold" }, t("LinkPreview_Error_Title")), /* @__PURE__ */ import_react.default.createElement("br", null), /* @__PURE__ */ import_react.default.createElement(import_design_system.Text, { light: true }, t("LinkPreview_Error_Subtitle")))), /* @__PURE__ */ import_react.default.createElement(import_design_system.Button, { prefixIcon: /* @__PURE__ */ import_react.default.createElement(import_wix_ui_icons_common.Link, null), priority: "secondary", onClick: showAsALink }, t("LinkPreview_Error_Button")))))
  );
};
//# sourceMappingURL=link-preview.js.map
