"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useSqueeze = void 0;
var _react = require("react");
var _useResizeObserver = require("./useResizeObserver");
// Copyright 1999-2023. Plesk International GmbH. All rights reserved.

const useSqueeze = _ref => {
  let {
    ref,
    compact,
    expand
  } = _ref;
  const minWidth = (0, _react.useRef)(0);
  const lastAction = (0, _react.useRef)(null);
  const [recheck, setRecheck] = (0, _react.useState)(false);
  const checkRef = (0, _react.useRef)();
  const check = () => {
    if (!ref.current) return;
    const root = ref.current;
    const width = Math.ceil(root.getBoundingClientRect().width);
    const scrollWidth = Math.floor(root.scrollWidth);
    let isCompactChanged = false;
    if (scrollWidth > width) {
      isCompactChanged = compact();
      minWidth.current = scrollWidth;
      lastAction.current = 'compact';
    } else if (minWidth.current && width > minWidth.current || lastAction.current === 'expand') {
      isCompactChanged = expand();
      lastAction.current = 'expand';
    }
    if (isCompactChanged) {
      setRecheck(c => !c);
    }
  };
  checkRef.current = check;
  (0, _react.useLayoutEffect)(() => {
    checkRef.current?.();
  }, [recheck]);
  (0, _useResizeObserver.useResizeObserver)({
    ref,
    onResize: check
  });
};
exports.useSqueeze = useSqueeze;