"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 use_size_calculator_exports = {};
__export(use_size_calculator_exports, {
  useSizeCalculator: () => useSizeCalculator
});
module.exports = __toCommonJS(use_size_calculator_exports);
var import_react = __toESM(require("react"));
var import_ricos_context = require("./../../../ricos-context/index.js");
var import_models = require("../../models/index.js");
const MORE_BUTTON_WIDTH = 30;
const GAP_BETWEEN_BUTTONS = 4;
const Overflow = ({
  width,
  toolbarButtons,
  onChange,
  onlyShownButtons
}) => {
  const { jsdomMode } = (0, import_react.useContext)(import_ricos_context.RicosContext);
  const [containerRef, setContainerRef] = (0, import_react.useState)(null);
  const buttonsToRender = new import_models.ToolbarButtons(
    toolbarButtons.toolbarButtons.filter((button) => onlyShownButtons === void 0 || onlyShownButtons.includes(button.getId())).sort(
      (a, b) => (onlyShownButtons?.findIndex((id) => id === a.getId()) ?? 0) - (onlyShownButtons?.findIndex((id) => id === b.getId()) ?? 0)
    )
  );
  const alwaysOverflownButtons = toolbarButtons.toolbarButtons.filter(
    (button) => onlyShownButtons !== void 0 && !onlyShownButtons.includes(button.getId())
  );
  (0, import_react.useEffect)(() => {
    let sumVisible = 0;
    const visibleButtons = new import_models.ToolbarButtons([]);
    const overflownButtons = new import_models.ToolbarButtons([]);
    if (!containerRef) {
      return;
    }
    const elements = Array.from(containerRef.children).map((child) => {
      return {
        child,
        width: Math.ceil(child.getBoundingClientRect().width)
      };
    });
    let stackIsFull = false;
    elements.forEach((element, index) => {
      if (!stackIsFull && sumVisible + element.width <= width - MORE_BUTTON_WIDTH || jsdomMode) {
        visibleButtons.addButton(buttonsToRender.getButtonByIndex(index));
        sumVisible += element.width + GAP_BETWEEN_BUTTONS;
      } else {
        stackIsFull = true;
        overflownButtons.addButton(buttonsToRender.getButtonByIndex(index));
      }
    });
    alwaysOverflownButtons.forEach((button) => {
      overflownButtons.addButton(button);
    });
    onChange({
      visibleButtons,
      overflownButtons
    });
  }, [width, toolbarButtons, containerRef]);
  return /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      style: { visibility: "hidden", position: "absolute", display: "flex" },
      ref: setContainerRef
    },
    buttonsToRender.getButtonsElements()
  );
};
const useSizeCalculator = ({
  width,
  toolbarButtons,
  onlyShownButtons
}) => {
  const [data, setData] = (0, import_react.useState)();
  const OverflowCalculator = (0, import_react.useCallback)(
    () => /* @__PURE__ */ import_react.default.createElement(
      Overflow,
      {
        width: Number(width),
        toolbarButtons,
        onChange: setData,
        onlyShownButtons
      }
    ),
    [width, toolbarButtons, onlyShownButtons, setData]
  );
  return { data, OverflowCalculator };
};
//# sourceMappingURL=use-size-calculator.js.map
