// Copyright 1999-2023. Plesk International GmbH. All rights reserved.

import { createContext } from 'react';
const RegistryContext = /*#__PURE__*/createContext(undefined);
RegistryContext.displayName = 'RegistryContext';
export default RegistryContext;
export const createRegistry = () => ({
  stack: [],
  register(item) {
    this.stack.push(item);
  },
  unregister(item) {
    this.stack = this.stack.filter(i => i !== item);
  },
  uncompact() {
    let changed = false;
    this.stack.every(item => {
      if (item.isCompact(true)) {
        item.setCompact(false);
        changed = true;
        return false;
      }
      return true;
    });
    return changed;
  },
  compact() {
    let changed = false;
    [...this.stack].reverse().every(item => {
      if (!item.isCompact(false)) {
        item.setCompact(true);
        changed = true;
        return false;
      }
      return true;
    });
    return changed;
  },
  isCompact() {
    let one = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
    if (one) {
      return this.stack.some(item => item.isCompact(one));
    }
    return this.stack.every(item => item.isCompact(one));
  }
});