"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const omit_js_1 = tslib_1.__importDefault(require("lodash-es/omit.js"));
const ClustersAPI_1 = tslib_1.__importDefault(require("../../common/api/assisted-service/ClustersAPI"));
const HostsService_1 = tslib_1.__importDefault(require("./HostsService"));
const InfraEnvsService_1 = tslib_1.__importDefault(require("./InfraEnvsService"));
const constants_1 = require("../../common/config/constants");
const axiosClient_1 = require("../../common/api/axiosClient");
const ClustersService = {
    findHost(hosts = [], hostId) {
        return hosts.find((host) => host.id === hostId);
    },
    create(params, isAssistedMigration, isSingleClusterFeatureEnabled) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            var _a, _b;
            if (isAssistedMigration) {
                params.tags = constants_1.AI_ASSISTED_MIGRATION_TAG;
            }
            const { data: cluster } = yield ClustersAPI_1.default.register((0, omit_js_1.default)(params, 'staticNetworkConfig'));
            const infraEnvCreateParams = {
                name: `${params.name}_infra-env`,
                pullSecret: params.pullSecret,
                clusterId: cluster.id,
                openshiftVersion: params.openshiftVersion,
                cpuArchitecture: params.cpuArchitecture,
                staticNetworkConfig: params.staticNetworkConfig,
            };
            if (((_a = params.platform) === null || _a === void 0 ? void 0 : _a.type) === 'external' && ((_b = params.platform.external) === null || _b === void 0 ? void 0 : _b.platformName) === 'oci') {
                infraEnvCreateParams.imageType = 'minimal-iso';
            }
            if (!isSingleClusterFeatureEnabled) {
                yield InfraEnvsService_1.default.create(infraEnvCreateParams);
            }
            return cluster;
        });
    },
    remove(clusterId) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            var _a;
            const { data: cluster } = yield ClustersAPI_1.default.get(clusterId);
            const hosts = (_a = cluster.hosts) !== null && _a !== void 0 ? _a : [];
            if (hosts.length > 0) {
                yield HostsService_1.default.removeAll(hosts);
            }
            yield InfraEnvsService_1.default.removeAll(clusterId);
            yield ClustersAPI_1.default.deregister(clusterId);
        });
    },
    downloadLogs(clusterId, hostId) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            var _a;
            const { data, headers } = yield ClustersAPI_1.default.downloadLogs(clusterId, hostId);
            const contentHeader = headers['content-disposition'];
            const fileName = (_a = contentHeader === null || contentHeader === void 0 ? void 0 : contentHeader.match(/filename="(.+)"/)) === null || _a === void 0 ? void 0 : _a[1];
            return { data, fileName };
        });
    },
    update(clusterId, clusterTags, params) {
        ClustersAPI_1.default.abortLastGetRequest();
        if (axiosClient_1.isInOcm) {
            params = ClustersService.updateClusterTags(clusterTags, params);
        }
        return ClustersAPI_1.default.update(clusterId, params);
    },
    install(clusterId, clusterTags) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            if (axiosClient_1.isInOcm) {
                const params = ClustersService.updateClusterTags(clusterTags, {});
                if (params.tags) {
                    yield ClustersService.update(clusterId, clusterTags, {});
                }
            }
            return ClustersAPI_1.default.install(clusterId);
        });
    },
    updateClusterTags(clusterTags, params) {
        const tags = (clusterTags === null || clusterTags === void 0 ? void 0 : clusterTags.split(',')) || [];
        if (tags.includes(constants_1.AI_UI_TAG)) {
            if (params.tags && params.tags.includes(constants_1.AI_CISCO_INTERSIGHT_TAG)) {
                tags === null || tags === void 0 ? void 0 : tags.push(constants_1.AI_CISCO_INTERSIGHT_TAG);
                params.tags = tags === null || tags === void 0 ? void 0 : tags.join(',');
            }
            else {
                delete params.tags;
            }
        }
        else {
            tags === null || tags === void 0 ? void 0 : tags.push(constants_1.AI_UI_TAG);
            params.tags = tags === null || tags === void 0 ? void 0 : tags.join(',');
        }
        return params;
    },
    get(clusterId) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const { data: cluster } = yield ClustersAPI_1.default.get(clusterId);
            return cluster;
        });
    },
    transformFormViewManifest(manifest) {
        return {
            folder: manifest.folder,
            fileName: manifest.filename,
            content: window.btoa(manifest.manifestYaml),
        };
    },
    getUpdateManifestParams(existingManifest, updatedManifest) {
        return {
            folder: existingManifest.folder,
            fileName: existingManifest.filename,
            updatedFolder: updatedManifest.folder,
            updatedFileName: updatedManifest.filename,
            updatedContent: window.btoa(updatedManifest.manifestYaml),
        };
    },
    createClusterManifests(manifests, clusterId) {
        const promises = manifests.map((manifest) => {
            return ClustersAPI_1.default.createCustomManifest(clusterId, this.transformFormViewManifest(manifest));
        });
        return Promise.all(promises);
    },
    removeClusterManifests(customManifests, clusterId) {
        const promises = customManifests.map((manifest) => {
            return ClustersAPI_1.default.removeCustomManifest(clusterId, manifest.folder || '', manifest.fileName || '');
        });
        return Promise.all(promises);
    },
    updateCustomManifest(existingManifest, updatedManifest, clusterId) {
        return ClustersAPI_1.default.updateCustomManifest(clusterId, this.getUpdateManifestParams(existingManifest, updatedManifest));
    },
};
exports.default = ClustersService;
//# sourceMappingURL=ClustersService.js.map