"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const react_1 = tslib_1.__importDefault(require("react"));
const common_1 = require("../../../common");
const ClusterWizardContext_1 = require("./ClusterWizardContext");
const ClusterWizardFooter_1 = tslib_1.__importDefault(require("./ClusterWizardFooter"));
const ClusterWizardNavigation_1 = tslib_1.__importDefault(require("./ClusterWizardNavigation"));
const WithErrorBoundary_1 = require("../../../common/components/ErrorHandling/WithErrorBoundary");
const react_core_1 = require("@patternfly/react-core");
const utils_1 = require("../../../common/utils");
const external_link_alt_icon_1 = require("@patternfly/react-icons/dist/js/icons/external-link-alt-icon");
const CredentialsDownload = ({ cluster }) => {
    const clusterWizardContext = (0, ClusterWizardContext_1.useClusterWizardContext)();
    const [isChecked, setIsChecked] = react_1.default.useState(false);
    const [isDownloading, setIsDownloading] = react_1.default.useState(false);
    const { addAlert, clearAlerts } = (0, common_1.useAlerts)();
    const { t } = (0, common_1.useTranslation)();
    const downloadSingleFile = (fileName) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
        try {
            const response = yield common_1.ClustersAPI.downloadClusterCredentials(cluster.id, fileName);
            (0, utils_1.downloadFile)('', response.data, fileName);
            return true;
        }
        catch (e) {
            (0, common_1.handleApiError)(e, (e) => {
                addAlert({
                    title: t('ai:Could not download {{fileName}} file', { fileName }),
                    message: (0, common_1.getApiErrorMessage)(e),
                });
            });
            return false;
        }
    });
    const handleDownloadClick = () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
        clearAlerts();
        setIsDownloading(true);
        const configSuccess = yield downloadSingleFile('kubeconfig');
        const passwordSuccess = yield downloadSingleFile('kubeadmin-password');
        const hasError = !configSuccess || !passwordSuccess;
        setTimeout(() => {
            setIsDownloading(false);
            if (!hasError) {
                clusterWizardContext.moveNext();
            }
        }, 500);
    });
    const footer = (react_1.default.createElement(ClusterWizardFooter_1.default, { cluster: cluster, onNext: () => void handleDownloadClick(), onBack: () => clusterWizardContext.moveBack(), isNextDisabled: !isChecked || isDownloading, nextButtonText: t('ai:Download credentials'), isNextButtonLoading: isDownloading }));
    return (react_1.default.createElement(common_1.ClusterWizardStep, { navigation: react_1.default.createElement(ClusterWizardNavigation_1.default, { cluster: cluster }), footer: footer },
        react_1.default.createElement(WithErrorBoundary_1.WithErrorBoundary, { title: t('ai:Failed to load Credentials Download step') },
            react_1.default.createElement(react_core_1.Stack, { hasGutter: true },
                react_1.default.createElement(common_1.ClusterWizardStepHeader, null, t('ai:Download credentials')),
                react_1.default.createElement(react_core_1.Alert, { title: t('ai:Make sure you download and store your credentials files in a safe place'), variant: react_core_1.AlertVariant.warning, isInline: true, actionLinks: react_1.default.createElement("a", { href: common_1.KUBECONFIG_INFO_LINK, target: "_blank", rel: "noopener noreferrer" },
                        t('ai:Learn more about Kubeconfig'),
                        " ",
                        react_1.default.createElement(external_link_alt_icon_1.ExternalLinkAltIcon, null)) },
                    t(`ai:You should use your cluster's Kubeconfig file to gain access to the cluster.`),
                    react_1.default.createElement("br", null),
                    t(`ai:You can use username and password to log-in to your console through the UI.`)),
                react_1.default.createElement(react_core_1.Checkbox, { id: "credentials-download-agreement", isChecked: isChecked, onChange: () => setIsChecked(!isChecked), label: t('ai:I understand that I need to download credentials files prior of proceeding with the cluster installation.') })))));
};
exports.default = CredentialsDownload;
//# sourceMappingURL=CredentialsDownload.js.map