import { string } from "yup";
import checkoutBillingStateOptions from "../../../utils/checkout-billing-state-options";
import { CountryCode } from "../../../utils/country-codes";
import { countryValidation } from "../utils/country-config";
import { businessServiceReceivedOptions, bankAccountTypeOptions, } from "../utils/business-form-options";
import { numbersOnlyRegex, phoneRegex, poBoxRegex, ssnRegex, transformEmptyString, urlRegex, validateRoutingNumber, } from "./schema-helpers";
// Common Validations
export const emailValidation = string()
    .email('Enter valid email')
    .transform(transformEmptyString);
export const phoneValidation = string()
    .matches(phoneRegex, 'Enter valid phone number')
    .transform(transformEmptyString);
// Core Info Validations
export const businessNameValidation = string()
    .min(2, 'Name must be at least 2 characters')
    .max(100, 'Name must be less than 100 characters')
    .transform(transformEmptyString);
export const doingBusinessAsValidation = string()
    .min(2, 'Name must be at least 2 characters')
    .max(100, 'Name must be less than 100 characters')
    .transform(transformEmptyString);
export const websiteUrlValidation = string()
    .matches(urlRegex, 'Enter valid website url')
    .transform(transformEmptyString);
export const businessClassificationValidation = string().transform(transformEmptyString);
export const industryValidation = string()
    .min(2, 'Industry must be at least 2 characters')
    .max(50, 'Industry must be less than 50 characters')
    .transform(transformEmptyString);
export const taxIdValidation = string()
    .matches(numbersOnlyRegex, 'Enter valid tax id, SSN, or EIN')
    .test('not-repeat', 'Enter valid tax id, SSN, or EIN', (value) => {
    return !/^(\d)\1+$/.test(value);
})
    .test('not-seq', 'Enter valid tax id, SSN, or EIN', (value) => {
    return value !== '123456789';
})
    .transform(transformEmptyString);
// Country-aware helpers (backward compatible)
export const makePostalValidation = (country) => string()
    .matches(countryValidation[country].postalRegex, 'Enter valid postal code')
    .transform(transformEmptyString);
export const makeIdentityNumberValidation = (country) => {
    if (country === CountryCode.CAN) {
        return string()
            .matches(numbersOnlyRegex, 'Enter valid SIN')
            .length(countryValidation[country].identityDigits, 'Enter valid SIN')
            .test('not-repeat', 'Enter valid SIN', (value) => {
            return !/^(\d)\1+$/.test(value);
        })
            .test('not-seq', 'Enter valid SIN', (value) => {
            return value !== '123456789';
        })
            .transform(transformEmptyString);
    }
    // USA default (SSN)
    return ssnValidation;
};
export const makeTaxIdValidation = (country) => {
    if (country === CountryCode.CAN) {
        return string()
            .matches(numbersOnlyRegex, 'Enter valid Business Number (BN)')
            .length(countryValidation[country].taxIdDigits, 'Enter valid Business Number (BN)')
            .test('not-repeat', 'Enter valid Business Number (BN)', (value) => {
            if (!value)
                return true;
            return !/^(\d)\1+$/.test(value);
        })
            .test('not-seq', 'Enter valid Business Number (BN)', (value) => {
            if (!value)
                return true;
            return value !== '123456789';
        })
            .transform(transformEmptyString);
    }
    // USA default
    return taxIdValidation;
};
export const makeBusinessClassificationValidation = (_country) => string().transform(transformEmptyString);
export const dateOfIncorporationValidation = string()
    .test('past-date', 'Date of registration must be in the past', (value) => {
    const inputDate = new Date(value);
    inputDate.setDate(inputDate.getDate() + 1);
    const today = new Date();
    return inputDate <= today;
})
    .transform(transformEmptyString);
// Identity Validations
export const identityNameValidation = string()
    .min(2, 'Name must be at least 2 characters')
    .max(100, 'Name must be less than 100 characters')
    .transform(transformEmptyString);
export const identityTitleValidation = string()
    .min(2, 'Title must be at least 2 characters')
    .max(50, 'Title must be less than 50 characters')
    .transform(transformEmptyString);
