import { defineComponent, shallowRef, onMounted, onBeforeUnmount, createVNode } from 'vue';
import '../../../utils/index.mjs';
import '../../../hooks/index.mjs';
import Button from './Button.mjs';
import { convertLegacyProps } from './utils.mjs';
import { objectType } from '../../../utils/vue/propType.mjs';
import { useDestroyed } from '../../../hooks/use-destroyed/index.mjs';
import { findDOMNode } from '../../../utils/vue/props.mjs';

const actionButtonProps = {
  type: {
    type: String
  },
  actionFn: Function,
  close: Function,
  autofocus: Boolean,
  prefixCls: String,
  buttonProps: objectType(),
  emitEvent: Boolean,
  quitOnNullishReturnValue: Boolean
};
function isThenable(thing) {
  return !!(thing && thing.then);
}
var ActionButton = /* @__PURE__ */ defineComponent({
  compatConfig: {
    MODE: 3
  },
  name: "ActionButton",
  props: actionButtonProps,
  setup(props, {
    slots
  }) {
    const clickedRef = shallowRef(false);
    const buttonRef = shallowRef();
    const loading = shallowRef(false);
    let timeoutId;
    const isDestroyed = useDestroyed();
    onMounted(() => {
      if (props.autofocus)
        timeoutId = setTimeout(() => {
          var _a, _b;
          return (_b = (_a = findDOMNode(buttonRef.value)) == null ? void 0 : _a.focus) == null ? void 0 : _b.call(_a);
        });
    });
    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 createVNode(Button, {
        ...convertLegacyProps(type),
        "onClick": onClick,
        "loading": loading.value,
        "prefixCls": prefixCls,
        ...buttonProps,
        "ref": buttonRef
      }, slots);
    };
  }
});

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