"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useModalDialogsContext = exports.ModalDialogsContextProvider = void 0;
const tslib_1 = require("tslib");
const react_1 = tslib_1.__importDefault(require("react"));
const common_1 = require("../../../common");
const { openDialog: openDialogAction, closeDialog: closeDialogAction } = common_1.dialogsSlice.actions;
const dialogsReducer = common_1.dialogsSlice.reducer;
const dialogIds = [
    'eventsDialog',
    'editHostDialog',
    'deleteHostDialog',
    'resetHostDialog',
    'additionalNTPSourcesDialog',
    'resetClusterDialog',
    'cancelInstallationDialog',
    'discoveryImageDialog',
    'day2DiscoveryImageDialog',
    'UpdateDay2ApiVipDialog',
    'massUpdateHostnameDialog',
    'massDeleteHostDialog',
    'resetSingleClusterDialog',
];
const ModalDialogsContext = react_1.default.createContext(undefined);
const ModalDialogsContextProvider = ({ children }) => {
    const [dialogsState, dispatchDialogsAction] = react_1.default.useReducer(dialogsReducer, {});
    function getOpenDialog(dialogId) {
        return (data) => dispatchDialogsAction(openDialogAction({ dialogId, data }));
    }
    const getCloseDialog = (dialogId) => () => dispatchDialogsAction(closeDialogAction({ dialogId }));
    const context = dialogIds.reduce((context, dialogId) => {
        const dialogData = dialogsState[dialogId];
        context[dialogId] = {
            isOpen: !!dialogData,
            open: (data) => getOpenDialog(dialogId)(data),
            close: () => getCloseDialog(dialogId)(),
            data: dialogData,
        };
        return context;
    }, {});
    return (react_1.default.createElement(ModalDialogsContext.Provider, { value: context }, children));
};
exports.ModalDialogsContextProvider = ModalDialogsContextProvider;
const useModalDialogsContext = () => {
    const context = react_1.default.useContext(ModalDialogsContext);
    if (context === undefined) {
        throw new Error('useModalDialogsContext must be used within a ModalDialogsContextProvider');
    }
    return context;
};
exports.useModalDialogsContext = useModalDialogsContext;
//# sourceMappingURL=ModalDialogsContext.js.map