"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 react_core_1 = require("@patternfly/react-core");
const use_translation_wrapper_1 = require("../../../../common/hooks/use-translation-wrapper");
const HypershiftKubeconfigDownload = ({ hostedCluster, fetchSecret, }) => {
    var _a, _b, _c, _d;
    const { t } = (0, use_translation_wrapper_1.useTranslation)();
    const kubeconfigSecretName = ((_a = hostedCluster.status) === null || _a === void 0 ? void 0 : _a.customKubeconfig)
        ? (_b = hostedCluster.status.customKubeconfig) === null || _b === void 0 ? void 0 : _b.name
        : (_d = (_c = hostedCluster.status) === null || _c === void 0 ? void 0 : _c.kubeconfig) === null || _d === void 0 ? void 0 : _d.name;
    const handleKubeconfigDownload = () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
        var _a, _b;
        const kubeconfigSecretNamespace = (_a = hostedCluster.metadata) === null || _a === void 0 ? void 0 : _a.namespace;
        if (kubeconfigSecretName && kubeconfigSecretNamespace) {
            try {
                const kubeconfigSecret = yield fetchSecret(kubeconfigSecretName, kubeconfigSecretNamespace);
                const kubeconfig = (_b = kubeconfigSecret.data) === null || _b === void 0 ? void 0 : _b.kubeconfig;
                if (!kubeconfig) {
                    throw new Error('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(react_core_1.Button, { variant: react_core_1.ButtonVariant.secondary, 
        // eslint-disable-next-line @typescript-eslint/no-misused-promises
        onClick: handleKubeconfigDownload, isDisabled: !kubeconfigSecretName }, t('ai:Download kubeconfig')));
};
exports.default = HypershiftKubeconfigDownload;
//# sourceMappingURL=HypershiftKubeconfigDownload.js.map