import { defineComponent, shallowRef, watch, unref, isRef, createVNode, computed } from 'vue';
import { withInfo, withSuccess, withError, withWarn, withConfirm } from '../confirm.mjs';
import destroyFns from '../destroyFns.mjs';
import HookModal from './HookModal.mjs';

let uuid = 0;
const ElementsHolder = /* @__PURE__ */ defineComponent({
  name: "ElementsHolder",
  inheritAttrs: false,
  setup(_, {
    expose
  }) {
    const modals = shallowRef([]);
    const addModal = (modal) => {
      modals.value.push(modal);
      modals.value = modals.value.slice();
      return () => {
        modals.value = modals.value.filter((currentModal) => currentModal !== modal);
      };
    };
    expose({
      addModal
    });
    return () => {
      return modals.value.map((modal) => modal());
    };
  }
});
function useModal() {
  const holderRef = shallowRef(null);
  const actionQueue = shallowRef([]);
  watch(actionQueue, () => {
    if (actionQueue.value.length) {
      const cloneQueue = [...actionQueue.value];
      cloneQueue.forEach((action) => {
        action();
      });
      actionQueue.value = [];
    }
  }, {
    immediate: true
  });
  const getConfirmFunc = (withFunc) => function hookConfirm(config) {
    var _a;
    uuid += 1;
    const open = shallowRef(true);
    const modalRef = shallowRef(null);
    const configRef = shallowRef(unref(config));
    const updateConfig = shallowRef({});
    const updateAction = (newConfig) => {
      configRef.value = {
        ...configRef.value,
        ...newConfig
      };
    };
    watch(() => config, (val) => {
      updateAction({
        ...isRef(val) ? val.value : val,
        ...updateConfig.value
      });
    });
    const destroyAction = (...args) => {
      open.value = false;
      const triggerCancel = args.some((param) => param && param.triggerCancel);
      if (configRef.value.onCancel && triggerCancel)
        configRef.value.onCancel(() => {
        }, ...args.slice(1));
    };
    let closeFunc;
    const modal = () => createVNode(HookModal, {
      "key": `modal-${uuid}`,
      "config": withFunc(configRef.value),
      "ref": modalRef,
      "open": open.value,
      "destroyAction": destroyAction,
      "afterClose": () => {
        closeFunc == null ? void 0 : closeFunc();
      }
    }, null);
    closeFunc = (_a = holderRef.value) == null ? void 0 : _a.addModal(modal);
    if (closeFunc)
      destroyFns.push(closeFunc);
    const destroy = () => {
      if (modalRef.value)
        destroyAction();
      else
        actionQueue.value = [...actionQueue.value, destroyAction];
    };
    const update = (newConfig) => {
      updateConfig.value = newConfig;
      if (modalRef.value)
        updateAction(newConfig);
      else
        actionQueue.value = [...actionQueue.value, () => updateAction(newConfig)];
    };
    return {
      destroy,
      update
    };
  };
  const fns = computed(() => ({
    info: getConfirmFunc(withInfo),
    success: getConfirmFunc(withSuccess),
    error: getConfirmFunc(withError),
    warning: getConfirmFunc(withWarn),
    confirm: getConfirmFunc(withConfirm)
  }));
  const holderKey = Symbol("modalHolderKey");
  return [fns.value, () => createVNode(ElementsHolder, {
    "key": holderKey,
    "ref": holderRef
  }, null)];
}

export { useModal as default };
//# sourceMappingURL=index.mjs.map
