import { inject, getCurrentInstance, isRef, shallowRef, watch, toValue, markRaw, onScopeDispose, computed, isReadonly } from "vue";
import { u as useSharedModalStack, _ as _sfc_main } from "../../../BModal.vue_vue_type_script_setup_true_lang-SIs6PFOZ.mjs";
import { o as orchestratorRegistryKey } from "../../../keys-D5Pj1dmf.mjs";
import { buildPromise } from "../orchestratorShared/index.mjs";
import "../../../BootstrapVueOptions-DiQ3EVFN.mjs";
import "../../../ConditionalTeleport.vue_vue_type_script_lang-Cp-egLmm.mjs";
import "../../../ConditionalWrapper.vue_vue_type_script_lang-IX_NpHH-.mjs";
import "../../../useTransitions-BAT154ve.mjs";
import "../../../tableUtils-CEJcbxqj.mjs";
const useModal = () => {
  const orchestratorRegistry = inject(orchestratorRegistryKey, null);
  if (!orchestratorRegistry) {
    throw Error(
      "useModal() must be called within setup(), and BApp, useRegistry or plugin must be installed/provided."
    );
  }
  const { store, _isOrchestratorInstalled } = orchestratorRegistry;
  const create = (obj = {}, options = {}) => {
    if (!_isOrchestratorInstalled.value) {
      throw new Error("BApp component must be mounted to use the modal controller");
    }
    const resolvedProps = isRef(obj) ? obj : shallowRef(obj);
    const _self = resolvedProps.value?.id || /* @__PURE__ */ Symbol("Modals controller");
    const promise = buildPromise(_self, store);
    promise.stop = watch(
      resolvedProps,
      (_newValue) => {
        const newValue = { ...toValue(_newValue) };
        const previousIndex = store.value.findIndex((el) => el._self === _self);
        const previous = previousIndex === -1 ? { _component: markRaw(_sfc_main) } : store.value[previousIndex];
        const v = {
          type: "modal",
          _self,
          position: "modal",
          ...previous,
          options,
          promise
        };
        for (const key in newValue) {
          if (key.startsWith("on")) {
            v[key] = newValue[key];
          } else if (key === "component" && newValue.component) {
            v._component = markRaw(newValue.component);
          } else if (key === "slots" && newValue.slots) {
            v.slots = markRaw(newValue.slots);
          } else {
            v[key] = toValue(newValue[key]);
          }
        }
        v.modelValue = v.modelValue ?? false;
        v["onUpdate:modelValue"] = (val) => {
          const onUpdateModelValue = newValue["onUpdate:modelValue"];
          onUpdateModelValue?.(val);
          const { modelValue } = toValue(obj);
          if (isRef(obj) && !isRef(modelValue)) obj.value.modelValue = val;
          if (isRef(modelValue) && !isReadonly(modelValue)) {
            modelValue.value = val;
          }
          const modal = store.value.find((el) => el._self === _self);
          if (modal) {
            modal.modelValue = val;
          }
        };
        if (previousIndex === -1) {
          store.value.push(v);
        } else {
          store.value.splice(previousIndex, 1, v);
        }
      },
      {
        immediate: true,
        deep: true
      }
    );
    onScopeDispose(() => {
      const modal = store.value.find((el) => el._self === _self);
      if (modal) {
        modal.promise.value.destroy?.();
      }
    }, true);
    return promise.value;
  };
  const { lastStack, stack, registry } = useSharedModalStack();
  const show = (id) => {
    if (id === void 0) {
      if (lastStack?.value) {
        lastStack?.value.exposed?.show();
      }
    } else {
      const stackModal = stack?.value.find((modal2) => modal2.exposed?.id === id);
      if (stackModal) {
        stackModal.exposed?.show();
        return;
      }
      const modal = store.value.find((el) => el._self === id);
      if (modal) {
        modal.modelValue = true;
        modal["onUpdate:modelValue"]?.(true);
      } else {
        stack?.value.forEach((modal2) => {
          if (modal2.exposed?.id === id) {
            modal2.exposed?.show();
          }
        });
      }
    }
  };
  const hide = (trigger, id) => {
    if (id === void 0) {
      if (lastStack?.value) {
        lastStack?.value.exposed?.hide(trigger);
      }
    } else {
      const stackModal = stack?.value.find((modal2) => modal2.exposed?.id === id);
      if (stackModal) {
        stackModal.exposed?.hide(trigger);
        return;
      }
      const modal = store.value.find((el) => el._self === id);
      if (modal) {
        modal.modelValue = false;
        modal["onUpdate:modelValue"]?.(false);
      } else {
        stack?.value.forEach((modal2) => {
          if (modal2.exposed?.id === id) {
            modal2.exposed?.hide(trigger, true);
          }
        });
      }
    }
  };
  const hideAll = (trigger) => {
    stack?.value.forEach((modal) => {
      modal.exposed?.hide(trigger, true);
    });
  };
  const get = (id) => {
    const modal = store.value.find((el) => el._self === id);
    if (modal) {
      return {
        modal,
        show() {
          modal?.promise.value.show();
        },
        hide(trigger) {
          modal?.promise.value.hide(trigger);
        }
      };
    }
    if (registry?.value) {
      for (const [, modal2] of registry?.value.entries() ?? []) {
        if (toValue(modal2.exposed?.id) === id) {
          return {
            modal: modal2,
            show() {
              modal2.exposed?.show();
            },
            hide(trigger) {
              modal2.exposed?.hide(trigger, true);
            }
          };
        }
      }
    }
    return null;
  };
  const instance = getCurrentInstance();
  const current = () => {
    const findBModal = (component) => {
      if (!component.parent) {
        return null;
      }
      if (component.parent.type === _sfc_main) {
        return component.parent;
      }
      return findBModal(component.parent);
    };
    if (!instance) {
      return null;
    }
    const modalComponent = computed(() => findBModal(instance));
    const modal = computed(() => modalComponent.value?.proxy);
    return {
      show() {
        modalComponent.value?.exposed?.show();
      },
      hide(trigger) {
        modalComponent.value?.exposed?.hide(trigger, true);
      },
      modal
    };
  };
  return {
    show,
    hide,
    hideAll,
    get,
    current,
    create,
    _isOrchestratorInstalled,
    store
    // Todo: Supports listening events globally in the future
  };
};
const useModalController = useModal;
export {
  useModal,
  useModalController
};
//# sourceMappingURL=index.mjs.map
