"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getFormViewManifestsValidationSchema = void 0;
const tslib_1 = require("tslib");
const Yup = tslib_1.__importStar(require("yup"));
const utils_1 = require("../../../../../common/utils");
const common_1 = require("../../../../../common");
/* eslint-enable */
Yup.addMethod(Yup.array, 'uniqueManifestFiles', function (message, mapper) {
    return this.test('unique', message, function (list) {
        if (!list)
            return true;
        const seen = new Set();
        const errors = [];
        list.forEach((item, index) => {
            const mappedValue = mapper(item);
            if (seen.has(mappedValue)) {
                errors.push(this.createError({
                    path: `${this.path}[${index}].filename`,
                    message,
                }));
            }
            seen.add(mappedValue);
        });
        if (errors.length > 0) {
            throw new Yup.ValidationError(errors);
        }
        return true;
    });
});
const INCORRECT_FILENAME = 'Must have a yaml, yml, json, yaml.patch or yml.patch extension and can not contain /.';
const UNIQUE_FOLDER_FILENAME = 'Ensure unique file names to avoid conflicts and errors.';
const getFormViewManifestsValidationSchema = (t) => Yup.object({
    manifests: Yup.array()
        .of(Yup.object({
        folder: Yup.mixed().required('Required'),
        filename: Yup.string()
            .required('Required')
            .min(1, 'Number of characters must be 1-255')
            .max(255, 'Number of characters must be 1-255')
            .test('not-correct-filename', INCORRECT_FILENAME, (value) => {
            return (0, utils_1.validateFileName)(value);
        }),
        manifestYaml: Yup.string().when('filename', {
            is: (filename) => !filename.includes('patch'),
            then: () => Yup.string()
                .required('Required')
                .test('not-big-file', (0, utils_1.getMaxFileSizeMessage)(t), utils_1.validateFileSize)
                .test('not-valid-file', (0, common_1.getIncorrectFileTypeMessage)(t), utils_1.validateFileType),
            otherwise: () => Yup.string()
                .required('Required')
                .test('not-big-file', (0, utils_1.getMaxFileSizeMessage)(t), utils_1.validateFileSize), // Validation of file content is not required if filename contains 'patch'
        }),
    }))
        .uniqueManifestFiles(UNIQUE_FOLDER_FILENAME, (val) => val.filename),
});
exports.getFormViewManifestsValidationSchema = getFormViewManifestsValidationSchema;
//# sourceMappingURL=customManifestsValidationSchema.js.map