"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const react_1 = tslib_1.__importDefault(require("react"));
const file_saver_1 = require("file-saver");
const KubeconfigDownload_1 = tslib_1.__importDefault(require("../../../common/components/clusterDetail/KubeconfigDownload"));
const status_1 = require("../helpers/status");
const use_translation_wrapper_1 = require("../../../common/hooks/use-translation-wrapper");
const ClusterDeploymentKubeconfigDownload = ({ clusterDeployment, agentClusterInstall, fetchSecret, }) => {
    var _a;
    const [clusterStatus] = (0, status_1.getClusterStatus)(agentClusterInstall);
    const { t } = (0, use_translation_wrapper_1.useTranslation)();
    const handleKubeconfigDownload = () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
        var _a, _b, _c, _d, _e, _f, _g, _h;
        const kubeconfigSecretName = ((_c = (_b = (_a = agentClusterInstall.spec) === null || _a === void 0 ? void 0 : _a.clusterMetadata) === null || _b === void 0 ? void 0 : _b.adminKubeconfigSecretRef) === null || _c === void 0 ? void 0 : _c.name) ||
            ((_f = (_e = (_d = clusterDeployment.spec) === null || _d === void 0 ? void 0 : _d.clusterMetadata) === null || _e === void 0 ? void 0 : _e.adminKubeconfigSecretRef) === null || _f === void 0 ? void 0 : _f.name);
        const kubeconfigSecretNamespace = (_g = clusterDeployment.metadata) === null || _g === void 0 ? void 0 : _g.namespace;
        if (kubeconfigSecretName && kubeconfigSecretNamespace) {
            try {
                const kubeconfigSecret = yield fetchSecret(kubeconfigSecretName, kubeconfigSecretNamespace);
                const kubeconfig = (_h = kubeconfigSecret.data) === null || _h === void 0 ? void 0 : _h.kubeconfig;
                if (!kubeconfig)
                    throw new Error(t('ai:Kubeconfig is empty.'));
                const blob = new Blob([atob(kubeconfig)], { type: 'text/plain;charset=utf-8' });
                (0, file_saver_1.saveAs)(blob, 'kubeconfig.yaml');
            }
            catch (e) {
                // console.error('Failed to fetch kubeconfig secret.', e);
            }
        }
    });
    return (react_1.default.createElement(KubeconfigDownload_1.default
    // eslint-disable-next-line @typescript-eslint/no-misused-promises
    , { 
        // eslint-disable-next-line @typescript-eslint/no-misused-promises
        handleDownload: handleKubeconfigDownload, clusterId: ((_a = clusterDeployment.metadata) === null || _a === void 0 ? void 0 : _a.uid) || '', status: clusterStatus }));
};
exports.default = ClusterDeploymentKubeconfigDownload;
//# sourceMappingURL=ClusterDeploymentKubeconfigDownload.js.map