"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.StaticIpPage = void 0;
const tslib_1 = require("tslib");
const react_1 = tslib_1.__importDefault(require("react"));
const react_core_1 = require("@patternfly/react-core");
const StaticIpViewRadioGroup_1 = tslib_1.__importDefault(require("./StaticIpViewRadioGroup"));
const fromInfraEnv_1 = require("../data/fromInfraEnv");
const YamlView_1 = require("./YamlView/YamlView");
const ClusterWizardContext_1 = require("../../../clusterWizard/ClusterWizardContext");
const FormViewHosts_1 = require("./FormViewHosts/FormViewHosts");
const FormViewNetworkWide_1 = require("./FormViewNetworkWide/FormViewNetworkWide");
require("./staticIp.css");
const common_1 = require("../../../../../common");
const isoRegenerationAlert = (react_1.default.createElement(react_core_1.Alert, { variant: react_core_1.AlertVariant.warning, isInline: true, "data-testid": "regenerate-iso-alert", title: "To add new hosts that will use the new or edited configurations, you'll need to regenerate the\n  Discovery ISO in the 'Host discovery' step and boot your new hosts from it." }));
const StaticIpPage = ({ infraEnv, updateInfraEnv, onFormStateChange: onFormStateChangeParent, }) => {
    const clusterWizardContext = (0, ClusterWizardContext_1.useClusterWizardContext)();
    const [confirmOnChangeView, setConfirmOnChangeView] = react_1.default.useState(false);
    const [viewChanged, setViewChanged] = react_1.default.useState(false);
    const onChangeView = react_1.default.useCallback((view) => {
        clusterWizardContext.onUpdateStaticIpView(view);
        setViewChanged(true);
    }, [clusterWizardContext]);
    const initialStaticIpInfo = react_1.default.useMemo(() => {
        return (0, fromInfraEnv_1.getStaticIpInfo)(infraEnv);
        // eslint-disable-next-line react-hooks/exhaustive-deps
    }, []);
    const onFormStateChange = react_1.default.useCallback((formState) => {
        const hasFilledData = clusterWizardContext.currentStepId === 'static-ip-host-configurations' ||
            !formState.isEmpty;
        setConfirmOnChangeView(hasFilledData);
        onFormStateChangeParent(formState);
    }, [clusterWizardContext.currentStepId, onFormStateChangeParent]);
    const viewProps = {
        onFormStateChange,
        infraEnv,
        updateInfraEnv,
        showEmptyValues: viewChanged,
    };
    const content = (() => {
        switch (clusterWizardContext.currentStepId) {
            case 'static-ip-yaml-view':
                return react_1.default.createElement(YamlView_1.YamlView, Object.assign({}, viewProps));
            case 'static-ip-network-wide-configurations':
                return react_1.default.createElement(FormViewNetworkWide_1.FormViewNetworkWide, Object.assign({}, viewProps));
            case 'static-ip-host-configurations':
                return react_1.default.createElement(FormViewHosts_1.FormViewHosts, Object.assign({}, viewProps));
            default:
                throw new Error(`Unexpected wizard step id ${clusterWizardContext.currentStepId} when entering static ip page`);
        }
    })();
    if (!initialStaticIpInfo) {
        return null;
    }
    return (react_1.default.createElement(react_core_1.Grid, { hasGutter: true },
        react_1.default.createElement(react_core_1.GridItem, null,
            react_1.default.createElement(common_1.ClusterWizardStepHeader, null, "Static network configurations"),
            react_1.default.createElement(react_core_1.Content, { component: react_core_1.ContentVariants.small }, "Network configuration can be done using either the form view or YAML view. Configurations done in this step are for discovering hosts.")),
        (clusterWizardContext.currentStepId === 'static-ip-network-wide-configurations' ||
            clusterWizardContext.currentStepId === 'static-ip-yaml-view') && (react_1.default.createElement(StaticIpViewRadioGroup_1.default, { initialView: initialStaticIpInfo.view, confirmOnChangeView: confirmOnChangeView, onChangeView: onChangeView })),
        initialStaticIpInfo.isDataComplete && isoRegenerationAlert,
        content));
};
exports.StaticIpPage = StaticIpPage;
//# sourceMappingURL=StaticIpPage.js.map