"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SubmitButton = SubmitButton;
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("react");
const icons_1 = require("@gravity-ui/icons");
const uikit_1 = require("@gravity-ui/uikit");
const cn_1 = require("../../../utils/cn.js");
const ActionButton_1 = require("../ActionButton/index.js");
const i18n_1 = require("./i18n/index.js");
require("./SubmitButton.css");
const b = (0, cn_1.block)('submit-button');
/**
 * Submit button component with state management through props
 *
 * States:
 * - disabled: disabled button state
 * - enabled: enabled button state
 * - loading: loading state
 * - cancelable: cancellation state (during loading)
 *
 * @returns Submit button component
 */
function SubmitButton({ onClick, state, className, size = 'm', tooltipSend, tooltipCancel, cancelableText, qa, }) {
    const isCancelable = state === 'cancelable';
    const isLoading = state === 'loading';
    const isDisabled = state === 'disabled';
    const handleClick = (0, react_1.useCallback)(async () => {
        if (['enabled', 'cancelable'].includes(state)) {
            return onClick();
        }
        return Promise.resolve();
    }, [state, onClick]);
    // Get tooltip based on state
    const getTooltipTitle = () => {
        switch (state) {
            case 'enabled':
                return tooltipSend || (0, i18n_1.i18n)('tooltip-send');
            case 'cancelable':
                return tooltipCancel || (0, i18n_1.i18n)('tooltip-cancel');
            case 'disabled':
            case 'loading':
                return undefined;
            default:
                return undefined;
        }
    };
    const renderContent = () => {
        if (isLoading) {
            return ((0, jsx_runtime_1.jsx)("div", { className: b('loader'), children: (0, jsx_runtime_1.jsx)(uikit_1.Spin, { className: b('spinner'), size: "xs" }) }));
        }
        if (isCancelable) {
            return cancelableText ? ([(0, jsx_runtime_1.jsx)(uikit_1.Icon, { size: 16, data: icons_1.CircleStop }, "icon"), cancelableText]) : ((0, jsx_runtime_1.jsx)(uikit_1.Icon, { size: 16, data: icons_1.CircleStop }));
        }
        return (0, jsx_runtime_1.jsx)(uikit_1.Icon, { size: 16, data: icons_1.ArrowUp });
    };
    return ((0, jsx_runtime_1.jsx)(ActionButton_1.ActionButton, { view: "action", size: size, color: "brand", disabled: isDisabled, onClick: handleClick, className: b({ size, loading: isLoading, cancelable: isCancelable }, className), qa: qa, tooltipTitle: getTooltipTitle(), children: renderContent() }));
}
//# sourceMappingURL=SubmitButton.js.map
