import { jsx as _jsx } from "react/jsx-runtime";
import { useCallback, useMemo, useState } from 'react';
import { ChevronDown, ChevronUp } from '@gravity-ui/icons';
import { Icon } from '@gravity-ui/uikit';
import { i18n } from "../components/organisms/ToolMessage/i18n/index.js";
import { useAutoCollapseOnCancelled } from "./useAutoCollapseOnCancelled.js";
import { useAutoCollapseOnSuccess } from "./useAutoCollapseOnSuccess.js";
function getDefaultFooterActions(status, footerActions, onAccept, onReject) {
    if (footerActions !== undefined) {
        return footerActions;
    }
    switch (status) {
        case 'waitingConfirmation':
            return [
                {
                    label: i18n('action-reject'),
                    onClick: onReject,
                    view: 'outlined',
                },
                {
                    label: i18n('action-accept'),
                    onClick: onAccept,
                    view: 'action',
                },
            ];
        case 'waitingSubmission':
            return [
                {
                    label: i18n('action-cancel'),
                    onClick: onReject,
                    view: 'outlined',
                },
            ];
        default:
            return [];
    }
}
function getDefaultFooterMessage(status, footerContent) {
    if (footerContent) {
        return footerContent;
    }
    switch (status) {
        case 'waitingConfirmation':
            return i18n('status-waiting-confirmation');
        case 'waitingSubmission':
            return 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 = [];
export function useToolMessage(options) {
    const { footerActions, headerActions = defaultHeaderActions, bodyContent, status, footerContent, expandable = Boolean(bodyContent), initialExpanded, autoCollapseOnSuccess, autoCollapseOnCancelled, onAccept, onReject, } = options;
    const [isExpanded, setIsExpanded] = useState(getDefaultInitialExpanded(status, initialExpanded));
    useAutoCollapseOnSuccess({ enabled: Boolean(autoCollapseOnSuccess), status, setIsExpanded });
    useAutoCollapseOnCancelled({ enabled: Boolean(autoCollapseOnCancelled), status, setIsExpanded });
    const toggleExpanded = useCallback(() => {
        setIsExpanded((prev) => !prev);
    }, []);
    const expandCollapseAction = useMemo(() => {
        if (!expandable) {
            return null;
        }
        return {
            label: isExpanded ? i18n('action-collapse') : i18n('action-expand'),
            onClick: toggleExpanded,
            icon: _jsx(Icon, { data: isExpanded ? ChevronUp : ChevronDown, size: 16 }),
        };
    }, [expandable, isExpanded, toggleExpanded]);
    const finalHeaderActions = useMemo(() => {
        return expandCollapseAction ? [...headerActions, expandCollapseAction] : headerActions;
    }, [headerActions, expandCollapseAction]);
    const finalFooterActions = 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
