"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const react_core_1 = require("@patternfly/react-core");
const check_circle_icon_1 = require("@patternfly/react-icons/dist/js/icons/check-circle-icon");
const exclamation_triangle_icon_1 = require("@patternfly/react-icons/dist/js/icons/exclamation-triangle-icon");
const pencil_alt_icon_1 = require("@patternfly/react-icons/dist/js/icons/pencil-alt-icon");
const common_1 = require("../../../common");
const common_2 = require("../common");
const EditPullSecretModal_1 = tslib_1.__importDefault(require("../modals/EditPullSecretModal"));
const EditSSHKeyModal_1 = tslib_1.__importDefault(require("../modals/EditSSHKeyModal"));
const EditNtpSourcesModal_1 = tslib_1.__importDefault(require("../modals/EditNtpSourcesModal"));
const use_translation_wrapper_1 = require("../../../common/hooks/use-translation-wrapper");
const modals_1 = require("../modals");
const EditItem = ({ title, onEdit, isLoading, isWarning, noIcon }) => {
    let icon = (React.createElement(react_core_1.Icon, { status: "success" },
        React.createElement(check_circle_icon_1.CheckCircleIcon, null)));
    if (isLoading) {
        icon = React.createElement(react_core_1.Spinner, { size: "md" });
    }
    else if (isWarning) {
        icon = (React.createElement(react_core_1.Icon, { status: "warning" },
            React.createElement(exclamation_triangle_icon_1.ExclamationTriangleIcon, null)));
    }
    const btn = (React.createElement(React.Fragment, null,
        title,
        "\u00A0",
        React.createElement(react_core_1.Button, { icon: React.createElement(pencil_alt_icon_1.PencilAltIcon, null), variant: "plain", onClick: onEdit })));
    return noIcon ? (btn) : (React.createElement("div", null,
        icon,
        "\u00A0",
        btn));
};
const NotConfigured = () => {
    const { t } = (0, use_translation_wrapper_1.useTranslation)();
    return React.createElement(react_core_1.Content, { component: react_core_1.ContentVariants.small }, t('ai:Not configured'));
};
const EnvironmentDetails = ({ infraEnv, fetchSecret, onEditPullSecret, onEditSSHKey, onEditNtpSources, hasAgents, hasBMHs, onEditProxy, }) => {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2, _3, _4, _5, _6, _7, _8;
    const [editPullSecret, setEditPullSecret] = React.useState(false);
    const [editSSHKey, setEditSSHKey] = React.useState(false);
    const [editNtpSources, setEditNtpSources] = React.useState(false);
    const [pullSecret, setPullSecret] = React.useState();
    const [pullSecretError, setPullSecretError] = React.useState();
    const [pullSecretLoading, setPullSecretLoading] = React.useState(true);
    const [editProxy, setEditProxy] = React.useState(false);
    const namespace = (_b = (_a = infraEnv.metadata) === null || _a === void 0 ? void 0 : _a.namespace) !== null && _b !== void 0 ? _b : '';
    const pullSecretName = (_e = (_d = (_c = infraEnv.spec) === null || _c === void 0 ? void 0 : _c.pullSecretRef) === null || _d === void 0 ? void 0 : _d.name) !== null && _e !== void 0 ? _e : '';
    const { t } = (0, use_translation_wrapper_1.useTranslation)();
    React.useEffect(() => {
        const fetch = () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
            try {
                if (namespace && pullSecretName) {
                    const result = yield fetchSecret(namespace, pullSecretName);
                    setPullSecret(result);
                }
            }
            catch (err) {
                setPullSecret(undefined);
                // eslint-disable-next-line
                if (err.code !== 404) {
                    setPullSecretError(err.message || t('ai:Could not fetch pull secret'));
                }
            }
            finally {
                setPullSecretLoading(false);
            }
        });
        void fetch();
    }, [namespace, pullSecretName, fetchSecret, editPullSecret, t]);
    const hasProxy = ((_g = (_f = infraEnv.spec) === null || _f === void 0 ? void 0 : _f.proxy) === null || _g === void 0 ? void 0 : _g.httpProxy) ||
        ((_j = (_h = infraEnv.spec) === null || _h === void 0 ? void 0 : _h.proxy) === null || _j === void 0 ? void 0 : _j.httpsProxy) ||
        ((_l = (_k = infraEnv.spec) === null || _k === void 0 ? void 0 : _k.proxy) === null || _l === void 0 ? void 0 : _l.noProxy);
    const arch = ((_m = infraEnv.spec) === null || _m === void 0 ? void 0 : _m.cpuArchitecture)
        ? (_p = common_1.architectureData[(_o = infraEnv.spec) === null || _o === void 0 ? void 0 : _o.cpuArchitecture]) === null || _p === void 0 ? void 0 : _p.label
        : (_q = infraEnv.spec) === null || _q === void 0 ? void 0 : _q.cpuArchitecture;
    return (React.createElement(React.Fragment, null,
        React.createElement(react_core_1.Grid, { hasGutter: true },
            React.createElement(react_core_1.GridItem, { span: 12 },
                React.createElement(react_core_1.Title, { headingLevel: "h1", size: react_core_1.TitleSizes.lg }, t('ai:Infrastructure environment details'))),
            React.createElement(react_core_1.GridItem, { span: 6 },
                React.createElement(react_core_1.DescriptionList, null,
                    React.createElement(react_core_1.DescriptionListGroup, null,
                        React.createElement(react_core_1.DescriptionListTerm, null, t('ai:Name')),
                        React.createElement(react_core_1.DescriptionListDescription, null, (_r = infraEnv.metadata) === null || _r === void 0 ? void 0 : _r.name)),
                    React.createElement(react_core_1.DescriptionListGroup, null,
                        React.createElement(react_core_1.DescriptionListTerm, null, t('ai:Location')),
                        React.createElement(react_core_1.DescriptionListDescription, null, (_u = (_t = (_s = infraEnv.metadata) === null || _s === void 0 ? void 0 : _s.labels) === null || _t === void 0 ? void 0 : _t[common_2.AGENT_LOCATION_LABEL_KEY]) !== null && _u !== void 0 ? _u : t('ai:No location'))),
                    React.createElement(react_core_1.DescriptionListGroup, null,
                        React.createElement(react_core_1.DescriptionListTerm, null, t('ai:Network type')),
                        React.createElement(react_core_1.DescriptionListDescription, null, ((_w = (_v = infraEnv.metadata) === null || _v === void 0 ? void 0 : _v.labels) === null || _w === void 0 ? void 0 : _w.networkType) === 'static'
                            ? t('ai:Static IP, bridges and bonds')
                            : t('ai:DHCP only'))),
                    React.createElement(react_core_1.DescriptionListGroup, null,
                        React.createElement(react_core_1.DescriptionListTerm, null, t('ai:CPU architecture')),
                        React.createElement(react_core_1.DescriptionListDescription, null, arch !== null && arch !== void 0 ? arch : '--')),
                    ((_x = infraEnv.spec) === null || _x === void 0 ? void 0 : _x.osImageVersion) && (React.createElement(react_core_1.DescriptionListGroup, null,
                        React.createElement(react_core_1.DescriptionListTerm, null, t('ai:OpenShift version')),
                        React.createElement(react_core_1.DescriptionListDescription, null, `OpenShift ${(_y = infraEnv.spec) === null || _y === void 0 ? void 0 : _y.osImageVersion}`))),
                    React.createElement(react_core_1.DescriptionListGroup, null,
                        React.createElement(react_core_1.DescriptionListTerm, null, t('ai:Labels')),
                        React.createElement(react_core_1.DescriptionListDescription, null, Object.keys(((_z = infraEnv.metadata) === null || _z === void 0 ? void 0 : _z.labels) || {}).map((k) => {
                            var _a, _b;
                            return (React.createElement(react_core_1.Label, { key: k },
                                k,
                                "=",
                                ((_b = (_a = infraEnv.metadata) === null || _a === void 0 ? void 0 : _a.labels) === null || _b === void 0 ? void 0 : _b[k]) || ''));
                        }))),
                    ((_0 = infraEnv.metadata) === null || _0 === void 0 ? void 0 : _0.creationTimestamp) && (React.createElement(react_core_1.DescriptionListGroup, null,
                        React.createElement(react_core_1.DescriptionListTerm, null, t('ai:Created at')),
                        React.createElement(react_core_1.DescriptionListDescription, null, new Date(infraEnv.metadata.creationTimestamp).toString()))))),
            React.createElement(react_core_1.GridItem, { span: 6 },
                React.createElement(react_core_1.DescriptionList, null,
                    React.createElement(react_core_1.DescriptionListGroup, null,
                        React.createElement(react_core_1.DescriptionListTerm, null,
                            React.createElement(EditItem, { noIcon: true, title: t('ai:Proxy settings'), onEdit: () => setEditProxy(true) })),
                        !hasProxy ? (React.createElement(NotConfigured, null)) : (React.createElement(React.Fragment, null,
                            React.createElement(react_core_1.DescriptionListTerm, null, t('ai:HTTP Proxy URL')),
                            React.createElement(react_core_1.DescriptionListDescription, null, ((_2 = (_1 = infraEnv.spec) === null || _1 === void 0 ? void 0 : _1.proxy) === null || _2 === void 0 ? void 0 : _2.httpProxy) || React.createElement(NotConfigured, null)),
                            React.createElement(react_core_1.DescriptionListTerm, null, t('ai:HTTPS Proxy URL')),
                            React.createElement(react_core_1.DescriptionListDescription, null, ((_4 = (_3 = infraEnv.spec) === null || _3 === void 0 ? void 0 : _3.proxy) === null || _4 === void 0 ? void 0 : _4.httpsProxy) || React.createElement(NotConfigured, null)),
                            React.createElement(react_core_1.DescriptionListTerm, null, t('ai:No proxy domains')),
                            React.createElement(react_core_1.DescriptionListDescription, null, ((_7 = (_6 = (_5 = infraEnv.spec) === null || _5 === void 0 ? void 0 : _5.proxy) === null || _6 === void 0 ? void 0 : _6.noProxy) === null || _7 === void 0 ? void 0 : _7.split(',').map((k) => React.createElement(react_core_1.Label, { key: k }, k))) || React.createElement(NotConfigured, null))))),
                    React.createElement(react_core_1.DescriptionListGroup, null,
                        React.createElement(react_core_1.DescriptionListTerm, null, t('ai:Secret and keys')),
                        React.createElement(react_core_1.DescriptionListDescription, null,
                            React.createElement(React.Fragment, null,
                                React.createElement(EditItem, { title: t('ai:Pull secret'), onEdit: () => setEditPullSecret(true), isLoading: pullSecretLoading, isWarning: !pullSecret }),
                                React.createElement(EditItem, { title: t('ai:SSH public key'), onEdit: () => setEditSSHKey(true), isWarning: !((_8 = infraEnv.spec) === null || _8 === void 0 ? void 0 : _8.sshAuthorizedKey) }),
                                React.createElement(EditItem, { title: t('ai:NTP sources'), onEdit: () => setEditNtpSources(true) }))))))),
        React.createElement(EditPullSecretModal_1.default, { isOpen: editPullSecret, onClose: () => setEditPullSecret(false), pullSecret: pullSecret, pullSecretError: pullSecretError, pullSecretLoading: pullSecretLoading, onSubmit: onEditPullSecret, infraEnv: infraEnv, hasAgents: hasAgents, hasBMHs: hasBMHs }),
        React.createElement(EditSSHKeyModal_1.default, { isOpen: editSSHKey, onClose: () => setEditSSHKey(false), infraEnv: infraEnv, onSubmit: onEditSSHKey, hasAgents: hasAgents, hasBMHs: hasBMHs }),
        React.createElement(EditNtpSourcesModal_1.default, { isOpen: editNtpSources, onClose: () => setEditNtpSources(false), infraEnv: infraEnv, onSubmit: onEditNtpSources }),
        editProxy && (React.createElement(modals_1.EditProxyModal, { onClose: () => setEditProxy(false), infraEnv: infraEnv, onSubmit: onEditProxy, hasAgents: hasAgents, hasBMHs: hasBMHs }))));
};
exports.default = EnvironmentDetails;
//# sourceMappingURL=EnvironmentDetails.js.map