import { Divider, InfoIcon } from "@wix/design-system";
import { ChevronRight } from "@wix/wix-ui-icons-common";
import lodash from "lodash";
import React, { useContext } from "react";
import {
  EditorContext,
  ModalServiceContext,
  PluginsContext,
  RicosContext,
  ZIndexContext
} from "./../../ricos-context/index.js";
import { DOC_STYLE_TYPES, Node_Type } from "./../../ricos-types/index.js";
import { DropdownModal } from "./../../_shared/dropdown-modal/dropdown-modal.js";
import { hasStyleChanges, translateHeading } from "./../../_shared/headings-dropdown-utils.js";
import { ListItemSelect } from "./../../_shared/list-item-select/list-item-select.js";
import { ColorsIcon } from "./colors-icon.js";
import classes from "./headings-dropdown-scss.js";
const HEADINGS_MODAL_ID = `${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 } = useContext(RicosContext);
  const { getPublicCommands } = useContext(EditorContext);
  const modalService = useContext(ModalServiceContext) || {};
  const plugins = useContext(PluginsContext);
  const headingsData = plugins?.getConfig(
    "wix-rich-content-plugin-headings"
  );
  const allowHeadingCustomization = !!headingsData?.allowHeadingCustomization;
  const [openOption, setOpenOption] = React.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__ */ React.createElement(
    DropdownModal,
    {
      onCloseButtonClick: closeModal,
      title: t("HeadingsDropdown_Title"),
      minWidth,
      className: classes.dropdown,
      options: (headingsData?.customHeadings || DEFAULT_HEADERS_DROPDOWN_OPTIONS).map((heading) => {
        const headerType = HEADER_TYPE_MAP[heading];
        if (!allowHeadingCustomization) {
          return /* @__PURE__ */ React.createElement(
            ListItemSelect,
            {
              key: heading,
              title: translateHeading(heading, t),
              onSelect: () => handleOnSelect(heading),
              selected: selectedHeading === headerType
            }
          );
        }
        const documentStyleType = DOC_STYLE_TYPES[heading];
        const optionWiredStyles = publicCommands.getWiredFontStyles()?.[documentStyleType];
        const subText = optionWiredStyles?.["font-size"] ?? "16px";
        const fontFamily = optionWiredStyles?.["font-family"] ?? "inherit";
        return /* @__PURE__ */ React.createElement("div", { className: classes.option, key: heading }, /* @__PURE__ */ React.createElement(
          ListItemSelect,
          {
            dataHook: "modal-option",
            selected: selectedHeading === headerType,
            prefix: /* @__PURE__ */ React.createElement(
              ColorsIcon,
              {
                color: optionWiredStyles?.color,
                backgroundColor: optionWiredStyles["background-color"]
              }
            ),
            subtitle: subText,
            title: /* @__PURE__ */ React.createElement("div", { style: { fontFamily } }, translateHeading(heading, t)),
            suffix: isMobile ? null : /* @__PURE__ */ React.createElement(
              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__ */ React.createElement(
          UpdateHeadingPanel,
          {
            optionName: 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 && hasStyleChanges(selectedHeading, currentInlineStyles, documentStyle);
            },
            isResetEnabled: () => {
              const documentStyle = publicCommands.getDocumentStyle();
              return !lodash.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 } = useContext(RicosContext);
  const zIndexService = useContext(ZIndexContext);
  return /* @__PURE__ */ React.createElement(
    DropdownModal,
    {
      onCloseButtonClick: () => {
      },
      className: classes.panel,
      onKeyDown,
      title: "title",
      options: [
        /* @__PURE__ */ React.createElement(
          ListItemSelect,
          {
            key: "apply",
            title: t("FormattingToolbar_TextStyle_Heading_Apply", { optionName }),
            onSelect: onApply
          }
        ),
        /* @__PURE__ */ React.createElement("div", { key: "divider", className: classes.divider }, /* @__PURE__ */ React.createElement(Divider, null)),
        /* @__PURE__ */ React.createElement(
          ListItemSelect,
          {
            key: "update",
            title: t("FormattingToolbar_TextStyle_Heading_Update", { optionName }),
            suffix: /* @__PURE__ */ React.createElement("div", { className: classes.infoIcon }, /* @__PURE__ */ React.createElement(
              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__ */ React.createElement(
          ListItemSelect,
          {
            key: "reset",
            title: t("FormattingToolbar_TextStyle_Heading_Reset"),
            onSelect: onReset,
            disabled: !isResetEnabled()
          }
        )
      ]
    }
  );
};
var headings_dropdown_default = HeadingsDropdown;
export {
  DEFAULT_HEADERS_DROPDOWN_OPTIONS,
  HEADER_TYPE_MAP,
  headings_dropdown_default as default
};
//# sourceMappingURL=headings-dropdown.js.map
