"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 add_caption_button_exports = {};
__export(add_caption_button_exports, {
  AddCaptionButton: () => AddCaptionButton
});
module.exports = __toCommonJS(add_caption_button_exports);
var import_design_system = require("@wix/design-system");
var import_wix_ui_icons_common = require("@wix/wix-ui-icons-common");
var import_classnames = __toESM(require("classnames"));
var import_react = __toESM(require("react"));
var import_data_hooks = require("./../../plugin-figure/data-hooks.js");
var import_ricos_context = require("./../../ricos-context/index.js");
var import_add_caption_button_scss = __toESM(require("./add-caption-button-scss.js"));
const CaptionButtonContent = (0, import_react.forwardRef)(
  ({ onAddCaption, isIconOnly }, ref) => {
    const { t, isMobile } = (0, import_react.useContext)(import_ricos_context.RicosContext);
    const handleClick = (e) => {
      e.preventDefault();
      e.stopPropagation();
      onAddCaption?.();
    };
    return /* @__PURE__ */ import_react.default.createElement(
      "button",
      {
        ref,
        className: (0, import_classnames.default)(import_add_caption_button_scss.default.addCaptionButton, {
          [import_add_caption_button_scss.default.mobile]: isMobile,
          [import_add_caption_button_scss.default.withIcon]: isIconOnly,
          [import_add_caption_button_scss.default.iconOnly]: isIconOnly
        }),
        onClick: handleClick,
        onMouseDown: (e) => {
          e.preventDefault();
          e.stopPropagation();
        },
        onTouchEnd: isMobile ? handleClick : void 0,
        "data-hook": import_data_hooks.ADD_CAPTION_BUTTON,
        type: "button",
        "aria-label": t("Figure_Caption_AddButton_AriaLabel")
      },
      /* @__PURE__ */ import_react.default.createElement(import_wix_ui_icons_common.EditSmall, { className: import_add_caption_button_scss.default.icon }),
      !isIconOnly && t("Figure_Caption_AddButton_Title")
    );
  }
);
const AddCaptionButton = ({
  onAddCaption,
  containerRef
}) => {
  const { t, jsdomMode } = (0, import_react.useContext)(import_ricos_context.RicosContext);
  const [buttonMode, setButtonMode] = (0, import_react.useState)(() => jsdomMode ? "full" : "hidden");
  const [dimensions, setDimensions] = (0, import_react.useState)(
    null
  );
  const buttonRef = (0, import_react.useRef)(null);
  const fullButtonRef = (0, import_react.useRef)(null);
  const iconOnlyButtonRef = (0, import_react.useRef)(null);
  (0, import_react.useEffect)(() => {
    if (!containerRef?.current || dimensions) {
      return;
    }
    const measureButtons = () => {
      if (!fullButtonRef.current || !iconOnlyButtonRef.current) {
        return;
      }
      const fullButtonWidth = fullButtonRef.current.offsetWidth;
      const iconButtonWidth = iconOnlyButtonRef.current.offsetWidth;
      setDimensions({ fullWidth: fullButtonWidth, iconWidth: iconButtonWidth });
    };
    measureButtons();
  }, [containerRef, dimensions]);
  (0, import_react.useEffect)(() => {
    if (!containerRef?.current || !dimensions || jsdomMode) {
      return;
    }
    const container = containerRef.current;
    const checkButtonFit = () => {
      const containerWidth = container.offsetWidth;
      const containerHeight = container.offsetHeight;
      if (dimensions.fullWidth <= containerWidth && containerHeight >= 50) {
        setButtonMode("full");
      } else if (dimensions.iconWidth <= containerWidth && containerHeight >= 40) {
        setButtonMode("icon-only");
      } else {
        setButtonMode("hidden");
      }
    };
    checkButtonFit();
    const resizeObserver = new ResizeObserver(() => {
      checkButtonFit();
    });
    resizeObserver.observe(container);
    return () => {
      resizeObserver.disconnect();
    };
  }, [containerRef, dimensions, jsdomMode]);
  return /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      className: import_add_caption_button_scss.default.addCaptionButtonContainer,
      contentEditable: false,
      suppressContentEditableWarning: true
    },
    !dimensions && /* @__PURE__ */ import_react.default.createElement(
      "div",
      {
        style: { position: "absolute", visibility: "hidden", top: "-9999px" },
        "aria-hidden": "true",
        role: "presentation"
      },
      /* @__PURE__ */ import_react.default.createElement(CaptionButtonContent, { ref: fullButtonRef, isIconOnly: false }),
      /* @__PURE__ */ import_react.default.createElement(CaptionButtonContent, { ref: iconOnlyButtonRef, isIconOnly: true })
    ),
    (() => {
      switch (buttonMode) {
        case "full":
          return /* @__PURE__ */ import_react.default.createElement(
            CaptionButtonContent,
            {
              ref: buttonRef,
              onAddCaption,
              isIconOnly: false
            }
          );
        case "icon-only":
          return /* @__PURE__ */ import_react.default.createElement(import_design_system.Tooltip, { content: t("Figure_Caption_AddButton_Title"), size: "small" }, /* @__PURE__ */ import_react.default.createElement(CaptionButtonContent, { ref: buttonRef, onAddCaption, isIconOnly: true }));
        case "hidden":
          return null;
        default:
          return null;
      }
    })()
  );
};
//# sourceMappingURL=add-caption-button.js.map
