"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useToolMessage = useToolMessage;
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 i18n_1 = require("../components/organisms/ToolMessage/i18n/index.js");
const useAutoCollapseOnCancelled_1 = require("./useAutoCollapseOnCancelled.js");
const useAutoCollapseOnSuccess_1 = require("./useAutoCollapseOnSuccess.js");
function getDefaultFooterActions(status, footerActions, onAccept, onReject) {
    if (footerActions !== undefined) {
        return footerActions;
    }
    switch (status) {
        case 'waitingConfirmation':
            return [
                {
                    label: (0, i18n_1.i18n)('action-reject'),
                    onClick: onReject,
                    view: 'outlined',
                },
                {
                    label: (0, i18n_1.i18n)('action-accept'),
                    onClick: onAccept,
                    view: 'action',
                },
            ];
        case 'waitingSubmission':
            return [
                {
                    label: (0, i18n_1.i18n)('action-cancel'),
                    onClick: onReject,
                    view: 'outlined',
                },
            ];
        default:
            return [];
    }
}
function getDefaultFooterMessage(status, footerContent) {
    if (footerContent) {
        return footerContent;
    }
    switch (status) {
        case 'waitingConfirmation':
            return (0, i18n_1.i18n)('status-waiting-confirmation');
        case 'waitingSubmission':
            return (0, i18n_1.i18n)('status-waiting-submission');
        default:
            return undefined;
    }
}
function getShowLoader(status) {
    return status === 'waitingConfirmation' || status === 'waitingSubmission';
}
function isWaitingTool(status) {
    return status === 'waitingConfirmation' || status === 'waitingSubmission';
}
function getDefaultInitialExpanded(status, initialExpanded) {
    if (initialExpanded !== undefined) {
        return initialExpanded;
    }
    return status === 'waitingConfirmation' || status === 'waitingSubmission';
}
const defaultHeaderActions = [];
function useToolMessage(options) {
    const { footerActions, headerActions = defaultHeaderActions, bodyContent, status, footerContent, expandable = Boolean(bodyContent), initialExpanded, autoCollapseOnSuccess, autoCollapseOnCancelled, onAccept, onReject, } = options;
    const [isExpanded, setIsExpanded] = (0, react_1.useState)(getDefaultInitialExpanded(status, initialExpanded));
    (0, useAutoCollapseOnSuccess_1.useAutoCollapseOnSuccess)({ enabled: Boolean(autoCollapseOnSuccess), status, setIsExpanded });
    (0, useAutoCollapseOnCancelled_1.useAutoCollapseOnCancelled)({ enabled: Boolean(autoCollapseOnCancelled), status, setIsExpanded });
    const toggleExpanded = (0, react_1.useCallback)(() => {
        setIsExpanded((prev) => !prev);
    }, []);
    const expandCollapseAction = (0, react_1.useMemo)(() => {
        if (!expandable) {
            return null;
        }
        return {
            label: isExpanded ? (0, i18n_1.i18n)('action-collapse') : (0, i18n_1.i18n)('action-expand'),
            onClick: toggleExpanded,
            icon: (0, jsx_runtime_1.jsx)(uikit_1.Icon, { data: isExpanded ? icons_1.ChevronUp : icons_1.ChevronDown, size: 16 }),
        };
    }, [expandable, isExpanded, toggleExpanded]);
    const finalHeaderActions = (0, react_1.useMemo)(() => {
        return expandCollapseAction ? [...headerActions, expandCollapseAction] : headerActions;
    }, [headerActions, expandCollapseAction]);
    const finalFooterActions = (0, react_1.useMemo)(() => {
        return getDefaultFooterActions(status, footerActions, onAccept, onReject);
    }, [status, footerActions, onAccept, onReject]);
    const finalFooterMessage = getDefaultFooterMessage(status, footerContent);
    const showLoader = getShowLoader(status);
    const isWaiting = isWaitingTool(status);
    return {
        isExpanded,
        toggleExpanded,
        headerActions: finalHeaderActions,
        footerActions: finalFooterActions,
        footerContent: finalFooterMessage,
        showLoader,
        isWaiting,
    };
}
//# sourceMappingURL=useToolMessage.js.map
