import _defineProperty from "@babel/runtime/helpers/defineProperty";
// Copyright 1999-2025. WebPros International GmbH. All rights reserved.

import { createContext, useContext, useState, useLayoutEffect } from 'react';
const RegistryContext = /*#__PURE__*/createContext(undefined);
RegistryContext.displayName = 'RegistryContext';
const useRegistryContext = () => useContext(RegistryContext);
export default RegistryContext;
class Registry {
  constructor() {
    _defineProperty(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 (item.isExpandable) {
        item.expand();
        changed = true;
        break;
      }
    }
    return changed;
  }
  get isCompact() {
    return this.items.every(_ref => {
      let {
        isCompact
      } = _ref;
      return isCompact;
    });
  }
  get isExpandable() {
    return this.items.some(_ref2 => {
      let {
        isExpandable
      } = _ref2;
      return isExpandable;
    });
  }
}
class RegistryItem {
  constructor(_ref3) {
    let {
      onCompactStateChange
    } = _ref3;
    _defineProperty(this, "_isCompact", false);
    _defineProperty(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;
  }
  get isExpandable() {
    return this._isCompact;
  }
}
export const useRegistry = () => {
  const parentRegistry = useRegistryContext();
  const [registry] = useState(() => new Registry());
  useLayoutEffect(() => {
    if (!parentRegistry) return undefined;
    parentRegistry.register(registry);
    return () => {
      parentRegistry.unregister(registry);
    };
  }, [parentRegistry, registry]);
  return registry;
};
export const useRegistryItem = () => {
  const registry = useRegistryContext();
  const [isCompact, setIsCompact] = useState(false);
  const [registryItem] = useState(() => new RegistryItem({
    onCompactStateChange: setIsCompact
  }));
  useLayoutEffect(() => {
    if (!registry) return undefined;
    registry.register(registryItem);
    return () => {
      registry.unregister(registryItem);
    };
  }, [registry, registryItem]);
  return [isCompact];
};