import { isNonEmptyString } from "@insite/client-framework/Common/Utilities/StringUtilities";
import translate from "@insite/client-framework/Translate";
import { PaymentDetailsCartState } from "@insite/content-library/Hooks/Payments/usePaymentDetailsCartState";
import { UsePaymentDetailsFormState } from "@insite/content-library/Hooks/Payments/usePaymentDetailsFormState";

export interface UsePaymentDetailsFormValidation {
    validatePaymentMethod: (paymentMethod: string) => boolean;
    validatePONumber: (poNumber: string) => boolean;
    validateAddress1: (address1: string) => boolean;
    validateCountry: (countryId: string) => boolean;
    validateState: (stateId: string) => boolean;
    validateCity: (city: string) => boolean;
    validatePostalCode: (postalCode: string) => boolean;
}

function usePaymentDetailsFormValidation(
    paymentDetailsFormState: UsePaymentDetailsFormState,
    paymentDetailsCartState: PaymentDetailsCartState,
): UsePaymentDetailsFormValidation {
    const { cart, paymentMethods, isCreditCard, isECheck, selectedCountry } = paymentDetailsCartState;
    const { useBillingAddress, isPayPal, externalTokenProvider } = paymentDetailsFormState;
    const validatePaymentMethod = (paymentMethod: string) => {
        const paymentMethodValid =
            isPayPal || !paymentMethods || paymentMethods.length === 0 || isNonEmptyString(paymentMethod);
        paymentDetailsFormState.setPaymentMethodError(
            !paymentMethodValid ? translate("Payment Method is required.") : "",
        );
        return paymentMethodValid;
    };

    const validatePONumber = (poNumber: string) => {
        const poNumberValid = !cart || !cart.showPoNumber || !cart.requiresPoNumber || isNonEmptyString(poNumber);
        paymentDetailsFormState.setPONumberError(!poNumberValid ? translate("PO Number is required.") : "");
        return poNumberValid;
    };

    const validateAddress1 = (address1: string) => {
        const address1Valid =
            !(isCreditCard || isECheck || externalTokenProvider) ||
            useBillingAddress ||
            (!useBillingAddress && isNonEmptyString(address1));
        paymentDetailsFormState.setAddress1Error(!address1Valid ? translate("Address is required.") : "");
        return address1Valid;
    };

    const validateCountry = (countryId: string) => {
        const countryValid =
            !(isCreditCard || isECheck || externalTokenProvider) ||
            useBillingAddress ||
            (!useBillingAddress && isNonEmptyString(countryId));
        paymentDetailsFormState.setCountryError(!countryValid ? translate("Country is required.") : "");
        return countryValid;
    };

    const validateState = (stateId: string) => {
        const stateValid =
            (!!selectedCountry && !!selectedCountry.states && selectedCountry.states.length <= 0) ||
            !(isCreditCard || isECheck) ||
            useBillingAddress ||
            (!useBillingAddress && isNonEmptyString(stateId));
        paymentDetailsFormState.setStateError(!stateValid ? translate("State is required.") : "");
        return stateValid;
    };

    const validateCity = (city: string) => {
        const cityValid =
            !(isCreditCard || isECheck || externalTokenProvider) ||
            useBillingAddress ||
            (!useBillingAddress && isNonEmptyString(city));
        paymentDetailsFormState.setCityError(!cityValid ? translate("City is required.") : "");
        return cityValid;
    };

    const validatePostalCode = (postalCode: string) => {
        const postalCodeValid =
            !(isCreditCard || isECheck || externalTokenProvider) ||
            useBillingAddress ||
            (!useBillingAddress && isNonEmptyString(postalCode));
        paymentDetailsFormState.setPostalCodeError(!postalCodeValid ? translate("Postal Code is required.") : "");
        return postalCodeValid;
    };

    return {
        validatePaymentMethod,
        validatePONumber,
        validateAddress1,
        validateCountry,
        validateState,
        validateCity,
        validatePostalCode,
    };
}

export default usePaymentDetailsFormValidation;
