"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ActionButton = ActionButton;
const classnames_1 = __importDefault(require("classnames"));
const react_1 = __importStar(require("react"));
const context_1 = require("../context");
function ActionButton(props, ref) {
    const { className, action, onClick, icon, type = 'button', shape, ...restProps } = props;
    const context = (0, react_1.useContext)(context_1.ActionButtonContext);
    if (!context) {
        throw new Error('ActionButton must be used within ActionButtonContext.Provider');
    }
    const { prefixCls, disabled: rootDisabled, setSubmitDisabled, canEmptySubmit } = context;
    // 计算按钮的禁用状态：显式传入的 disabled（如编辑态）优先；发送按钮且允许空提交时不禁用；否则用 context
    const contextDisabled = rootDisabled !== null && rootDisabled !== void 0 ? rootDisabled : context[`${action}Disabled`];
    const sendEmptyEnabled = action === 'onSend' && canEmptySubmit;
    const mergedDisabled = restProps.disabled === true || (!sendEmptyEnabled && contextDisabled);
    (0, react_1.useEffect)(() => {
        if (action === 'onSend') {
            setSubmitDisabled === null || setSubmitDisabled === void 0 ? void 0 : setSubmitDisabled(mergedDisabled);
        }
    }, [mergedDisabled, action, setSubmitDisabled]);
    const buttonClassName = (0, classnames_1.default)(prefixCls, className, {
        [`${prefixCls}-disabled`]: mergedDisabled,
        [`${prefixCls}-circle`]: shape === 'circle',
        [`${prefixCls}-round`]: shape === 'round',
    });
    return (react_1.default.createElement("button", { type: type, ...restProps, ref: ref, onClick: (e) => {
            var _a;
            if (mergedDisabled) {
                return;
            }
            (_a = context[action]) === null || _a === void 0 ? void 0 : _a.call(context); // 调用 context.onSend()
            onClick === null || onClick === void 0 ? void 0 : onClick(e); // 执行自定义 onClick（如果有）
        }, disabled: mergedDisabled, className: buttonClassName },
        icon,
        restProps.children));
}
exports.default = react_1.default.forwardRef(ActionButton);
