'use strict';

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

var vue = require('vue');
require('../../button/index.js');
var buttonTypes = require('../../button/buttonTypes.js');
require('../../hooks/index.js');
var type = require('../type.js');
var index = require('../props-util/index.js');
var useDestroyed = require('../../hooks/useDestroyed.js');
var button = require('../../button/button.js');

const actionButtonProps = {
  type: {
    type: String
  },
  actionFn: Function,
  close: Function,
  autofocus: Boolean,
  prefixCls: String,
  buttonProps: type.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, {
    slots
  }) {
    const clickedRef = vue.shallowRef(false);
    const buttonRef = vue.shallowRef();
    const loading = vue.shallowRef(false);
    let timeoutId;
    const isDestroyed = useDestroyed.useDestroyed();
    vue.onMounted(() => {
      if (props.autofocus)
        timeoutId = setTimeout(() => {
          var _a, _b;
          return (_b = (_a = index.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.close) == null ? void 0 : _a.call(props, ...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;
      if (clickedRef.value)
        return;
      clickedRef.value = true;
      if (!actionFn) {
        onInternalClose();
        return;
      }
      let returnValueOfOnOk;
      if (props.emitEvent) {
        returnValueOfOnOk = actionFn(e);
        if (props.quitOnNullishReturnValue && !isThenable(returnValueOfOnOk)) {
          clickedRef.value = false;
          onInternalClose(e);
          return;
        }
      } else if (actionFn.length) {
        returnValueOfOnOk = actionFn(props.close);
        clickedRef.value = false;
      } else {
        returnValueOfOnOk = actionFn();
        if (!returnValueOfOnOk) {
          onInternalClose();
          return;
        }
      }
      handlePromiseOnOk(returnValueOfOnOk);
    };
    return () => {
      const {
        type,
        prefixCls,
        buttonProps
      } = props;
      return vue.createVNode(button.default, vue.mergeProps(buttonTypes.convertLegacyProps(type), {
        "onClick": onClick,
        "loading": loading.value,
        "prefixCls": prefixCls
      }, buttonProps, {
        "ref": buttonRef
      }), slots);
    };
  }
});

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