"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useClusterStatusVarieties = void 0;
const tslib_1 = require("tslib");
const react_1 = tslib_1.__importDefault(require("react"));
const common_1 = require("../../../common");
const utils_1 = require("./utils");
const apis_1 = require("../../services/apis");
const ClusterDetailStatusMessages_1 = tslib_1.__importDefault(require("./ClusterDetailStatusMessages"));
const react_core_1 = require("@patternfly/react-core");
const utils_2 = require("../../../common/utils");
const useClusterStatusVarieties = (cluster) => {
    const [credentials, setCredentials] = react_1.default.useState();
    const [credentialsError, setCredentialsError] = react_1.default.useState('');
    const clusterId = cluster === null || cluster === void 0 ? void 0 : cluster.id;
    const clusterStatus = cluster === null || cluster === void 0 ? void 0 : cluster.status;
    const clusterMonitoredOperators = (0, common_1.selectMonitoredOperators)(cluster === null || cluster === void 0 ? void 0 : cluster.monitoredOperators);
    const olmOperators = (0, common_1.selectOlmOperators)(cluster);
    const failedOlmOperators = olmOperators.filter((o) => o.status === 'failed');
    const consoleOperator = react_1.default.useMemo(() => clusterMonitoredOperators.find((o) => o.name === 'console'), [clusterMonitoredOperators]);
    const fetchCredentials = react_1.default.useCallback(() => {
        const fetch = () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
            setCredentialsError('');
            if (!clusterId) {
                return;
            }
            try {
                const response = yield apis_1.ClustersAPI.getCredentials(clusterId);
                setCredentials(response.data);
            }
            catch (err) {
                setCredentialsError((0, utils_2.getErrorMessage)(err));
            }
        });
        void fetch();
    }, [clusterId]);
    const consoleOperatorStatus = consoleOperator === null || consoleOperator === void 0 ? void 0 : consoleOperator.status;
    react_1.default.useEffect(() => {
        if ((!consoleOperatorStatus && clusterStatus === 'installed') || // Retain backwards compatibility with clusters which don't have monitored clusters
            consoleOperatorStatus === 'available') {
            fetchCredentials();
        }
    }, [clusterStatus, consoleOperatorStatus, fetchCredentials]);
    return {
        credentials,
        credentialsError,
        olmOperators,
        failedOlmOperators,
        consoleOperator,
        fetchCredentials,
    };
};
exports.useClusterStatusVarieties = useClusterStatusVarieties;
const ClusterDetailStatusVarieties = ({ cluster, clusterVarieties, showAddHostsInfo = true, }) => {
    const { credentials, credentialsError, consoleOperator, fetchCredentials } = clusterVarieties;
    const showClusterCredentials = (consoleOperator === null || consoleOperator === void 0 ? void 0 : consoleOperator.status) === 'available' || (!consoleOperator && cluster.status === 'installed'); // Retain backwards compatibility with clusters which don't have monitored clusters
    return (react_1.default.createElement(react_core_1.Stack, { hasGutter: true },
        showClusterCredentials && (react_1.default.createElement(react_core_1.StackItem, { className: "pf-v6-u-mt-lg" },
            react_1.default.createElement(common_1.ClusterCredentials, { cluster: cluster, credentials: credentials, error: !!credentialsError, retry: fetchCredentials, idPrefix: (0, utils_1.getClusterDetailId)('cluster-creds'), credentialsError: credentialsError, isMceEnabled: (0, common_1.hasEnabledOperators)(cluster.monitoredOperators, common_1.OPERATOR_NAME_MCE) }))),
        react_1.default.createElement(react_core_1.StackItem, { className: "pf-v6-u-mt-lg" },
            react_1.default.createElement(ClusterDetailStatusMessages_1.default, { cluster: cluster, showAddHostsInfo: showAddHostsInfo }))));
};
exports.default = ClusterDetailStatusVarieties;
//# sourceMappingURL=ClusterDetailStatusVarieties.js.map