"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 headings_dropdown_exports = {};
__export(headings_dropdown_exports, {
  DEFAULT_HEADERS_DROPDOWN_OPTIONS: () => DEFAULT_HEADERS_DROPDOWN_OPTIONS,
  HEADER_TYPE_MAP: () => HEADER_TYPE_MAP,
  default: () => headings_dropdown_default
});
module.exports = __toCommonJS(headings_dropdown_exports);
var import_design_system = require("@wix/design-system");
var import_wix_ui_icons_common = require("@wix/wix-ui-icons-common");
var import_lodash = __toESM(require("lodash"));
var import_react = __toESM(require("react"));
var import_ricos_context = require("./../../ricos-context/index.js");
var import_ricos_types = require("./../../ricos-types/index.js");
var import_dropdown_modal = require("./../../_shared/dropdown-modal/dropdown-modal.js");
var import_headings_dropdown_utils = require("./../../_shared/headings-dropdown-utils.js");
var import_list_item_select = require("./../../_shared/list-item-select/list-item-select.js");
var import_colors_icon = require("./colors-icon.js");
var import_headings_dropdown_scss = __toESM(require("./headings-dropdown-scss.js"));
const HEADINGS_MODAL_ID = `${import_ricos_types.Node_Type.HEADING}.modal`;
const DEFAULT_HEADERS_DROPDOWN_OPTIONS = ["H1", "H2", "H3", "H4", "H5", "H6", "P"];
const HEADER_TYPE_MAP = {
  H1: "header-one",
  H2: "header-two",
  H3: "header-three",
  H4: "header-four",
  H5: "header-five",
  H6: "header-six",
  P: "unstyled"
};
const HeadingsDropdown = ({ minWidth = 216 }) => {
  const { isMobile, 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 plugins = (0, import_react.useContext)(import_ricos_context.PluginsContext);
  const headingsData = plugins?.getConfig(
    "wix-rich-content-plugin-headings"
  );
  const allowHeadingCustomization = !!headingsData?.allowHeadingCustomization;
  const [openOption, setOpenOption] = import_react.default.useState(null);
  const publicCommands = getPublicCommands();
  const selectedHeading = publicCommands.getCurrentHeading();
  const closeModal = () => modalService.closeModal(HEADINGS_MODAL_ID);
  const handleOnSelect = (heading) => {
    const headingType = HEADER_TYPE_MAP[heading];
    const shouldSetBlockType = selectedHeading !== headingType;
    shouldSetBlockType && publicCommands.setBlockType(headingType);
    closeModal();
  };
  return /* @__PURE__ */ import_react.default.createElement(
    import_dropdown_modal.DropdownModal,
    {
      onCloseButtonClick: closeModal,
      title: t("HeadingsDropdown_Title"),
      minWidth,
      className: import_headings_dropdown_scss.default.dropdown,
      options: (headingsData?.customHeadings || DEFAULT_HEADERS_DROPDOWN_OPTIONS).map((heading) => {
        const headerType = HEADER_TYPE_MAP[heading];
        if (!allowHeadingCustomization) {
          return /* @__PURE__ */ import_react.default.createElement(
            import_list_item_select.ListItemSelect,
            {
              key: heading,
              title: (0, import_headings_dropdown_utils.translateHeading)(heading, t),
              onSelect: () => handleOnSelect(heading),
              selected: selectedHeading === headerType
            }
          );
        }
        const documentStyleType = import_ricos_types.DOC_STYLE_TYPES[heading];
        const optionWiredStyles = publicCommands.getWiredFontStyles()?.[documentStyleType];
        const subText = optionWiredStyles?.["font-size"] ?? "16px";
        const fontFamily = optionWiredStyles?.["font-family"] ?? "inherit";
        return /* @__PURE__ */ import_react.default.createElement("div", { className: import_headings_dropdown_scss.default.option, key: heading }, /* @__PURE__ */ import_react.default.createElement(
          import_list_item_select.ListItemSelect,
          {
            dataHook: "modal-option",
            selected: selectedHeading === headerType,
            prefix: /* @__PURE__ */ import_react.default.createElement(
              import_colors_icon.ColorsIcon,
              {
                color: optionWiredStyles?.color,
                backgroundColor: optionWiredStyles["background-color"]
              }
            ),
            subtitle: subText,
            title: /* @__PURE__ */ import_react.default.createElement("div", { style: { fontFamily } }, (0, import_headings_dropdown_utils.translateHeading)(heading, t)),
            suffix: isMobile ? null : /* @__PURE__ */ import_react.default.createElement(
              import_wix_ui_icons_common.ChevronRight,
              {
                "data-hook": "chevron-right",
                style: {
                  height: "54px",
                  marginBottom: "-16px",
                  marginTop: "-16px"
                }
              }
            ),
            onClick: () => handleOnSelect(heading),
            onKeyDown: (e) => {
              if (e.key === "Enter" || e.key === " ") {
                e.stopPropagation();
                handleOnSelect(heading);
                return;
              }
              if (e.key === "ArrowRight") {
                e.stopPropagation();
                e.preventDefault();
                setOpenOption(heading);
                return;
              }
            },
            onMouseOver: (e) => {
              const target = e.target;
              const isChevronRight = target.closest('[data-hook~="chevron-right"]');
              if (isChevronRight) {
                setOpenOption(heading);
                return;
              }
              if (openOption !== heading) {
                setOpenOption(null);
              }
            }
          }
        ), openOption === heading && /* @__PURE__ */ import_react.default.createElement(
          UpdateHeadingPanel,
          {
            optionName: (0, import_headings_dropdown_utils.translateHeading)(heading, t),
            onApply: () => handleOnSelect(heading),
            onMatchStyling: () => {
              publicCommands.updateDocumentStyle({});
              closeModal();
            },
            onReset: () => {
              publicCommands.resetDocumentStyleByNodeType(documentStyleType);
              closeModal();
            },
            isUpdatedEnabled: () => {
              const currentInlineStyles = publicCommands.getInlineStylesInSelection();
              const documentStyle = publicCommands.getDocumentStyle();
              return headerType === selectedHeading && (0, import_headings_dropdown_utils.hasStyleChanges)(selectedHeading, currentInlineStyles, documentStyle);
            },
            isResetEnabled: () => {
              const documentStyle = publicCommands.getDocumentStyle();
              return !import_lodash.default.isEmpty(documentStyle?.[documentStyleType]);
            },
            onKeyDown: (e) => {
              if (e.key === "ArrowLeft") {
                e.stopPropagation();
                e.preventDefault();
                setOpenOption(null);
                return;
              }
            }
          }
        ));
      })
    }
  );
};
const UpdateHeadingPanel = ({
  optionName,
  onApply,
  onMatchStyling,
  onReset,
  isUpdatedEnabled,
  isResetEnabled,
  onKeyDown
}) => {
  const { t } = (0, import_react.useContext)(import_ricos_context.RicosContext);
  const zIndexService = (0, import_react.useContext)(import_ricos_context.ZIndexContext);
  return /* @__PURE__ */ import_react.default.createElement(
    import_dropdown_modal.DropdownModal,
    {
      onCloseButtonClick: () => {
      },
      className: import_headings_dropdown_scss.default.panel,
      onKeyDown,
      title: "title",
      options: [
        /* @__PURE__ */ import_react.default.createElement(
          import_list_item_select.ListItemSelect,
          {
            key: "apply",
            title: t("FormattingToolbar_TextStyle_Heading_Apply", { optionName }),
            onSelect: onApply
          }
        ),
        /* @__PURE__ */ import_react.default.createElement("div", { key: "divider", className: import_headings_dropdown_scss.default.divider }, /* @__PURE__ */ import_react.default.createElement(import_design_system.Divider, null)),
        /* @__PURE__ */ import_react.default.createElement(
          import_list_item_select.ListItemSelect,
          {
            key: "update",
            title: t("FormattingToolbar_TextStyle_Heading_Update", { optionName }),
            suffix: /* @__PURE__ */ import_react.default.createElement("div", { className: import_headings_dropdown_scss.default.infoIcon }, /* @__PURE__ */ import_react.default.createElement(
              import_design_system.InfoIcon,
              {
                size: "small",
                content: t("FormattingToolbar_TextStyle_Update_Heading_Tooltip", { optionName }),
                tooltipProps: {
                  appendTo: (el) => Boolean(el.getAttribute("data-hook")?.includes("ricos-portal")),
                  zIndex: zIndexService.getZIndex("TOOLTIP"),
                  size: "small",
                  enterDelay: 300
                }
              }
            )),
            onSelect: onMatchStyling,
            disabled: !isUpdatedEnabled()
          }
        ),
        /* @__PURE__ */ import_react.default.createElement(
          import_list_item_select.ListItemSelect,
          {
            key: "reset",
            title: t("FormattingToolbar_TextStyle_Heading_Reset"),
            onSelect: onReset,
            disabled: !isResetEnabled()
          }
        )
      ]
    }
  );
};
var headings_dropdown_default = HeadingsDropdown;
//# sourceMappingURL=headings-dropdown.js.map
