"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const vue = require("vue");
const BModal_vue_vue_type_script_setup_true_lang = require("../../../BModal.vue_vue_type_script_setup_true_lang-ePg_le3O.js");
const keys = require("../../../keys-D70E9HED.js");
const src_composables_orchestratorShared_index = require("../orchestratorShared/index.umd.js");
require("../../../BootstrapVueOptions-B17IcLXU.js");
require("../../../ConditionalTeleport.vue_vue_type_script_lang-BL9AAILt.js");
require("../../../ConditionalWrapper.vue_vue_type_script_lang-CFfEbslT.js");
require("../../../useTransitions-C9Xv-y_E.js");
require("../../../tableUtils-MbGcRaD0.js");
const useModal = () => {
  const orchestratorRegistry = vue.inject(keys.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 = vue.isRef(obj) ? obj : vue.shallowRef(obj);
    const _self = resolvedProps.value?.id || /* @__PURE__ */ Symbol("Modals controller");
    const promise = src_composables_orchestratorShared_index.buildPromise(_self, store);
    promise.stop = vue.watch(
      resolvedProps,
      (_newValue) => {
        const newValue = { ...vue.toValue(_newValue) };
        const previousIndex = store.value.findIndex((el) => el._self === _self);
        const previous = previousIndex === -1 ? { _component: vue.markRaw(BModal_vue_vue_type_script_setup_true_lang._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 = vue.markRaw(newValue.component);
          } else if (key === "slots" && newValue.slots) {
            v.slots = vue.markRaw(newValue.slots);
          } else {
            v[key] = vue.toValue(newValue[key]);
          }
        }
        v.modelValue = v.modelValue ?? false;
        v["onUpdate:modelValue"] = (val) => {
          const onUpdateModelValue = newValue["onUpdate:modelValue"];
          onUpdateModelValue?.(val);
          const { modelValue } = vue.toValue(obj);
          if (vue.isRef(obj) && !vue.isRef(modelValue)) obj.value.modelValue = val;
          if (vue.isRef(modelValue) && !vue.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
      }
    );
    vue.onScopeDispose(() => {
      const modal = store.value.find((el) => el._self === _self);
      if (modal) {
        modal.promise.value.destroy?.();
      }
    }, true);
    return promise.value;
  };
  const { lastStack, stack, registry } = BModal_vue_vue_type_script_setup_true_lang.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 (vue.toValue(modal2.exposed?.id) === id) {
          return {
            modal: modal2,
            show() {
              modal2.exposed?.show();
            },
            hide(trigger) {
              modal2.exposed?.hide(trigger, true);
            }
          };
        }
      }
    }
    return null;
  };
  const instance = vue.getCurrentInstance();
  const current = () => {
    const findBModal = (component) => {
      if (!component.parent) {
        return null;
      }
      if (component.parent.type === BModal_vue_vue_type_script_setup_true_lang._sfc_main) {
        return component.parent;
      }
      return findBModal(component.parent);
    };
    if (!instance) {
      return null;
    }
    const modalComponent = vue.computed(() => findBModal(instance));
    const modal = vue.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;
exports.useModal = useModal;
exports.useModalController = useModalController;
//# sourceMappingURL=index.umd.js.map
