import { string } from 'yup';
import { buildYupChain } from '@/UI-kit/utils';
import {
    birthDateValidator,
    correctDateValidator,
    requiredValueValidator,
} from '@/modules/documents/constants/validation/common.validation';
import { uiKitI18n } from '@/configs';

export const correctPassportSeriesValidator = () => ({
    name: 'passport-series',
    message: uiKitI18n?.global.t('helpers.validation.error'),
    test: (value: string) => !value.length || value.length > 1,
});

export const nameValidator = () => ({
    name: 'name',
    message: uiKitI18n?.global.t('helpers.validation.error'),
    test: (value: string) => /^[А-Яа-яЁё\s-]+$/.test(value) || !value.length,
});

export const nameLengthValidator = () => ({
    name: 'name-length',
    message: uiKitI18n?.global.t('helpers.validation.error'),
    test: (value: string) => !value.length || (value.length >= 1 && value.length <= 50),
});

export const getPassportValidator = () => ({
    passportSeries: buildYupChain(string(), [correctPassportSeriesValidator()]),
    passportNumber: buildYupChain(string(), [
        requiredValueValidator(),
        correctPassportSeriesValidator(),
    ]),
    issueDate: buildYupChain(string(), [
        requiredValueValidator(),
        correctDateValidator(),
        birthDateValidator(),
    ]),
    lastName: buildYupChain(string(), [
        requiredValueValidator(),
        nameValidator(),
        nameLengthValidator(),
    ]),
    firstName: buildYupChain(string(), [
        requiredValueValidator(),
        nameValidator(),
        nameLengthValidator(),
    ]),
    middleName: buildYupChain(string(), [nameValidator(), nameLengthValidator()]),
    birthDate: buildYupChain(string(), [
        requiredValueValidator(),
        correctDateValidator(),
        birthDateValidator(),
    ]),
    gender: buildYupChain(string(), [requiredValueValidator()]),
    citizenship: buildYupChain(string(), [requiredValueValidator()]),
    regionId: buildYupChain(string(), [requiredValueValidator()]),
    issuingCountry: buildYupChain(string(), [requiredValueValidator()]),
});
