"use strict";

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

const RegistryContext = /*#__PURE__*/(0, _react.createContext)(undefined);
RegistryContext.displayName = 'RegistryContext';
const useRegistryContext = () => (0, _react.useContext)(RegistryContext);
var _default = exports.default = RegistryContext;
class Registry {
  constructor() {
    (0, _defineProperty2.default)(this, "items", []);
  }
  register(item) {
    this.items.push(item);
  }
  unregister(item) {
    this.items = this.items.filter(i => i !== item);
  }
  compact() {
    let changed = false;
    for (const item of [...this.items].reverse()) {
      if (!item.isCompact) {
        item.compact();
        changed = true;
        break;
      }
    }
    return changed;
  }
  expand() {
    let changed = false;
    for (const item of this.items) {
      if (this.checkItemIsCompactOrHasCompactItems(item)) {
        item.expand();
        changed = true;
        break;
      }
    }
    return changed;
  }
  get isCompact() {
    return this.items.every(_ref => {
      let {
        isCompact
      } = _ref;
      return isCompact;
    });
  }
  get hasCompactItems() {
    return this.items.some(this.checkItemIsCompactOrHasCompactItems);
  }
  checkItemIsCompactOrHasCompactItems(item) {
    return item.isCompact || 'hasCompactItems' in item && item.hasCompactItems;
  }
}
class RegistryItem {
  constructor(_ref2) {
    let {
      onCompactStateChange
    } = _ref2;
    (0, _defineProperty2.default)(this, "_isCompact", false);
    (0, _defineProperty2.default)(this, "onCompactStateChange", void 0);
    this.onCompactStateChange = onCompactStateChange;
  }
  compact() {
    this._isCompact = true;
    this.onCompactStateChange(true);
  }
  expand() {
    this._isCompact = false;
    this.onCompactStateChange(false);
  }
  get isCompact() {
    return this._isCompact;
  }
}
const useRegistry = () => {
  const parentRegistry = useRegistryContext();
  const [registry] = (0, _react.useState)(() => new Registry());
  (0, _react.useLayoutEffect)(() => {
    if (!parentRegistry) return undefined;
    parentRegistry.register(registry);
    return () => {
      parentRegistry.unregister(registry);
    };
  }, [parentRegistry, registry]);
  return registry;
};
exports.useRegistry = useRegistry;
const useRegistryItem = () => {
  const registry = useRegistryContext();
  const [isCompact, setIsCompact] = (0, _react.useState)(false);
  const [registryItem] = (0, _react.useState)(() => new RegistryItem({
    onCompactStateChange: setIsCompact
  }));
  (0, _react.useLayoutEffect)(() => {
    if (!registry) return undefined;
    registry.register(registryItem);
    return () => {
      registry.unregister(registryItem);
    };
  }, [registry, registryItem]);
  return [isCompact];
};
exports.useRegistryItem = useRegistryItem;