import { ref, inject, provide, nextTick, toValue } from "vue";
import { o as orchestratorRegistryKey } from "../../../keys-D5Pj1dmf.mjs";
function buildPromise(_id, store) {
  let resolveFunc = () => {
  };
  const promise = new Promise((resolve) => {
    resolveFunc = resolve;
  });
  Object.assign(promise, {
    id: _id,
    ref: null,
    show() {
      const refWithMethods = this.ref;
      if (refWithMethods?.show) {
        refWithMethods.show();
      } else {
        this.set({ modelValue: true });
      }
      const self = this;
      const asyncDispose = async () => {
        await self.destroy();
      };
      const showPromise = promise.then(
        (event) => Object.assign(Object.create(event), {
          [Symbol.asyncDispose]: asyncDispose
        })
      );
      return Object.assign(showPromise, {
        id: self.id,
        ref: self.ref,
        show: () => self.show(),
        hide: (trigger) => self.hide(trigger),
        toggle: () => self.toggle(),
        set: (val) => self.set(val),
        get: () => self.get(),
        destroy: () => self.destroy(),
        [Symbol.asyncDispose]: asyncDispose
      });
    },
    hide(trigger) {
      const refWithMethods = this.ref;
      if (refWithMethods?.hide) {
        refWithMethods.hide(trigger, true);
      } else return this.set({ modelValue: false });
      return promise;
    },
    toggle() {
      const currentItem = this.get();
      const refWithMethods = this.ref;
      if (refWithMethods?.toggle) {
        refWithMethods.toggle();
      } else return this.set({ modelValue: !currentItem?.modelValue });
      return promise;
    },
    get() {
      return store.value.find((el) => el._self === _id);
    },
    set(val) {
      const itemIndex = store.value.findIndex((el) => el._self === _id);
      const item = store.value[itemIndex];
      if (item) {
        const v = { ...toValue(item), ...toValue(val) };
        if (item.modelValue !== v.modelValue) {
          item["onUpdate:modelValue"]?.(v.modelValue);
        }
        const updatedItem = {
          ...v,
          modelValue: toValue(v.modelValue)
        };
        const vRecord = v;
        if ("title" in vRecord && vRecord.title !== void 0) {
          updatedItem.title = toValue(vRecord.title);
        }
        if ("body" in vRecord && vRecord.body !== void 0) {
          updatedItem.body = toValue(vRecord.body);
        }
        store.value.splice(itemIndex, 1, updatedItem);
      }
      return promise;
    },
    async destroy() {
      const item = store.value.find((el) => el._self === _id);
      if (!item) return;
      item.promise.stop?.();
      if (item.modelValue) {
        await new Promise((resolve) => {
          const prev = item["onHidden"];
          item["onHidden"] = (e) => {
            prev?.(e);
            resolve(e);
          };
          nextTick(() => {
            this.hide("destroy");
          });
        });
      }
      store.value.splice(
        store.value.findIndex((el) => el._self === _id),
        1
      );
    },
    async [Symbol.asyncDispose]() {
      await this.destroy();
    }
  });
  return {
    value: promise,
    resolve: resolveFunc,
    stop: void 0
  };
}
const _newOrchestratorRegistry = () => ({
  store: ref([]),
  _isOrchestratorInstalled: ref(false),
  _isToastAppend: ref(false)
});
const useOrchestratorRegistry = () => {
  const orchestratorRegistry = inject(orchestratorRegistryKey, null);
  if (orchestratorRegistry) {
    return orchestratorRegistry;
  }
  const newOrchestratorRegistry = _newOrchestratorRegistry();
  provide(orchestratorRegistryKey, newOrchestratorRegistry);
  return newOrchestratorRegistry;
};
export {
  _newOrchestratorRegistry,
  buildPromise,
  useOrchestratorRegistry
};
//# sourceMappingURL=index.mjs.map
