import { __assign } from "tslib";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useCallback } from 'react';
import Button from '../button';
import { I18nReceiver as Receiver } from '../i18n';
import isPromise from '../utils/isPromise';
import { usePopover } from '../popover/withPopover';
function handleClick(stateKey, changePending, popover, callback) {
    if (typeof callback !== 'function') {
        return popover.close();
    }
    var startClose = function () {
        changePending(stateKey, true);
    };
    var finishClose = function () {
        changePending(stateKey, false, popover.close);
    };
    if (callback.length >= 1) {
        startClose();
        return callback(finishClose);
    }
    var maybePromise = callback();
    if (isPromise(maybePromise)) {
        startClose();
        maybePromise.then(finishClose).catch(function () { return changePending(stateKey, false); });
    }
    else {
        popover.close();
    }
}
function PopAction(_a) {
    var type = _a.type, onConfirm = _a.onConfirm, onCancel = _a.onCancel, confirmText = _a.confirmText, cancelText = _a.cancelText, confirmPending = _a.confirmPending, cancelPending = _a.cancelPending, changePending = _a.changePending;
    var popover = usePopover();
    var onConfirmClick = useCallback(function () {
        handleClick('confirmPending', changePending, popover, onConfirm);
    }, [onConfirm, popover, changePending]);
    var onCancelClick = useCallback(function () {
        handleClick('cancelPending', changePending, popover, onCancel);
    }, [onCancel, popover, changePending]);
    return (_jsxs("div", __assign({ className: "zent-pop-v2-buttons", "data-zv": 'v10.0.11' }, { children: [_jsx(Receiver, __assign({ componentName: "Pop" }, { children: function (i18n) { return (_jsx(Button, __assign({ loading: cancelPending, disabled: confirmPending, size: "small", onClick: onCancelClick }, { children: cancelText || i18n.cancel }), void 0)); } }), void 0), _jsx(Receiver, __assign({ componentName: "Pop" }, { children: function (i18n) { return (_jsx(Button, __assign({ loading: confirmPending, disabled: cancelPending, size: "small", type: type, onClick: onConfirmClick }, { children: confirmText || i18n.confirm }), void 0)); } }), void 0)] }), void 0));
}
export default PopAction;
