'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var vue = require('vue');
var confirm = require('../confirm.cjs');
var destroyFns = require('../destroyFns.cjs');
var HookModal = require('./HookModal.cjs');

let uuid = 0;
const ElementsHolder = /* @__PURE__ */ vue.defineComponent({
  name: "ElementsHolder",
  inheritAttrs: false,
  setup(_, {
    expose
  }) {
    const modals = vue.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 = vue.shallowRef(null);
  const actionQueue = vue.shallowRef([]);
  vue.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 = vue.shallowRef(true);
    const modalRef = vue.shallowRef(null);
    const configRef = vue.shallowRef(vue.unref(config));
    const updateConfig = vue.shallowRef({});
    const updateAction = (newConfig) => {
      configRef.value = {
        ...configRef.value,
        ...newConfig
      };
    };
    vue.watch(() => config, (val) => {
      updateAction({
        ...vue.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 = () => vue.createVNode(HookModal.default, {
      "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.default.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 = vue.computed(() => ({
    info: getConfirmFunc(confirm.withInfo),
    success: getConfirmFunc(confirm.withSuccess),
    error: getConfirmFunc(confirm.withError),
    warning: getConfirmFunc(confirm.withWarn),
    confirm: getConfirmFunc(confirm.withConfirm)
  }));
  const holderKey = Symbol("modalHolderKey");
  return [fns.value, () => vue.createVNode(ElementsHolder, {
    "key": holderKey,
    "ref": holderRef
  }, null)];
}

exports.default = useModal;
//# sourceMappingURL=index.cjs.map
