"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getClusterDetailsValidationSchema = exports.getClusterDetailsInitialValues = exports.parseTangServers = void 0;
const tslib_1 = require("tslib");
const Yup = tslib_1.__importStar(require("yup"));
const types_1 = require("../../types");
const ui_1 = require("../ui");
const validationSchemas_1 = require("../../validationSchemas");
const emptyTangServers = () => {
    return [
        {
            url: '',
            thumbprint: '',
        },
    ];
};
const parseTangServers = (tangServersString) => {
    if (!tangServersString) {
        return emptyTangServers();
    }
    try {
        return JSON.parse(tangServersString);
    }
    catch (e) {
        // console.warn('Tang Servers can not be parsed');
    }
    return emptyTangServers();
};
exports.parseTangServers = parseTangServers;
const getClusterDetailsInitialValues = ({ cluster, pullSecret, managedDomains, ocpVersions, baseDomain, }) => {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
    const { name = '', baseDnsDomain = baseDomain || '', openshiftVersion = (0, ui_1.getDefaultOpenShiftVersion)(ocpVersions), controlPlaneCount = 3, } = cluster || {};
    return {
        name,
        openshiftVersion,
        pullSecret: pullSecret || '',
        baseDnsDomain,
        controlPlaneCount,
        useRedHatDnsService: !!baseDnsDomain && managedDomains.map((d) => d.domain).includes(baseDnsDomain),
        enableDiskEncryptionOnMasters: ['all', 'masters'].includes((_b = (_a = cluster === null || cluster === void 0 ? void 0 : cluster.diskEncryption) === null || _a === void 0 ? void 0 : _a.enableOn) !== null && _b !== void 0 ? _b : 'none'),
        enableDiskEncryptionOnWorkers: ['all', 'workers'].includes((_d = (_c = cluster === null || cluster === void 0 ? void 0 : cluster.diskEncryption) === null || _c === void 0 ? void 0 : _c.enableOn) !== null && _d !== void 0 ? _d : 'none'),
        diskEncryptionMode: (_f = (_e = cluster === null || cluster === void 0 ? void 0 : cluster.diskEncryption) === null || _e === void 0 ? void 0 : _e.mode) !== null && _f !== void 0 ? _f : 'tpmv2',
        diskEncryptionTangServers: (0, exports.parseTangServers)((_g = cluster === null || cluster === void 0 ? void 0 : cluster.diskEncryption) === null || _g === void 0 ? void 0 : _g.tangServers),
        diskEncryption: (_h = cluster === null || cluster === void 0 ? void 0 : cluster.diskEncryption) !== null && _h !== void 0 ? _h : {},
        cpuArchitecture: (cluster === null || cluster === void 0 ? void 0 : cluster.cpuArchitecture) || (0, types_1.getDefaultCpuArchitecture)(),
        platform: ((_j = cluster === null || cluster === void 0 ? void 0 : cluster.platform) === null || _j === void 0 ? void 0 : _j.type) || 'baremetal',
        customOpenshiftSelect: null,
        userManagedNetworking: (cluster === null || cluster === void 0 ? void 0 : cluster.userManagedNetworking) || false,
        enableDiskEncryptionOnArbiters: ['all', 'arbiters'].includes((_l = (_k = cluster === null || cluster === void 0 ? void 0 : cluster.diskEncryption) === null || _k === void 0 ? void 0 : _k.enableOn) !== null && _l !== void 0 ? _l : 'none'),
    };
};
exports.getClusterDetailsInitialValues = getClusterDetailsInitialValues;
const getClusterDetailsValidationSchema = ({ usedClusterNames, pullSecretSet, validateUniqueName, isOcm, t, }) => Yup.lazy((values) => {
    if (pullSecretSet) {
        return Yup.object({
            name: (0, validationSchemas_1.nameValidationSchema)(t, usedClusterNames, values.baseDnsDomain, validateUniqueName, isOcm),
            baseDnsDomain: isOcm
                ? (0, validationSchemas_1.baseDomainValidationSchema)(t).required(t('ai:Required field'))
                : (0, validationSchemas_1.dnsNameValidationSchema)(t).required(t('ai:Required field')),
        });
    }
    return Yup.object({
        name: (0, validationSchemas_1.nameValidationSchema)(t, usedClusterNames, values.baseDnsDomain, validateUniqueName, isOcm),
        baseDnsDomain: isOcm
            ? (0, validationSchemas_1.baseDomainValidationSchema)(t).required(t('ai:Required field'))
            : (0, validationSchemas_1.dnsNameValidationSchema)(t).required(t('ai:Required field')),
        pullSecret: (0, validationSchemas_1.pullSecretValidationSchema)(t).required(t('ai:Required field')),
        diskEncryptionTangServers: Yup.array().when('diskEncryptionMode', {
            is: (diskEncryptionMode) => {
                return diskEncryptionMode === 'tang';
            },
            then: () => Yup.array().of(Yup.object().shape({
                url: Yup.string()
                    .url(t('ai:Tang Server URL must be a valid URL starting with "http://" or "https://"'))
                    .required(t('ai:Required field')),
                thumbprint: Yup.string().required(t('ai:Required field')),
            })),
        }),
    });
});
exports.getClusterDetailsValidationSchema = getClusterDetailsValidationSchema;
//# sourceMappingURL=clusterDetailsValidation.js.map