"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 ricos_desktop_toolbar_exports = {};
__export(ricos_desktop_toolbar_exports, {
  RicosDesktopToolbar: () => RicosDesktopToolbar
});
module.exports = __toCommonJS(ricos_desktop_toolbar_exports);
var import_react = __toESM(require("react"));
var import_ricos_context = require("./../../../../ricos-context/index.js");
var import_buttons = require("../../buttons/index.js");
var import_UseSizeCalculator = require("../../UseSizeCalculator/index.js");
var import_data_hooks = require("../data-hooks.js");
var import_data_hooks2 = require("./data-hooks.js");
var import_handle_arrow_navigation = require("./handle-arrow-navigation.js");
var import_overflown_buttons = require("./overflown-buttons.js");
var import_ricos_desktop_toolbar_wrapper = require("./ricos-desktop-toolbar-wrapper.js");
var import_RicosDesktopToolbar_scss = __toESM(require("./RicosDesktopToolbar-scss.js"));
var import_use_ricos_desktop_toolbar_actions = require("./use-ricos-desktop-toolbar-actions.js");
const RicosDesktopToolbar = ({
  type,
  maxWidth,
  toolbarButtons,
  onlyShownButtons,
  isRTL,
  overflownItemsPosition,
  alignment,
  ariaLabel
}) => {
  const [moreButton, setMoreButton] = (0, import_react.useState)(null);
  const [toolbarElement, setToolbarElement] = (0, import_react.useState)(null);
  const showMoreAbove = overflownItemsPosition === "top";
  const { portal, lastInteraction } = (0, import_react.useContext)(import_ricos_context.RicosContext);
  const { shouldShowMore, closeToolbar, onClickOutside, toggleMoreItems } = (0, import_use_ricos_desktop_toolbar_actions.useRicosDesktopToolbarActions)();
  const { data, OverflowCalculator } = (0, import_UseSizeCalculator.useSizeCalculator)({
    width: maxWidth,
    toolbarButtons,
    onlyShownButtons
  });
  (0, import_react.useEffect)(() => {
    if (shouldShowMore && data && portal && lastInteraction === "keyboard") {
      const firstOverflowButtonDataHook = data.overflownButtons.getFirstButtonDataHook();
      if (firstOverflowButtonDataHook) {
        setTimeout(() => {
          const firstOverflowButton = portal.querySelector(
            `[data-hook~="${import_data_hooks2.OverflownButtonsToolbar}"] [data-hook~="${firstOverflowButtonDataHook}"]`
          );
          if (firstOverflowButton) {
            firstOverflowButton.focus();
          }
        }, 0);
      }
    }
  }, [shouldShowMore, data, portal]);
  const wrapperProps = {
    type,
    isRTL,
    onKeyDown: (e) => {
      if (type === "floating" && (e.key === "Escape" || e.key === "Tab")) {
        e.preventDefault();
        closeToolbar(moreButton);
      }
    },
    onClickOutside
  };
  return /* @__PURE__ */ import_react.default.createElement(import_ricos_desktop_toolbar_wrapper.RicosDesktopToolbarWrapper, { ...wrapperProps }, /* @__PURE__ */ import_react.default.createElement(OverflowCalculator, null), !!data && /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      ref: setToolbarElement,
      className: import_RicosDesktopToolbar_scss.default.toolbarVisible,
      style: alignment && alignment !== "left" ? { width: "100%" } : void 0,
      "data-hook": import_data_hooks.TOOLBAR_VISIBLE,
      role: "toolbar",
      "aria-label": ariaLabel,
      onKeyDown: (e) => {
        if (toolbarElement) {
          (0, import_handle_arrow_navigation.handleArrowKeyNavigation)({
            e,
            scope: toolbarElement
          });
        }
      }
    },
    !!moreButton && /* @__PURE__ */ import_react.default.createElement(
      import_overflown_buttons.OverflownButtons,
      {
        shouldShowMore,
        overflownButtons: data.overflownButtons,
        showMoreAbove,
        attachTo: moreButton,
        onTabPress: () => closeToolbar(moreButton)
      }
    ),
    !data.visibleButtons.isEmpty() && /* @__PURE__ */ import_react.default.createElement(
      "div",
      {
        className: import_RicosDesktopToolbar_scss.default.visibleItems,
        style: {
          justifyContent: alignment,
          ...alignment && alignment !== "left" ? { width: "100%" } : {}
        }
      },
      data.visibleButtons.getButtonsElementsWithDataHook(),
      !data.overflownButtons.isEmpty() && /* @__PURE__ */ import_react.default.createElement(
        import_buttons.MoreButton,
        {
          ref: setMoreButton,
          key: "more-button",
          onClick: toggleMoreItems,
          shouldShowMore
        }
      )
    )
  ));
};
//# sourceMappingURL=ricos-desktop-toolbar.js.map
