import { inject, isRef, shallowRef, watch, toValue, markRaw, isReadonly, onScopeDispose } from "vue";
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";
import { _ as _sfc_main } from "../../../BToast.vue_vue_type_script_setup_true_lang-DAFk-8Vs.mjs";
const posDefault = "top-end";
const useToast = () => {
  const orchestratorRegistry = inject(orchestratorRegistryKey, null);
  if (!orchestratorRegistry) {
    throw new Error(
      "useToast() must be called within setup(), and BApp, useRegistry or plugin must be installed/provided."
    );
  }
  const { store, _isToastAppend, _isOrchestratorInstalled } = orchestratorRegistry;
  const create = (obj = {}, options = {}) => {
    if (!_isOrchestratorInstalled.value) {
      throw new Error("The BApp component must be mounted to use the toast composable");
    }
    const resolvedProps = isRef(obj) ? obj : shallowRef(obj);
    const _self = resolvedProps.value?.id || /* @__PURE__ */ Symbol("Toast 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 = {
          ...previous,
          type: "toast",
          _self,
          promise,
          options
        };
        if (newValue.props) {
          Object.assign(v, newValue.props);
          newValue.props = void 0;
        }
        if (newValue.pos) {
          v.position = newValue.pos;
          newValue.pos = void 0;
        }
        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.position = v.position || posDefault;
        v.modelValue = v.modelValue ?? 5e3;
        v["onUpdate:modelValue"] = (val) => {
          newValue["onUpdate:modelValue"]?.(val);
          const { modelValue } = toValue(obj);
          if (isRef(obj) && !isRef(modelValue)) obj.value.modelValue = val;
          if (isRef(modelValue) && !isReadonly(modelValue)) {
            modelValue.value = val;
          }
          if (v.modelValue !== val) {
            const toast = store.value.find((el) => el._self === _self);
            if (toast) {
              toast.modelValue = val;
            }
          }
        };
        if (previousIndex === -1) {
          if (resolvedProps.value?.appendToast !== void 0 ? resolvedProps.value.appendToast : _isToastAppend.value) {
            store.value.push(v);
          } else {
            store.value.unshift(v);
          }
        } else {
          store.value.splice(previousIndex, 1, v);
        }
      },
      {
        deep: true,
        immediate: true
      }
    );
    onScopeDispose(() => {
      const toast = store.value.find((el) => el._self === _self);
      if (toast) {
        toast.promise.value.destroy?.();
      }
    }, true);
    return promise.value;
  };
  const show = (obj = {}) => {
    console.warn(
      "[BootstrapVueNext] useToast: The `show` method is deprecated. Use the `create` method instead."
    );
    return create(obj);
  };
  return {
    _isToastAppend,
    _isOrchestratorInstalled,
    store,
    create,
    show
  };
};
const useToastController = useToast;
export {
  useToast,
  useToastController
};
//# sourceMappingURL=index.mjs.map
