"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getNetworkType = exports.getCurrentClusterVersion = exports.getSelectedVersion = exports.getOCPVersions = exports.isValidImageSet = exports.getVersionFromClusterImageSet = exports.getVersionFromReleaseString = void 0;
const tslib_1 = require("tslib");
const uniqBy_js_1 = tslib_1.__importDefault(require("lodash-es/uniqBy.js"));
const common_1 = require("../../../common");
const utils_1 = require("../../../common/utils");
const getVersionFromReleaseString = (value = '') => {
    const match = /(\d+\.\d+(?:\.\d+)?(?:-\w+)?)/gm.exec(value);
    if (match && match.length > 1 && match[1]) {
        return match[1];
    }
};
exports.getVersionFromReleaseString = getVersionFromReleaseString;
const getVersionFromClusterImageSet = (clusterImageSet) => {
    var _a, _b, _c, _d;
    return ((0, exports.getVersionFromReleaseString)((_b = (_a = clusterImageSet === null || clusterImageSet === void 0 ? void 0 : clusterImageSet.metadata) === null || _a === void 0 ? void 0 : _a.labels) === null || _b === void 0 ? void 0 : _b['releaseTag']) ||
        (0, exports.getVersionFromReleaseString)((_c = clusterImageSet === null || clusterImageSet === void 0 ? void 0 : clusterImageSet.spec) === null || _c === void 0 ? void 0 : _c.releaseImage) ||
        ((_d = clusterImageSet === null || clusterImageSet === void 0 ? void 0 : clusterImageSet.metadata) === null || _d === void 0 ? void 0 : _d.name) ||
        '');
};
exports.getVersionFromClusterImageSet = getVersionFromClusterImageSet;
const getCPUArchFromReleaseString = (value = '') => {
    const match = /(?:\d+\.)+\d+-(x86-64|[\w_]+)/gm.exec(value);
    if (match && match.length > 1 && match[1]) {
        return match[1];
    }
};
const getCPUArchFromClusterImageSet = (clusterImageSet) => {
    var _a, _b, _c, _d, _e, _f;
    const cpuArch = ((_b = (_a = clusterImageSet === null || clusterImageSet === void 0 ? void 0 : clusterImageSet.metadata) === null || _a === void 0 ? void 0 : _a.labels) === null || _b === void 0 ? void 0 : _b.architecture) ||
        getCPUArchFromReleaseString((_d = (_c = clusterImageSet === null || clusterImageSet === void 0 ? void 0 : clusterImageSet.metadata) === null || _c === void 0 ? void 0 : _c.labels) === null || _d === void 0 ? void 0 : _d['releaseTag']) ||
        getCPUArchFromReleaseString((_e = clusterImageSet === null || clusterImageSet === void 0 ? void 0 : clusterImageSet.spec) === null || _e === void 0 ? void 0 : _e.releaseImage) ||
        getCPUArchFromReleaseString((_f = clusterImageSet === null || clusterImageSet === void 0 ? void 0 : clusterImageSet.metadata) === null || _f === void 0 ? void 0 : _f.name) ||
        '';
    switch (cpuArch) {
        case common_1.CpuArchitecture.x86:
        case common_1.CpuArchitecture.ARM:
        case common_1.CpuArchitecture.s390x:
        case common_1.CpuArchitecture.MULTI:
            return cpuArch;
        case 'x86-64':
            return common_1.CpuArchitecture.x86;
        default:
            return undefined;
    }
};
const isValidImageSet = (cis, architectures, extended) => {
    var _a, _b;
    if (!extended && ((_b = (_a = cis.metadata) === null || _a === void 0 ? void 0 : _a.labels) === null || _b === void 0 ? void 0 : _b.visible) !== 'true') {
        return false;
    }
    if (!architectures) {
        return true;
    }
    return architectures.some((arch) => { var _a, _b, _c; return ((_a = cis.spec) === null || _a === void 0 ? void 0 : _a.releaseImage.endsWith(arch)) || ((_c = (_b = cis.metadata) === null || _b === void 0 ? void 0 : _b.labels) === null || _c === void 0 ? void 0 : _c.architecture) === arch; });
};
exports.isValidImageSet = isValidImageSet;
// Extracts major.minor version, strips any non-numeric prefix (e.g., "img4.18.55" → "4.18")
const extractMajorMinor = (version) => {
    const versionWithoutPrefix = version.replace(/^[^\d]*/, '');
    return versionWithoutPrefix.split('.').slice(0, 2).join('.');
};
// Converts a ClusterImageSet to an OpenshiftVersionOptionType
const toVersionOption = (clusterImageSet) => {
    var _a;
    const version = (0, exports.getVersionFromClusterImageSet)(clusterImageSet);
    const cpuArch = getCPUArchFromClusterImageSet(clusterImageSet);
    return {
        label: `OpenShift ${version}`,
        version,
        value: (_a = clusterImageSet.metadata) === null || _a === void 0 ? void 0 : _a.name,
        default: false,
        supportLevel: 'production', // ACM doesn't have feature support levels
        cpuArchitectures: cpuArch ? [cpuArch] : undefined,
    };
};
/**
 * In case there are osImages specified on the AgentServiceConfig, we want to limit
 * the availability in the dropdown to only versions that are listed there.
 * Otherwise the installation fails.
 */
