import React from 'react';
import { Field, Form, Select } from '@theorchard/suite-components';
import { useGetReferenceFlowthroughCalculations } from 'src/apollo/queries/reference-flowthrough-calculation';
import type { ReferenceFlowthroughCalculation } from 'src/types/reference-flowthrough-calculation-option';

export interface ContractFlowthroughCalculationProps {
    abacusContractFlowthroughCalculationId: string | undefined;
    errors: any;
    serverErrors: any;
    setServerErrors: (params: any) => void;
    handleFormChange: (
        // eslint-disable-next-line no-unused-vars
        formField: string,
        // eslint-disable-next-line no-unused-vars
        inputValue: string | null | undefined
    ) => void;
}
export const ContractFlowthroughCalculation: React.FC<
    ContractFlowthroughCalculationProps
> = ({
    abacusContractFlowthroughCalculationId,
    errors,
    handleFormChange,
    setServerErrors,
}) => {
    const { data: referenceFlowthroughCalculationTypes, error: serverError } =
        useGetReferenceFlowthroughCalculations();

    if (serverError) {
        const stringifiedError =
            typeof serverError === 'string'
                ? serverError
                : JSON.stringify(serverError);
        setServerErrors(stringifiedError);
    }

    if (abacusContractFlowthroughCalculationId) {
        abacusContractFlowthroughCalculationId =
            abacusContractFlowthroughCalculationId || undefined;
    }

    const referenceFlowthroughCalculationTypesOptions: ReferenceFlowthroughCalculation[] =
        referenceFlowthroughCalculationTypes?.abacusReferenceFlowthroughCalculations.items.map(
            item => ({
                label: item.flowthroughCalculation || '',
                value: item.referenceFlowthroughCalculationId || '',
                example: item.flowthroughCalculationExample || '',
                exampleSummary: item.flowthroughCalculationExampleSummary || '',
            })
        ) || [];

    const fetchedCalculation = referenceFlowthroughCalculationTypesOptions.find(
        item => item.value === abacusContractFlowthroughCalculationId
    );

    const calculation = fetchedCalculation;
    const fetchedCalculationOption:
        | ReferenceFlowthroughCalculation
        | undefined = calculation
        ? {
              label: calculation.label || '',
              value: calculation.value || '',
              example: calculation.example || '',
              exampleSummary: calculation.exampleSummary || '',
          }
        : undefined;

    const selectedReferenceFlowthroughCalculation:
        | ReferenceFlowthroughCalculation
        | undefined =
        fetchedCalculationOption ||
        referenceFlowthroughCalculationTypesOptions.find(
            option =>
                option.value.toString() ===
                abacusContractFlowthroughCalculationId?.toString()
        );

    return (
        <>
            <Form.Group>
                <Field
                    controlId="flowthroughCalculation"
                    labelText="Calculation"
                    errorMessage={
                        errors?.referenceFlowthroughCalculation || undefined
                    }
                >
                    <Select
                        className="ContractFlowthroughFormFields-Calculation"
                        name="calculation"
                        hideClearButton={true}
                        placeholder="Select Calculation"
                        options={referenceFlowthroughCalculationTypesOptions}
                        testId="contractFlowthroughCalculation"
                        menuWidth="100%"
                        selectedValue={
                            selectedReferenceFlowthroughCalculation || undefined
                        }
                        onChange={(
                            selectedOption:
                                | ReferenceFlowthroughCalculation
                                | undefined
                        ) => {
                            handleFormChange(
                                'referenceFlowthroughCalculationId',
                                selectedOption?.value
                            );
                        }}
                    />

                    {selectedReferenceFlowthroughCalculation?.example && (
                        <Form.Text className="ContractFlowthroughFormFields-Calculation-Example">
                            {selectedReferenceFlowthroughCalculation.example}
                        </Form.Text>
                    )}
                    {selectedReferenceFlowthroughCalculation?.exampleSummary && (
                        <Form.Text
                            className="ContractFlowthroughFormFields-Calculation-Summary"
                            muted
                        >
                            {
                                selectedReferenceFlowthroughCalculation.exampleSummary
                            }
                        </Form.Text>
                    )}
                </Field>
            </Form.Group>
        </>
    );
};