export const dobValidation = (role) => {
    return string()
        .test('min', 'Enter a valid date', (value) => {
        const date = new Date(value);
        const minDate = new Date('1902-01-01');
        return date >= minDate;
    })
        .test('age', `${role} must be at least 18 years old`, (value) => {
        const date = new Date(value);
        const minAgeDate = new Date();
        minAgeDate.setFullYear(minAgeDate.getFullYear() - 18);
        return date <= minAgeDate;
    })
        .transform(transformEmptyString);
};
export const ssnValidation = string()
    .matches(ssnRegex, 'Enter valid SSN')
    .when('ssn_last4', {
    is: (val) => !val || val.length === 0,
    then: (schema) => schema.required('Enter SSN'),
    otherwise: (schema) => schema.nullable(),
})
    .test('not-repeat', 'Enter valid SSN', (value) => {
    return !/^(\d)\1+$/.test(value);
})
    .test('not-seq', 'Enter valid SSN', (value) => {
    return value !== '123456789';
})
    .transform(transformEmptyString);
export const ownershipPercentageValidation = string()
    .matches(numbersOnlyRegex, 'Enter valid percentage')
    .test('min', 'Percentage must be at least 0', (value) => {
    if (!value)
        return true;
    const num = parseFloat(value);
    return num >= 0;
})
    .test('max', 'Percentage must be at most 100', (value) => {
    if (!value)
        return true;
    const num = parseFloat(value);
    return num <= 100;
})
    .transform(transformEmptyString);
// Address Validations
export const lineOneValidation = string()
    .min(5, 'Address must be at least 5 characters')
    .max(100, 'Address must be less than 100 characters')
    .test('not-po-box', 'A PO Box is not a valid address entry', (value) => {
    return !poBoxRegex.test(value);
})
    .transform(transformEmptyString);
export const lineTwoValidation = string()
    .max(100, 'Address must be less than 100 characters')
    .test('not-po-box', 'A PO Box is not a valid address entry', (value) => {
    return !poBoxRegex.test(value);
})
    .transform(transformEmptyString);
export const cityValidation = string()
    .min(2, 'City must be at least 2 characters')
    .max(50, 'City must be less than 50 characters')
    .transform(transformEmptyString);
export const stateValidation = string()
    .oneOf(checkoutBillingStateOptions.map((option) => option.value), 'Select a valid state or province')
    .transform(transformEmptyString);
export const postalValidation = string()
    .matches(/^((\d{5}-\d{4})|(\d{5})|([A-Z]\d[A-Z]\s\d[A-Z]\d))$/, 'Enter valid postal code')
    .transform(transformEmptyString);
// Additional Questions Validations
export const revenueValidation = string()
    .matches(numbersOnlyRegex, 'Enter valid revenue')
    .transform(transformEmptyString);
export const paymentVolumeValidation = string()
    .matches(numbersOnlyRegex, 'Enter valid payment volume')
    .transform(transformEmptyString);
export const whenServiceReceivedValidation = string()
    .oneOf(businessServiceReceivedOptions.map((option) => option.value), 'Select when service is received')
    .transform(transformEmptyString);
export const otherPaymentDetailsValidation = string().transform(transformEmptyString);
// Bank Account Validations
export const bankNameValidation = string()
    .min(2, 'Name must be at least 2 characters')
    .max(50, 'Name must be less than 50 characters')
    .transform(transformEmptyString);
export const nicknameValidation = string()
    .min(2, 'Name must be at least 2 characters')
    .max(50, 'Name must be less than 50 characters')
    .transform(transformEmptyString);
export const accountTypeValidation = string()
    .oneOf(bankAccountTypeOptions.map((option) => option.value), 'Select account type')
    .transform(transformEmptyString);
export const accountNumberValidation = string()
    .min(4, 'Account number must be at least 4 digits')
    .max(17, 'Account number must be less than 17 digits')
    .matches(numbersOnlyRegex, 'Enter valid account number')
    .test('not-repeat', 'Enter valid account number', (value) => {
    return !/^(\d)\1+$/.test(value);
})
    .transform(transformEmptyString);
export const routingNumberValidation = string()
    .length(9, 'Routing number must be 9 digits')
    .matches(numbersOnlyRegex, 'Enter valid routing number')
    .test('not-repeat', 'Enter valid routing number', (value) => {
    if (!value)
        return true;
    return !/^(\d)\1+$/.test(value);
})
    .test('valid', 'Enter valid routing number', (value) => {
    if (!value)
        return true;
    return validateRoutingNumber(value);
})
    .transform(transformEmptyString);
