import { inject, provide, ref, computed, readonly } from "vue";
import { s as showHideRegistryKey, m as modalManagerKey, e as breadcrumbRegistryKey, a as breadcrumbGlobalIndexKey, r as rtlRegistryKey } from "../../../keys-D5Pj1dmf.mjs";
const useRegistry = (rtl = false) => {
  const showHideStorage = inject(showHideRegistryKey, null);
  if (!showHideStorage) {
    const { register, values } = _newShowHideRegistry();
    provide(showHideRegistryKey, { register, values });
  }
  const modalManager = inject(modalManagerKey, null);
  if (!modalManager) {
    const stack = ref(/* @__PURE__ */ new Map());
    const countStack = computed(() => stack.value.size);
    const valuesStack = computed(() => [...stack.value.values()]);
    const lastStack = computed(() => valuesStack.value[valuesStack.value.length - 1]);
    const pushStack = (modal) => {
      stack.value.set(modal.uid, modal);
    };
    const removeStack = (modal) => {
      stack.value.delete(modal.uid);
    };
    const registry = ref(/* @__PURE__ */ new Map());
    const pushRegistry = (modal) => {
      registry.value.set(modal.uid, modal);
    };
    const removeRegistry = (modal) => {
      registry.value.delete(modal.uid);
    };
    provide(modalManagerKey, {
      countStack,
      lastStack,
      registry: computed(() => registry.value),
      stack: valuesStack,
      pushStack,
      removeStack,
      pushRegistry,
      removeRegistry
    });
  }
  const breadcrumb = inject(breadcrumbRegistryKey, null);
  if (!breadcrumb) {
    const items = ref({
      [breadcrumbGlobalIndexKey]: []
    });
    const reset = (key = breadcrumbGlobalIndexKey) => {
      items.value[key] = [];
    };
    provide(breadcrumbRegistryKey, { items, reset });
  }
  const rtlRegistry = inject(rtlRegistryKey, null);
  if (!rtlRegistry) {
    const rtlDefault = false;
    const localeDefault = void 0;
    const rtlInitial = typeof rtl === "boolean" ? rtlDefault : rtl?.rtlInitial ?? rtlDefault;
    const localeInitial = typeof rtl === "boolean" ? localeDefault : rtl?.localeInitial ?? localeDefault;
    const isRtl = ref(rtlInitial);
    const locale = ref(localeInitial);
    provide(rtlRegistryKey, { isRtl, locale });
  }
};
const createGetActive = (instances) => () => instances.length > 0 ? instances[instances.length - 1] : void 0;
const _newShowHideRegistry = () => {
  const values = ref(/* @__PURE__ */ new Map());
  const register = ({
    id,
    component,
    value,
    toggle,
    show,
    hide,
    registerTrigger,
    unregisterTrigger
  }) => {
    let currentId = id;
    const instanceValue = {
      id,
      component,
      value: readonly(value),
      toggle,
      show,
      hide,
      registerTrigger,
      unregisterTrigger
    };
    let instancesHolder = values.value.get(currentId);
    if (!instancesHolder) {
      const instances = [];
      instancesHolder = {
        instances,
        // Returns the last mounted instance (most recent)
        getActive: createGetActive(instances)
      };
      values.value.set(currentId, instancesHolder);
    }
    instancesHolder.instances.push(instanceValue);
    const componentUid = component.uid;
    return {
      unregister() {
        const holder = values.value.get(currentId);
        if (!holder) return;
        const index = holder.instances.findIndex(
          (inst) => inst.component.uid === componentUid
        );
        if (index !== -1) {
          holder.instances.splice(index, 1);
        }
        if (holder.instances.length === 0) {
          values.value.delete(currentId);
        }
      },
      updateId(newId, oldId) {
        const holder = values.value.get(oldId);
        if (!holder) return;
        const instance = holder.instances.find(
          (inst) => inst.component.uid === componentUid
        );
        if (!instance) return;
        instance.id = newId;
        let newHolder = values.value.get(newId);
        if (!newHolder) {
          const instances = [];
          newHolder = {
            instances,
            getActive: createGetActive(instances)
          };
          values.value.set(newId, newHolder);
        }
        const index = holder.instances.findIndex(
          (inst) => inst.component.uid === componentUid
        );
        if (index !== -1) {
          holder.instances.splice(index, 1);
          newHolder.instances.push(instance);
        }
        if (holder.instances.length === 0) {
          values.value.delete(oldId);
        }
        currentId = newId;
      }
    };
  };
  return {
    register,
    values
  };
};
export {
  _newShowHideRegistry,
  useRegistry
};
//# sourceMappingURL=index.mjs.map
