import React from 'react';
import { Status } from '@theorchard/suite-components';
import { WORKSHEET_PAYMENT_ACTTION_STATUS_MAP } from 'src/apollo/type-constants/advance';
import { getSendPayments } from 'src/utils/payment-overview';
import type { WorksheetPaymentContractAdvancesQuery } from 'src/apollo/queries/payment-group-payment/__generated__/worksheet-payment-contract-advances';
import { ABACUS_ACTION_STATUSES } from '@theorchard/accounting-apps-shared';

type ActionStates =
    WorksheetPaymentContractAdvancesQuery['worksheetPaymentContractAdvances']['items'][0]['actionStates'][0];

export const paymentStatus = (actionStates: ActionStates[]) => {
    const sendPayments = getSendPayments(actionStates);

    if (!sendPayments) return <></>;

    const statusText = String(
        WORKSHEET_PAYMENT_ACTTION_STATUS_MAP[sendPayments.actionStatus]
    );
    const statusVariant = (payment: string) => {
        switch (payment) {
            case ABACUS_ACTION_STATUSES.INIT:
                return 'neutral';
            case ABACUS_ACTION_STATUSES.RUNNING:
                return 'warning';
            case ABACUS_ACTION_STATUSES.ERROR:
                return 'error';
            case ABACUS_ACTION_STATUSES.REJECTED:
                return 'error';
            case ABACUS_ACTION_STATUSES.COMPLETE:
                return 'success';
            default:
                return 'neutral';
        }
    };
    return (
        <Status
            text={statusText}
            filled
            variant={statusVariant(sendPayments.actionStatus)}
        />
    );
};
