"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 figure_exports = {};
__export(figure_exports, {
  Figure: () => Figure
});
module.exports = __toCommonJS(figure_exports);
var import_classnames = __toESM(require("classnames"));
var import_lodash = __toESM(require("lodash"));
var import_react = __toESM(require("react"));
var import_ricos_context = require("./../../ricos-context/index.js");
var import_activate_caption = require("./../../_shared/figure/activate-caption.js");
var import_add_caption_button = require("./../../_shared/figure/add-caption-button.js");
var import_is_only_node_selected = require("./../../_shared/is-only-node-selected.js");
var import_general_scss = __toESM(require("../../editor-common/statics/styles/general-scss.js"));
var import_general_scss2 = __toESM(require("../../wix-rich-content-common/statics/styles/general-scss.js"));
var import_figure_scss = __toESM(require("./figure-scss.js"));
const stylesWithRTL = { ...import_general_scss.default, ...import_general_scss2.default };
const Figure = ({ NodeViewContent, ...props }) => {
  const { isMobile } = (0, import_react.useContext)(import_ricos_context.RicosContext);
  const { editor, node } = props;
  const containerRef = (0, import_react.useRef)(null);
  const pmNode = node.toJSON();
  const mainChild = pmNode.content[0];
  if (!mainChild) {
    return null;
  }
  const width = getContainerWidth(mainChild.attrs, isMobile);
  const isMediaSelected = (0, import_is_only_node_selected.isOnlyNodeSelected)(editor.state, mainChild.attrs.id);
  const captionNode = pmNode.content[1];
  const captionHasContent = !!captionNode?.content?.length;
  const captionIsVisible = !!captionNode?.attrs?.showEmpty || captionHasContent;
  const shouldShowAddCaptionButton = isMediaSelected && !captionHasContent && !captionIsVisible;
  const handleAddCaption = () => {
    const captionNode2 = pmNode.content[1];
    if (captionNode2 && captionNode2.attrs?.id) {
      (0, import_activate_caption.activateCaption)(editor.view, captionNode2.attrs.id);
    }
  };
  return /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      ref: containerRef,
      style: {
        "--ricos-internal-figure-width": width ? `${width}px` : "auto",
        position: shouldShowAddCaptionButton ? "relative" : void 0
      },
      "data-hook": `figure-${mainChild.type}`,
      className: getContainerClasses(mainChild.attrs.containerData)
    },
    /* @__PURE__ */ import_react.default.createElement(
      NodeViewContent,
      {
        as: "figure",
        className: (0, import_classnames.default)(import_figure_scss.default.figure, {
          [import_figure_scss.default.figureWithCaption]: shouldShowAddCaptionButton
        })
      }
    ),
    shouldShowAddCaptionButton && /* @__PURE__ */ import_react.default.createElement(import_add_caption_button.AddCaptionButton, { onAddCaption: handleAddCaption, containerRef })
  );
};
const getContainerWidth = (nodeData, isMobile) => {
  const disableCustomWidthMapper = nodeData?.image?.width && isMobile && (Number(nodeData?.containerData?.width?.custom) || 0) > 150;
  const disableCustomWidth = disableCustomWidthMapper ?? false;
  const customWidth = !disableCustomWidth ? nodeData?.containerData?.width?.custom : void 0;
  const originalWidth = nodeData?.containerData?.width?.size === "ORIGINAL" || disableCustomWidth ? nodeData?.image?.width : void 0;
  const width = customWidth || originalWidth;
  return width;
};
const getContainerClasses = (containerData) => {
  const alignment = containerData?.alignment;
  const alignmentClassName = alignment ? stylesWithRTL[`align${import_lodash.default.upperFirst(alignment.toLowerCase())}`] : void 0;
  const size = containerData?.width?.size;
  const sizeClassName = size && size !== "FULL_WIDTH" ? stylesWithRTL[`size${import_lodash.default.upperFirst(size.toLowerCase())}`] : void 0;
  const textWrap = containerData?.textWrap;
  const textWrapClassName = textWrap ? void 0 : stylesWithRTL.textWrapNowrap;
  return (0, import_classnames.default)(alignmentClassName, sizeClassName, textWrapClassName, import_figure_scss.default.figureContainer);
};
//# sourceMappingURL=figure.js.map
