import clsx from 'classnames';
import React, { useContext, useEffect } from 'react';
import { ActionButtonContext } from '../context';
export function ActionButton(props, ref) {
    const { className, action, onClick, icon, type = 'button', shape, ...restProps } = props;
    const context = useContext(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);
    useEffect(() => {
        if (action === 'onSend') {
            setSubmitDisabled === null || setSubmitDisabled === void 0 ? void 0 : setSubmitDisabled(mergedDisabled);
        }
    }, [mergedDisabled, action, setSubmitDisabled]);
    const buttonClassName = clsx(prefixCls, className, {
        [`${prefixCls}-disabled`]: mergedDisabled,
        [`${prefixCls}-circle`]: shape === 'circle',
        [`${prefixCls}-round`]: shape === 'round',
    });
    return (React.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));
}
export default React.forwardRef(ActionButton);
