"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 toAssisted_1 = require("../helpers/toAssisted");
const use_translation_wrapper_1 = require("../../../common/hooks/use-translation-wrapper");
const ClusterDeploymentCredentials = ({ clusterDeployment, agentClusterInstall, agents, consoleUrl, fetchSecret, }) => {
    var _a, _b, _c, _d, _e, _f, _g;
    const [credentials, setCredentials] = react_1.default.useState();
    const [isError, setIsError] = react_1.default.useState(false);
    const [isLoading, setIsLoading] = react_1.default.useState(true);
    const cluster = (0, toAssisted_1.getAICluster)({ clusterDeployment, agentClusterInstall, agents });
    const adminPasswordSecretRefName = ((_c = (_b = (_a = agentClusterInstall.spec) === null || _a === void 0 ? void 0 : _a.clusterMetadata) === null || _b === void 0 ? void 0 : _b.adminPasswordSecretRef) === 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.adminPasswordSecretRef) === null || _f === void 0 ? void 0 : _f.name);
    const namespace = (_g = clusterDeployment.metadata) === null || _g === void 0 ? void 0 : _g.namespace;
    const { t } = (0, use_translation_wrapper_1.useTranslation)();
    react_1.default.useEffect(() => {
        const fetchCredentials = () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
            var _a, _b;
            setIsError(false);
            try {
                if (adminPasswordSecretRefName && namespace) {
                    const secret = yield fetchSecret(adminPasswordSecretRefName, namespace);
                    setCredentials({
                        username: atob(((_a = secret === null || secret === void 0 ? void 0 : secret.data) === null || _a === void 0 ? void 0 : _a.username) || ''),
                        password: atob(((_b = secret === null || secret === void 0 ? void 0 : secret.data) === null || _b === void 0 ? void 0 : _b.password) || ''),
                        consoleUrl,
                    });
                }
            }
            catch (e) {
                setIsError(true);
                // console.error(t('ai:Failed to fetch adminPasswordSecret secret.'), e);
            }
            finally {
                setIsLoading(false);
            }
        });
        void fetchCredentials();
    }, [adminPasswordSecretRefName, namespace, fetchSecret, consoleUrl, t]);
    return (react_1.default.createElement(common_1.ClusterCredentials, { cluster: cluster, credentials: credentials, error: isError, notAvailable: !credentials && !isLoading }));
};
exports.default = ClusterDeploymentCredentials;
//# sourceMappingURL=ClusterDeploymentCredentials.js.map