"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 BMCForm_1 = tslib_1.__importDefault(require("../Agent/BMCForm"));
const common_1 = require("../../../common");
const constants_1 = require("../common/constants");
const use_translation_wrapper_1 = require("../../../common/hooks/use-translation-wrapper");
const EditBMHModal = ({ isOpen, onClose, onEdit, infraEnv, bmh, nmStates, fetchSecret, usedHostnames, }) => {
    var _a, _b, _c, _d, _e, _f;
    const [secret, setSecret] = React.useState();
    const [isLoading, setLoading] = React.useState(true);
    const hasDHCP = ((_b = (_a = infraEnv.metadata) === null || _a === void 0 ? void 0 : _a.labels) === null || _b === void 0 ? void 0 : _b.networkType) !== 'static';
    const bmhName = (_c = bmh === null || bmh === void 0 ? void 0 : bmh.metadata) === null || _c === void 0 ? void 0 : _c.name;
    const bmhNamespace = (_d = bmh === null || bmh === void 0 ? void 0 : bmh.metadata) === null || _d === void 0 ? void 0 : _d.namespace;
    const bmhSecret = (_f = (_e = bmh === null || bmh === void 0 ? void 0 : bmh.spec) === null || _e === void 0 ? void 0 : _e.bmc) === null || _f === void 0 ? void 0 : _f.credentialsName;
    const nmState = nmStates.find((nm) => { var _a, _b; return ((_b = (_a = nm.metadata) === null || _a === void 0 ? void 0 : _a.labels) === null || _b === void 0 ? void 0 : _b[constants_1.AGENT_BMH_NAME_LABEL_KEY]) === bmhName; });
    const { t } = (0, use_translation_wrapper_1.useTranslation)();
    React.useEffect(() => {
        setSecret(undefined);
        const getResources = () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
            if (bmhSecret && bmhNamespace) {
                try {
                    const secretResult = yield fetchSecret(bmhNamespace, bmhSecret);
                    setSecret(secretResult);
                }
                catch (err) {
                    // console.error(t('ai:Could not get secret'), err);
                }
            }
            setLoading(false);
        });
        if (bmhNamespace && bmhName) {
            setLoading(true);
            void getResources();
        }
    }, [bmhName, bmhNamespace, fetchSecret, bmhSecret, hasDHCP, t]);
    return (React.createElement(react_core_1.Modal, { "aria-label": t('ai:Edit BMH dialog'), isOpen: isOpen, onClose: onClose, variant: react_core_1.ModalVariant.small, id: "edit-bmh-modal" },
        React.createElement(react_core_1.ModalHeader, { title: t('ai:Edit BMH') }),
        isLoading ? (React.createElement(common_1.LoadingState, null)) : (React.createElement(BMCForm_1.default, { isEdit: true, onCreateBMH: onEdit({ bmh, secret, nmState }), onClose: onClose, hasDHCP: hasDHCP, infraEnv: infraEnv, bmh: bmh, secret: secret, nmState: nmState, usedHostnames: usedHostnames }))));
};
exports.default = EditBMHModal;
//# sourceMappingURL=EditBMHModal.js.map