"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.isAHostVM = exports.onAdditionalNtpSourceAction = exports.downloadHostInstallationLogs = void 0;
const tslib_1 = require("tslib");
const file_saver_1 = require("file-saver");
const api_1 = require("../../../common/api");
const slice_1 = require("../../store/slices/current-cluster/slice");
const services_1 = require("../../services");
const ClustersAPI_1 = tslib_1.__importDefault(require("../../../common/api/assisted-service/ClustersAPI"));
const utils_1 = require("../../../common/utils");
const downloadHostInstallationLogs = (addAlert, host) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
    try {
        if (!host.clusterId) {
            throw new Error(`Cannot download logs for host ${host.id}. Missing clusterId field value in host.`);
        }
        if (api_1.isInOcm) {
            const { data } = yield ClustersAPI_1.default.getPresignedForClusterFiles({
                clusterId: host.clusterId,
                fileName: 'logs',
                hostId: host.id,
                logsType: 'host',
            });
            (0, file_saver_1.saveAs)(data.url);
        }
        else {
            const { data, fileName } = yield services_1.ClustersService.downloadLogs(host.clusterId, host.id);
            (0, file_saver_1.saveAs)(data, fileName);
        }
    }
    catch (e) {
        (0, api_1.handleApiError)(e, (e) => {
            addAlert({ title: 'Could not download host logs.', message: (0, api_1.getApiErrorMessage)(e) });
        });
    }
});
exports.downloadHostInstallationLogs = downloadHostInstallationLogs;
const onAdditionalNtpSourceAction = (
// eslint-disable-next-line @typescript-eslint/no-explicit-any
dispatch, cluster, additionalNtpSource, onError) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
    try {
        const values = {
            additionalNtpSource,
        };
        const { data } = yield services_1.ClustersService.update(cluster.id, cluster.tags, values);
        dispatch((0, slice_1.updateCluster)(data));
    }
    catch (e) {
        (0, api_1.handleApiError)(e, () => onError((0, api_1.getApiErrorMessage)(e)));
    }
});
exports.onAdditionalNtpSourceAction = onAdditionalNtpSourceAction;
const isAHostVM = (hosts) => hosts.some((host) => {
    var _a;
    const inventory = (0, utils_1.stringToJSON)(host.inventory || '') || {};
    return (_a = inventory.systemVendor) === null || _a === void 0 ? void 0 : _a.virtual;
});
exports.isAHostVM = isAHostVM;
//# sourceMappingURL=utils.js.map