"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getConsoleUrl = void 0;
const tslib_1 = require("tslib");
const react_1 = tslib_1.__importDefault(require("react"));
const react_core_1 = require("@patternfly/react-core");
const uiState_1 = require("../../components/ui/uiState");
const DetailList_1 = require("../../components/ui/DetailList");
const ConsoleModal_1 = require("./ConsoleModal");
const use_translation_wrapper_1 = require("../../hooks/use-translation-wrapper");
const getConsoleUrl = (consoleUrl, isMceOperatorEnabled) => {
    if (isMceOperatorEnabled) {
        return consoleUrl.concat('/multicloud/infrastructure/clusters');
    }
    else {
        return consoleUrl;
    }
};
exports.getConsoleUrl = getConsoleUrl;
const ClusterCredentials = ({ cluster, credentials, error = false, retry, idPrefix = 'cluster-creds', credentialsError = '', notAvailable = false, isMceEnabled = false, }) => {
    var _a, _b;
    let credentialsBody;
    const { t } = (0, use_translation_wrapper_1.useTranslation)();
    if (error) {
        //Unauthorized users can't fetch cluster credentials
        if (credentialsError.includes('403')) {
            credentialsBody = (react_1.default.createElement(react_core_1.Alert, { variant: "info", isInline: true, title: 'You do not have permission to view the cluster credentials' }, "For more information, contact your organization administrator"));
        }
        else {
            credentialsBody = (react_1.default.createElement(uiState_1.ErrorState, { title: t('ai:Failed to fetch cluster credentials.'), fetchData: retry }));
        }
    }
    else if (notAvailable) {
        credentialsBody = (react_1.default.createElement(react_core_1.Alert, { variant: "info", isInline: true, title: t('ai:Admin credentials are not available.') }));
    }
    else if (!credentials) {
        credentialsBody = react_1.default.createElement(uiState_1.LoadingState, null);
    }
    else if (!credentials.username && !credentials.consoleUrl) {
        return react_1.default.createElement(react_1.default.Fragment, null, "N/A");
    }
    else {
        const consoleUrl = credentials.consoleUrl
            ? (0, exports.getConsoleUrl)(credentials.consoleUrl, isMceEnabled)
            : '';
        const hasVips = Boolean(((_a = cluster.apiVips) === null || _a === void 0 ? void 0 : _a.length) && ((_b = cluster.ingressVips) === null || _b === void 0 ? void 0 : _b.length));
        credentialsBody = (react_1.default.createElement(react_1.default.Fragment, null,
            react_1.default.createElement(DetailList_1.DetailList, null,
                credentials.consoleUrl && (react_1.default.createElement(DetailList_1.DetailItem, { title: t('ai:Web Console URL'), value: react_1.default.createElement(react_1.default.Fragment, null,
                        react_1.default.createElement(react_core_1.ClipboardCopy, { isReadOnly: true, className: "pf-v6-u-my-sm", "data-testid": `${idPrefix}-link-console-url` }, consoleUrl),
                        hasVips && (react_1.default.createElement(ConsoleModal_1.TroubleshootingOpenshiftConsoleButton, { consoleUrl: consoleUrl, cluster: cluster, idPrefix: idPrefix }))) })),
                credentials.username && (react_1.default.createElement(react_1.default.Fragment, null,
                    react_1.default.createElement(DetailList_1.DetailItem, { title: t('ai:Username'), value: react_1.default.createElement(react_core_1.ClipboardCopy, { isReadOnly: true, className: "pf-v6-u-my-sm" }, credentials.username) }),
                    react_1.default.createElement(DetailList_1.DetailItem, { title: t('ai:Password'), value: react_1.default.createElement(react_core_1.ClipboardCopy, { isReadOnly: true, className: "pf-v6-u-my-sm", onCopy: (event) => (0, react_core_1.clipboardCopyFunc)(event, credentials.password) }, "\u2022\u2022\u2022\u2022\u2022") }))))));
    }
    return react_1.default.createElement(react_core_1.GridItem, { span: 12 }, credentialsBody);
};
exports.default = ClusterCredentials;
//# sourceMappingURL=ClusterCredentials.js.map