import { Add, AddSmall } from "@wix/wix-ui-icons-common";
import React, { useContext, useState } from "react";
import { RicosContext } from "./../../../ricos-context/index.js";
import {
  getIsModalOpen,
  useModalService,
  useModalState
} from "./../../../ricos-modals/modal-service-provider.js";
import { DropdownButton } from "./../../../_shared/dropdown-button.js";
import { DropdownModal } from "./../../../_shared/dropdown-modal/dropdown-modal.js";
import { ListItemSelect } from "./../../../_shared/list-item-select/list-item-select.js";
import {
  LAYOUT_CELL_TOOLBAR_ADD_CELL_DROPDOWN_AFTER_BUTTON,
  LAYOUT_CELL_TOOLBAR_ADD_CELL_DROPDOWN_BEFORE_BUTTON
} from "../data-hooks.js";
const MODAL_ID = "AddCellDropdown.modal";
const AddCellDropdownButton = ({ dataHook, toolbarItem }) => {
  const { t, isMobile } = useContext(RicosContext) || {};
  const modalService = useModalService();
  const isModalOpen = useModalState(getIsModalOpen(MODAL_ID));
  const [referenceElement, setReferenceElement] = useState(null);
  const onClick = (position) => {
    toolbarItem.commands?.click({ position });
    closeModal();
  };
  const closeModal = () => modalService.closeModal(MODAL_ID);
  const openAddCellDropdownPanel = () => {
    const isModalOpen2 = modalService.isModalOpen(MODAL_ID);
    if (isModalOpen2) {
      closeModal();
      return;
    }
    modalService.openModal({
      id: MODAL_ID,
      Component: AddCellPanel,
      componentProps: {
        onClick,
        closeModal,
        canAddLeftCell: toolbarItem.attributes.canAddLeftCell,
        canAddRightCell: toolbarItem.attributes.canAddRightCell
      },
      layout: isMobile ? "drawer" : "floating",
      positioning: { referenceElement, placement: "bottom" }
    });
  };
  return /* @__PURE__ */ React.createElement(
    DropdownButton,
    {
      disabled: toolbarItem.attributes.disabled,
      dataHook,
      active: isModalOpen,
      onClick: openAddCellDropdownPanel,
      ref: setReferenceElement,
      icon: Add,
      tooltip: t("Toolbar_AddCellDropdown_Tooltip")
    }
  );
};
const AddCellPanel = ({
  onClick,
  canAddLeftCell,
  canAddRightCell,
  closeModal
}) => {
  const { t } = useContext(RicosContext) || {};
  const onKeyDown = (e, position) => {
    if (e.key === "Enter") {
      onClick(position);
      e.stopPropagation();
    }
  };
  const dropdownOptions = [
    /* @__PURE__ */ React.createElement(
      ListItemSelect,
      {
        key: LAYOUT_CELL_TOOLBAR_ADD_CELL_DROPDOWN_BEFORE_BUTTON,
        dataHook: LAYOUT_CELL_TOOLBAR_ADD_CELL_DROPDOWN_BEFORE_BUTTON,
        prefix: /* @__PURE__ */ React.createElement(AddSmall, null),
        title: t("Toolbar_AddCellDropdown_AddBefore_Tooltip"),
        onClick: () => onClick("left"),
        onKeyDown: (e) => {
          onKeyDown(e, "left");
        },
        disabled: !canAddLeftCell
      }
    ),
    /* @__PURE__ */ React.createElement(
      ListItemSelect,
      {
        key: LAYOUT_CELL_TOOLBAR_ADD_CELL_DROPDOWN_AFTER_BUTTON,
        dataHook: LAYOUT_CELL_TOOLBAR_ADD_CELL_DROPDOWN_AFTER_BUTTON,
        prefix: /* @__PURE__ */ React.createElement(AddSmall, null),
        title: t("Toolbar_AddCellDropdown_AddAfter_Tooltip"),
        onClick: () => onClick("right"),
        onKeyDown: (e) => {
          onKeyDown(e, "right");
        },
        disabled: !canAddRightCell
      }
    )
  ];
  return /* @__PURE__ */ React.createElement(
    DropdownModal,
    {
      options: dropdownOptions,
      title: t("Toolbar_AddCellDropdown_Tooltip"),
      onCloseButtonClick: closeModal
    }
  );
};
export {
  AddCellDropdownButton,
  AddCellPanel
};
//# sourceMappingURL=add-cell-dropdown-button.js.map
