import { ERROR_MESSAGE, usTaxFormValidation } from '../us-tax-form-validation';
import type {
    USTaxFormInput,
    USTaxFormW8BENEInput,
    USTaxFormW9Input,
} from 'src/apollo/definitions/globalTypes';

describe('usTaxFormValidation', () => {
    test('validates required fields for USTaxFormW8BENEInput', () => {
        const formData: USTaxFormInput = {
            USTaxFormW8BENEInput: {
                taxName: '',
                countryOfTaxResidence: '',
                taxIdCountry: '',
                signedDate: '',
                typeOfEntity: '',
                taxTreatyClaim: true,
                lob: '',
            } as USTaxFormW8BENEInput,
        };

        const errors = usTaxFormValidation(formData);

        expect(errors).toEqual({
            taxName: ERROR_MESSAGE.TAX_NAME,
            countryOfTaxResidence: ERROR_MESSAGE.COUNTRY_OF_TAX_RESIDENCE,
            taxIdCountry: ERROR_MESSAGE.TAX_ID_COUNTRY,
            signedDate: ERROR_MESSAGE.SIGNED_DATE,
            typeOfEntity: ERROR_MESSAGE.TYPE_OF_ENTITY,
            lob: ERROR_MESSAGE.LOB,
        });
    });

    test('validates LOB when taxTreatyClaim is true and lob is missing', () => {
        const formData: USTaxFormInput = {
            USTaxFormW8BENEInput: {
                taxName: 'Valid Name',
                countryOfTaxResidence: 'Valid Country',
                taxIdCountry: 'Valid Country',
                signedDate: '2024-01-01',
                typeOfEntity: 'Corporation',
                taxTreatyClaim: true,
                lob: null,
            } as USTaxFormW8BENEInput,
        };

        const errors = usTaxFormValidation(formData);

        expect(errors.lob).toBe(ERROR_MESSAGE.LOB);
    });

    test('does not add LOB error when taxTreatyClaim is false', () => {
        const formData: USTaxFormInput = {
            USTaxFormW8BENEInput: {
                taxName: 'Valid Name',
                countryOfTaxResidence: 'Valid Country',
                taxIdCountry: 'Valid Country',
                signedDate: '2024-01-01',
                typeOfEntity: 'Corporation',
                taxTreatyClaim: false,
                lob: '', // LOB is empty but taxTreatyClaim is false
            } as USTaxFormW8BENEInput,
        };

        const errors = usTaxFormValidation(formData);

        expect(errors.lob).toBeUndefined();
    });

    test('validates required fields for USTaxFormW9Input', () => {
        const formData: USTaxFormInput = {
            USTaxFormW9Input: {
                taxName: '',
                countryOfTaxResidence: '',
                taxIdCountry: '',
                taxClassification: '',
            } as USTaxFormW9Input,
        };

        const errors = usTaxFormValidation(formData);

        expect(errors).toEqual({
            taxName: ERROR_MESSAGE.TAX_NAME,
            countryOfTaxResidence: ERROR_MESSAGE.COUNTRY_OF_TAX_RESIDENCE,
            taxIdCountry: ERROR_MESSAGE.TAX_ID_COUNTRY,
            taxClassification: ERROR_MESSAGE.TAX_CLASSIFICATION,
            tin: ERROR_MESSAGE.TIN,
            tinType: ERROR_MESSAGE.TIN_TYPE,
        });

        expect(errors.signedDate).toBeUndefined(); // signedDate error should be deleted for W9
    });

    test('returns no errors for valid input', () => {
        const formData: USTaxFormInput = {
            USTaxFormW8BENEInput: {
                taxName: 'Valid Name',
                countryOfTaxResidence: 'Valid Country',
                taxIdCountry: 'Valid Country',
                signedDate: '2024-01-01',
                typeOfEntity: 'Corporation',
                taxTreatyClaim: false,
                lob: 'Some LOB',
            } as USTaxFormW8BENEInput,
        };

        const errors = usTaxFormValidation(formData);

        expect(errors).toEqual({});
    });
});
