"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const react_1 = tslib_1.__importDefault(require("react"));
const Day2WizardContext_1 = tslib_1.__importDefault(require("./Day2WizardContext"));
const common_1 = require("../../../../common");
const constants_1 = require("./constants");
const services_1 = require("../../../services");
const dataTypes_1 = require("../../clusterConfiguration/staticIp/data/dataTypes");
const getWizardStepIds = (staticIpView) => {
    const stepIds = [...constants_1.defaultWizardSteps];
    if (staticIpView === dataTypes_1.StaticIpView.YAML) {
        stepIds.splice(1, 0, 'static-ip-yaml-view');
    }
    else if (staticIpView === dataTypes_1.StaticIpView.FORM) {
        stepIds.splice(1, 0, ...constants_1.staticIpFormViewSubSteps);
    }
    return stepIds;
};
const getNextStep = (wizardStepIds, currentStepId) => {
    const currentStepIdx = wizardStepIds.indexOf(currentStepId);
    return wizardStepIds[currentStepIdx + 1];
};
const Day2WizardContextProvider = ({ children, }) => {
    const [currentStepId, setCurrentStepId] = react_1.default.useState('cluster-details');
    const [selectedCpuArchitecture, setSelectedCpuArchitecture] = react_1.default.useState((0, common_1.getDefaultCpuArchitecture)());
    const [selectedIsoUrl, setSelectedIsoUrl] = react_1.default.useState('');
    const [selectedIpxeUrl, setSelectedIpxeUrl] = react_1.default.useState('');
    const [wizardStepIds, setWizardStepIds] = react_1.default.useState(getWizardStepIds());
    const contextValue = react_1.default.useMemo(() => {
        if (!wizardStepIds || !currentStepId) {
            return null;
        }
        return {
            moveBack() {
                const currentStepIdx = wizardStepIds.indexOf(currentStepId);
                let nextStepId = wizardStepIds[currentStepIdx - 1];
                if (nextStepId === 'static-ip-host-configurations') {
                    //when moving back to static ip form view, it should go to network wide configurations
                    nextStepId = 'static-ip-network-wide-configurations';
                }
                setCurrentStepId(nextStepId);
            },
            moveNext() {
                setCurrentStepId(getNextStep(wizardStepIds, currentStepId));
            },
            onUpdateStaticIpView(view) {
                setWizardStepIds(getWizardStepIds(view));
                if (view === dataTypes_1.StaticIpView.YAML) {
                    setCurrentStepId('static-ip-yaml-view');
                }
                else {
                    setCurrentStepId('static-ip-network-wide-configurations');
                }
            },
            onUpdateHostNetworkConfigType(type) {
                let stepIds;
                if (type === services_1.HostsNetworkConfigurationType.STATIC) {
                    stepIds = getWizardStepIds(dataTypes_1.StaticIpView.FORM);
                }
                else {
                    stepIds = getWizardStepIds();
                }
                setWizardStepIds(stepIds);
                setCurrentStepId(getNextStep(stepIds, currentStepId));
            },
            wizardStepIds,
            currentStepId,
            setCurrentStepId,
            selectedIsoUrl,
            setSelectedIsoUrl,
            selectedCpuArchitecture,
            setSelectedCpuArchitecture,
            selectedIpxeUrl,
            setSelectedIpxeUrl,
        };
    }, [wizardStepIds, currentStepId, selectedIsoUrl, selectedCpuArchitecture, selectedIpxeUrl]);
    if (!contextValue) {
        return null;
    }
    return (react_1.default.createElement(react_1.default.Fragment, null,
        react_1.default.createElement(Day2WizardContext_1.default.Provider, { value: contextValue }, children)));
};
exports.default = Day2WizardContextProvider;
//# sourceMappingURL=Day2WizardContextProvider.js.map