"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _react = require("react");
var _reactMeasure = _interopRequireDefault(require("react-measure"));
var _utils = require("../utils");
var _jsxRuntime = require("react/jsx-runtime");
// Copyright 1999-2023. Plesk International GmbH. All rights reserved.

class Squeezer extends _react.Component {
  constructor() {
    super(...arguments);
    (0, _defineProperty2.default)(this, "rootRef", /*#__PURE__*/(0, _react.createRef)());
    (0, _defineProperty2.default)(this, "measuring", false);
    (0, _defineProperty2.default)(this, "measurements", []);
    (0, _defineProperty2.default)(this, "minWidth", 0);
    (0, _defineProperty2.default)(this, "handleResize", () => {
      this.measure();
    });
  }
  addMeasurement(width) {
    this.measurements.push(width);
    if (this.measurements.length > 2) {
      this.measurements.shift();
    }
  }
  measure() {
    let force = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
    const {
      compact,
      uncompact
    } = this.props;
    if (this.measuring && !force) {
      return;
    }
    this.measuring = true;
    const root = this.rootRef.current;
    const width = Math.ceil(root.getBoundingClientRect().width);
    const scrollWidth = Math.ceil(root.scrollWidth);
    let checkForce = false;
    if (width < scrollWidth) {
      if (compact()) {
        this.minWidth = scrollWidth;
        checkForce = true;
      }
    } else if (width > this.minWidth) {
      if (uncompact()) {
        this.minWidth = width;
        checkForce = true;
      }
    }
    this.addMeasurement(width);
    this.check(checkForce);
  }
  check() {
    let force = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
    if (!force && this.measurements.length === 2 && this.measurements[0] === this.measurements[1]) {
      this.measuring = false;
    } else {
      this.setState({}, () => {
        this.measure(true);
      });
    }
  }
  render() {
    const {
      children,
      innerRef,
      innerMeasure
    } = this.props;
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactMeasure.default, {
      onResize: this.handleResize,
      innerRef: el => {
        if (el) {
          this.rootRef.current = el;
          (0, _utils.safeInvoke)(innerRef, el);
          (0, _utils.safeInvoke)(innerMeasure, () => {
            this.measure(true);
          });
        }
      },
      children: _ref => {
        let {
          measureRef
        } = _ref;
        return children({
          ref: measureRef
        });
      }
    });
  }
}
var _default = exports.default = Squeezer;