"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const common_1 = require("../../common");
const DiskEncryptionService_1 = tslib_1.__importDefault(require("./DiskEncryptionService"));
const types_1 = require("./types");
const dummyData_1 = require("../components/clusterConfiguration/staticIp/data/dummyData");
const api_1 = require("../../common/api");
const CpuArchitectureService_1 = require("./CpuArchitectureService");
const toNumber_1 = tslib_1.__importDefault(require("lodash-es/toNumber"));
const getNewClusterCpuArchitecture = (urlSearchParams) => {
    const params = new URLSearchParams(urlSearchParams);
    const hasArmSearchParam = params.get('useArm') === 'true';
    return hasArmSearchParam ? common_1.CpuArchitecture.ARM : (0, CpuArchitectureService_1.getDefaultCpuArchitecture)();
};
const getExistingClusterCpuArchitecture = (infraEnv) => {
    return infraEnv.cpuArchitecture || (0, CpuArchitectureService_1.getDefaultCpuArchitecture)();
};
const ntpSourceForCluster = (cluster, infraEnv) => {
    var _a, _b;
    const fromCluster = (_a = cluster === null || cluster === void 0 ? void 0 : cluster.additionalNtpSource) === null || _a === void 0 ? void 0 : _a.trim();
    const fromInfra = (_b = infraEnv === null || infraEnv === void 0 ? void 0 : infraEnv.additionalNtpSources) === null || _b === void 0 ? void 0 : _b.trim();
    const merged = fromCluster || fromInfra;
    return merged || undefined;
};
const ClusterDetailsService = {
    getClusterCreateParams(values, infraEnv) {
        var _a;
        const params = {
            name: values.name,
            controlPlaneCount: (0, toNumber_1.default)(values.controlPlaneCount),
            openshiftVersion: values.openshiftVersion,
            pullSecret: values.pullSecret,
            baseDnsDomain: values.baseDnsDomain,
            cpuArchitecture: values.cpuArchitecture,
            diskEncryption: DiskEncryptionService_1.default.getDiskEncryptionParams(values),
            platform: Object.assign({ type: values.platform }, (values.platform === 'external'
                ? {
                    external: {
                        platformName: 'oci',
                        cloudControllerManager: 'External',
                    },
                }
                : {})),
        };
        if (!values.isCMNSupported) {
            params.userManagedNetworking = true;
        }
        if (values.hostsNetworkConfigurationType === types_1.HostsNetworkConfigurationType.STATIC) {
            params.staticNetworkConfig = (0, dummyData_1.getDummyInfraEnvField)();
        }
        if (api_1.isInOcm) {
            params.tags = common_1.AI_UI_TAG;
        }
        if (((_a = params.platform) === null || _a === void 0 ? void 0 : _a.type) === 'none') {
            delete params.platform;
        }
        const ntp = ntpSourceForCluster(undefined, infraEnv);
        if (ntp) {
            params.additionalNtpSource = ntp;
        }
        return params;
    },
    getClusterUpdateParams(values, platform, cluster, infraEnv) {
        const params = {
            name: values.name,
            baseDnsDomain: values.baseDnsDomain,
        };
        if (values.pullSecret) {
            params.pullSecret = values.pullSecret;
        }
        if (platform) {
            params.platform =
                platform === 'external'
                    ? {
                        type: platform,
                        external: { platformName: 'oci', cloudControllerManager: 'External' },
                    }
                    : { type: platform };
        }
        const ntp = ntpSourceForCluster(cluster, infraEnv);
        if (ntp) {
            params.additionalNtpSource = ntp;
        }
        return params;
    },
    getClusterDetailsInitialValues(_a) {
        var { cluster, infraEnv, urlSearchParams, isSingleClusterFeatureEnabled } = _a, args = tslib_1.__rest(_a, ["cluster", "infraEnv", "urlSearchParams", "isSingleClusterFeatureEnabled"]);
        const values = (0, common_1.getClusterDetailsInitialValues)(Object.assign({ cluster }, args));
        const cpuArchitecture = infraEnv
            ? getExistingClusterCpuArchitecture(infraEnv)
            : getNewClusterCpuArchitecture(urlSearchParams);
        const hostsNetworkConfigurationType = (infraEnv === null || infraEnv === void 0 ? void 0 : infraEnv.staticNetworkConfig)
            ? types_1.HostsNetworkConfigurationType.STATIC
            : types_1.HostsNetworkConfigurationType.DHCP;
        const platform = isSingleClusterFeatureEnabled && !cluster ? 'none' : values.platform;
        return Object.assign(Object.assign({}, values), { platform,
            cpuArchitecture,
            hostsNetworkConfigurationType, isCMNSupported: true, isSNODevPreview: false });
    },
};
exports.default = ClusterDetailsService;
//# sourceMappingURL=ClusterDetailsService.js.map