const hasMatchingOsImage = (version, osImageVersions) => {
    if (!osImageVersions.length)
        return true;
    return osImageVersions.includes(extractMajorMinor(version.version));
};
const supportedNutanixPlatforms = ['x86_64', 'x86-64'];
const getOCPVersions = (clusterImageSets, isNutanix, osImages, extended) => {
    const architectures = isNutanix ? supportedNutanixPlatforms : undefined;
    const osImageVersions = [...new Set(osImages === null || osImages === void 0 ? void 0 : osImages.map((img) => img.openshiftVersion))];
    const versions = clusterImageSets
        .filter((clusterImageSet) => (0, exports.isValidImageSet)(clusterImageSet, architectures, extended))
        .map(toVersionOption)
        .filter((v) => hasMatchingOsImage(v, osImageVersions))
        .sort((a, b) => b.label.localeCompare(a.label));
    // Mark first version as the default selection
    versions[0] && (versions[0].default = true);
    return (0, uniqBy_js_1.default)(versions, (v) => v.version);
};
exports.getOCPVersions = getOCPVersions;
const getSelectedVersion = (clusterImages, agentClusterInstall) => {
    var _a, _b, _c, _d;
    const selectedClusterImage = clusterImages.find((ci) => { var _a, _b, _c; return ((_a = ci.metadata) === null || _a === void 0 ? void 0 : _a.name) === ((_c = (_b = agentClusterInstall === null || agentClusterInstall === void 0 ? void 0 : agentClusterInstall.spec) === null || _b === void 0 ? void 0 : _b.imageSetRef) === null || _c === void 0 ? void 0 : _c.name); });
    return selectedClusterImage
        ? (_b = (_a = (0, exports.getOCPVersions)([selectedClusterImage], undefined, undefined, true)) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.version
        : (_d = (_c = agentClusterInstall === null || agentClusterInstall === void 0 ? void 0 : agentClusterInstall.spec) === null || _c === void 0 ? void 0 : _c.imageSetRef) === null || _d === void 0 ? void 0 : _d.name;
};
exports.getSelectedVersion = getSelectedVersion;
const getCurrentClusterVersion = (cv) => { var _a, _b, _c, _d, _e; return ((_c = (_b = (_a = cv === null || cv === void 0 ? void 0 : cv.status) === null || _a === void 0 ? void 0 : _a.history) === null || _b === void 0 ? void 0 : _b[0]) === null || _c === void 0 ? void 0 : _c.version) || ((_e = (_d = cv === null || cv === void 0 ? void 0 : cv.spec) === null || _d === void 0 ? void 0 : _d.desiredUpdate) === null || _e === void 0 ? void 0 : _e.version); };
exports.getCurrentClusterVersion = getCurrentClusterVersion;
const getNetworkType = (ocpVersion) => {
    const checkVersionNum = (0, utils_1.getComparableVersionInt)((ocpVersion === null || ocpVersion === void 0 ? void 0 : ocpVersion.version) || '');
    const toVersionNum = (0, utils_1.getComparableVersionInt)('4.12');
    return !checkVersionNum || checkVersionNum >= toVersionNum ? 'OVNKubernetes' : 'OpenShiftSDN';
};
exports.getNetworkType = getNetworkType;
//# sourceMappingURL=versions.js.map