'use strict';

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

var vue = require('vue');
require('../../../utils/index.cjs');
require('../../../hooks/index.cjs');
var Button = require('./Button.cjs');
var utils = require('./utils.cjs');
var propType = require('../../../utils/vue/propType.cjs');
var index = require('../../../hooks/use-destroyed/index.cjs');
var props = require('../../../utils/vue/props.cjs');

const actionButtonProps = {
  type: {
    type: String
  },
  actionFn: Function,
  close: Function,
  autofocus: Boolean,
  prefixCls: String,
  buttonProps: propType.objectType(),
  emitEvent: Boolean,
  quitOnNullishReturnValue: Boolean
};
function isThenable(thing) {
  return !!(thing && thing.then);
}
var ActionButton = /* @__PURE__ */ vue.defineComponent({
  compatConfig: {
    MODE: 3
  },
  name: "ActionButton",
  props: actionButtonProps,
  setup(props$1, {
    slots
  }) {
    const clickedRef = vue.shallowRef(false);
    const buttonRef = vue.shallowRef();
    const loading = vue.shallowRef(false);
    let timeoutId;
    const isDestroyed = index.useDestroyed();
    vue.onMounted(() => {
      if (props$1.autofocus)
        timeoutId = setTimeout(() => {
          var _a, _b;
          return (_b = (_a = props.findDOMNode(buttonRef.value)) == null ? void 0 : _a.focus) == null ? void 0 : _b.call(_a);
        });
    });
    vue.onBeforeUnmount(() => {
      clearTimeout(timeoutId);
    });
    const onInternalClose = (...args) => {
      var _a;
      (_a = props$1.close) == null ? void 0 : _a.call(props$1, ...args);
    };
    const handlePromiseOnOk = (returnValueOfOnOk) => {
      if (!isThenable(returnValueOfOnOk))
        return;
      loading.value = true;
      returnValueOfOnOk.then((...args) => {
        if (!isDestroyed.value)
          loading.value = false;
        onInternalClose(...args);
        clickedRef.value = false;
      }, (e) => {
        if (!isDestroyed.value)
          loading.value = false;
        clickedRef.value = false;
        return Promise.reject(e);
      });
    };
    const onClick = (e) => {
      const {
        actionFn
      } = props$1;
      if (clickedRef.value)
        return;
      clickedRef.value = true;
      if (!actionFn) {
        onInternalClose();
        return;
      }
      let returnValueOfOnOk;
      if (props$1.emitEvent) {
        returnValueOfOnOk = actionFn(e);
        if (props$1.quitOnNullishReturnValue && !isThenable(returnValueOfOnOk)) {
          clickedRef.value = false;
          onInternalClose(e);
          return;
        }
      } else if (actionFn.length) {
        returnValueOfOnOk = actionFn(props$1.close);
        clickedRef.value = false;
      } else {
        returnValueOfOnOk = actionFn();
        if (!returnValueOfOnOk) {
          onInternalClose();
          return;
        }
      }
      handlePromiseOnOk(returnValueOfOnOk);
    };
    return () => {
      const {
        type,
        prefixCls,
        buttonProps
      } = props$1;
      return vue.createVNode(Button.default, {
        ...utils.convertLegacyProps(type),
        "onClick": onClick,
        "loading": loading.value,
        "prefixCls": prefixCls,
        ...buttonProps,
        "ref": buttonRef
      }, slots);
    };
  }
});

exports.default = ActionButton;
//# sourceMappingURL=ActionButton.cjs.map
