"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PopupReducer = exports.ProgressIndicatorHide = exports.ProgressIndicatorShow = exports.PopupClearParam = exports.PopupCancelConfirmation = exports.PopupConfirmConfirmation = exports.PopupShowConfirmation = exports.PopupConfirmPrompt = exports.PopupHidePrompt = exports.PopupShowPrompt = exports.PopupShowAlert = exports.PopupHideForm = exports.PopupShowForm = exports.PopupHideWindow = exports.PopupShowWindow = exports.PopupHideScreen = exports.PopupShowScreen = exports.PROGRESS_INDICATOR_HIDE = exports.PROGRESS_INDICATOR_SHOW = exports.POPUP_HIDE_FORM = exports.POPUP_SHOW_FORM = exports.POPUP_HIDE_WINDOW = exports.POPUP_SHOW_WINDOW = exports.POPUP_CLEAR_PARAM = exports.POPUP_CANCEL_CONFIRMATION = exports.POPUP_CONFIRM_CONFIRMATION = exports.POPUP_SHOW_CONFIRMATION = exports.POPUP_CONFIRM_PROMPT = exports.POPUP_HIDE_PROMPT = exports.POPUP_SHOW_PROMPT = exports.POPUP_SHOW_ALERT = exports.POPUP_HIDE_SCREEN = exports.POPUP_SHOW_SCREEN = void 0;
exports.POPUP_SHOW_SCREEN = 'POPUP_SHOW_SCREEN';
exports.POPUP_HIDE_SCREEN = 'POPUP_HIDE_SCREEN';
exports.POPUP_SHOW_ALERT = 'POPUP_SHOW_ALERT';
exports.POPUP_SHOW_PROMPT = 'POPUP_SHOW_PROMPT';
exports.POPUP_HIDE_PROMPT = 'POPUP_HIDE_PROMPT';
exports.POPUP_CONFIRM_PROMPT = 'POPUP_CONFIRM_PROMPT';
exports.POPUP_SHOW_CONFIRMATION = 'POPUP_SHOW_CONFIRMATION';
exports.POPUP_CONFIRM_CONFIRMATION = 'POPUP_CONFIRM_CONFIRMATION';
exports.POPUP_CANCEL_CONFIRMATION = 'POPUP_CANCEL_CONFIRMATION';
exports.POPUP_CLEAR_PARAM = 'POPUP_CLEAR_PARAM';
exports.POPUP_SHOW_WINDOW = 'POPUP_SHOW_WINDOW';
exports.POPUP_HIDE_WINDOW = 'POPUP_HIDE_WINDOW';
exports.POPUP_SHOW_FORM = 'POPUP_SHOW_FORM';
exports.POPUP_HIDE_FORM = 'POPUP_FORM_HIDE';
exports.PROGRESS_INDICATOR_SHOW = 'PROGRESS_INDICATOR_SHOW';
exports.PROGRESS_INDICATOR_HIDE = 'PROGRESS_INDICATOR_HIDE';
const PopupShowScreen = (componentModule, componentName, params, popupProps) => ({
    type: exports.POPUP_SHOW_SCREEN,
    ComponentModule: componentModule,
    ComponentName: componentName,
    Params: params,
    PopupProps: popupProps,
});
exports.PopupShowScreen = PopupShowScreen;
const PopupHideScreen = () => ({
    type: exports.POPUP_HIDE_SCREEN,
});
exports.PopupHideScreen = PopupHideScreen;
const PopupShowWindow = (config) => ({
    type: exports.POPUP_SHOW_WINDOW,
    Id: config.Id,
    Title: config.Title,
    PopupProps: config.PopupProps,
    FactoryId: config.FactoryId,
    Icon: config.Icon,
});
exports.PopupShowWindow = PopupShowWindow;
const PopupHideWindow = (id) => ({
    type: exports.POPUP_HIDE_WINDOW,
    Id: id,
});
exports.PopupHideWindow = PopupHideWindow;
const PopupShowForm = (config) => ({
    type: exports.POPUP_SHOW_FORM,
    Id: config.Id,
    Form: config.Form,
    FormProps: config.FormProps,
    prepareContext: config.prepareContext,
});
exports.PopupShowForm = PopupShowForm;
const PopupHideForm = (id) => ({
    type: exports.POPUP_HIDE_FORM,
    Id: id,
});
exports.PopupHideForm = PopupHideForm;
const PopupShowAlert = (alert) => {
    return {
        type: exports.POPUP_SHOW_ALERT,
        alert: alert,
    };
};
exports.PopupShowAlert = PopupShowAlert;
const PopupShowPrompt = (prompt) => ({
    type: exports.POPUP_SHOW_PROMPT,
    prompt: prompt,
});
exports.PopupShowPrompt = PopupShowPrompt;
const PopupHidePrompt = () => ({
    type: exports.POPUP_HIDE_PROMPT,
});
exports.PopupHidePrompt = PopupHidePrompt;
const PopupConfirmPrompt = (inputText) => ({
    type: exports.POPUP_CONFIRM_PROMPT,
    InputText: inputText,
});
exports.PopupConfirmPrompt = PopupConfirmPrompt;
const PopupShowConfirmation = (confirmation) => ({
    type: exports.POPUP_SHOW_CONFIRMATION,
    confirmation: confirmation,
});
exports.PopupShowConfirmation = PopupShowConfirmation;
const PopupConfirmConfirmation = (comment) => ({
    type: exports.POPUP_CONFIRM_CONFIRMATION,
    comment,
});
exports.PopupConfirmConfirmation = PopupConfirmConfirmation;
const PopupCancelConfirmation = () => ({
    type: exports.POPUP_CANCEL_CONFIRMATION,
});
exports.PopupCancelConfirmation = PopupCancelConfirmation;
const PopupClearParam = () => ({
    type: exports.POPUP_CLEAR_PARAM,
});
exports.PopupClearParam = PopupClearParam;
const ProgressIndicatorShow = (progressIndicatorConfig) => ({
    type: exports.PROGRESS_INDICATOR_SHOW,
    progressIndicatorConfig: progressIndicatorConfig,
});
exports.ProgressIndicatorShow = ProgressIndicatorShow;
const ProgressIndicatorHide = () => ({
    type: exports.PROGRESS_INDICATOR_HIDE,
});
exports.ProgressIndicatorHide = ProgressIndicatorHide;
const initialState = {
    ScreenPopup: {
        ShowScreenPopup: false,
        ComponentModule: undefined,
        ComponentName: '',
        Params: null,
    },
    ConfirmationPopup: {
        ShowConfirmationPopup: false,
        Msg: '',
        Header: '',
        ConfirmButtonText: '',
        CancelButtonText: '',
        CancelAction: null,
        ConfirmAction: null,
        ShowInputBox: false,
        ConfirmationComment: null,
        MessageType: 'Info',
    },
    PromptPopup: {
        ShowPromptPopup: false,
        Header: '',
        Message: '',
        ConfirmAction: null,
    },
    WindowPopup: {
        PopupList: [],
    },
    FormPopup: {
        FormList: [],
    },
    ProgressIndicator: { active: false, text: '' },
};
const PopupReducer = (state = initialState, action) => {
    switch (action.type) {
        case exports.POPUP_SHOW_SCREEN: {
            let actionTypedShowPopup = action;
            let newScreenPopup = {
                ShowScreenPopup: true,
                ComponentModule: actionTypedShowPopup.ComponentModule,
                ComponentName: actionTypedShowPopup.ComponentName,
                Params: actionTypedShowPopup.Params,
                PopupProps: actionTypedShowPopup.PopupProps,
            };
            return Object.assign({}, state, { ScreenPopup: newScreenPopup });
        }
        case exports.POPUP_HIDE_SCREEN: {
            let newScreenPopup = {
                ShowScreenPopup: false,
                ComponentModule: undefined,
                ComponentName: '',
                Params: null,
            };
            return Object.assign({}, state, { ScreenPopup: newScreenPopup });
        }
        case exports.POPUP_SHOW_PROMPT: {
            let actionTyped = action;
            let newPromptPopup = {
                ShowPromptPopup: true,
                Header: actionTyped.prompt.Header,
                Message: actionTyped.prompt.Msg,
                ConfirmAction: actionTyped.prompt.ConfirmAction,
                ConfirmActionCreator: actionTyped.prompt.ConfirmActionCreator,
                DefaultValue: actionTyped.prompt.DefaultValue,
            };
            return Object.assign({}, state, { PromptPopup: newPromptPopup });
        }
        case exports.POPUP_HIDE_PROMPT: {
            let newPromptPopup = {
                ShowPromptPopup: false,
                Header: '',
                Message: '',
                ConfirmAction: null,
                ConfirmActionCreator: null,
                DefaultValue: '',
            };
            return Object.assign({}, state, { PromptPopup: newPromptPopup });
        }
        case exports.POPUP_CONFIRM_PROMPT: {
            //we dispatch the Action of ConfirmAction in the middelware in order to keep the reducer pure
            let newPromptPopup = {
                ShowPromptPopup: false,
                Header: '',
                Message: '',
                ConfirmAction: null,
            };
            return Object.assign({}, state, { PromptPopup: newPromptPopup });
        }
        case exports.POPUP_SHOW_CONFIRMATION: {
            let actionTyped = action;
            let newConfirmationPopup = {
                ShowConfirmationPopup: true,
                Msg: actionTyped.confirmation.Msg,
                Header: actionTyped.confirmation.Header,
                ConfirmButtonText: actionTyped.confirmation.ConfirmButtonText,
                CancelButtonText: actionTyped.confirmation.CancelButtonText,
                ConfirmAction: actionTyped.confirmation.ConfirmAction,
                CancelAction: actionTyped.confirmation.CancelAction,
                ShowInputBox: actionTyped.confirmation.ShowInputBox,
                ConfirmationComment: null,
                MessageType: actionTyped.confirmation.MessageType,
            };
            return Object.assign({}, state, {
                ConfirmationPopup: newConfirmationPopup,
            });
        }
        case exports.POPUP_CONFIRM_CONFIRMATION: {
            let actionTyped = action;
            //we dispatch the Action of ConfirmAction in the middelware in order to keep the reducer pure
            let newConfirmationPopup = {
                ShowConfirmationPopup: false,
                Msg: '',
                Header: '',
                ConfirmButtonText: '',
                CancelButtonText: '',
                ConfirmAction: null,
                CancelAction: null,
                ShowInputBox: false,
                ConfirmationComment: actionTyped.comment,
                MessageType: null, // ???
            };
            return Object.assign({}, state, {
                ConfirmationPopup: newConfirmationPopup,
            });
        }
        case exports.POPUP_CANCEL_CONFIRMATION: {
            //we dispatch the Action of CancelAction in the middleware in order to keep the reducer pure
            let newConfirmationPopup = {
                ShowConfirmationPopup: false,
                Msg: '',
                Header: '',
                ConfirmButtonText: '',
                CancelButtonText: '',
                ConfirmAction: null,
                CancelAction: null,
                ShowInputBox: false,
                ConfirmationComment: null,
                MessageType: null,
            };
            return Object.assign({}, state, {
                ConfirmationPopup: newConfirmationPopup,
            });
        }
        case exports.POPUP_CLEAR_PARAM: {
            let newScreenPopup = {
                ShowScreenPopup: state.ScreenPopup.ShowScreenPopup,
                ComponentModule: state.ScreenPopup.ComponentModule,
                ComponentName: state.ScreenPopup.ComponentName,
                Params: null,
            };
            return Object.assign({}, state, { ScreenPopup: newScreenPopup });
        }
        case exports.POPUP_SHOW_WINDOW: {
            const showWindowAction = action;
            const windowSettings = {
                Id: showWindowAction.Id,
                Icon: showWindowAction.Icon,
                Title: showWindowAction.Title,
                PopupProps: showWindowAction.PopupProps,
                FactoryId: showWindowAction.FactoryId,
            };
            let PopupList = [...state.WindowPopup.PopupList];
            // Prevent opening the same popup twice
            if (state.WindowPopup.PopupList.some((windowPopup) => windowPopup.Id === windowSettings.Id)) {
                PopupList = PopupList.map((windowPopup) => {
                    if (windowPopup.Id !== windowSettings.Id) {
                        return windowPopup;
                    }
                    return {
                        ...windowPopup,
                        ...windowSettings, // props may have changed
                    };
                });
            }
            else {
                PopupList = [...PopupList, windowSettings];
            }
            return {
                ...state,
                WindowPopup: {
                    ...state.WindowPopup,
                    PopupList,
                },
            };
        }
        case exports.POPUP_HIDE_WINDOW: {
            const PopupList = state.WindowPopup.PopupList.filter((windowPopup) => windowPopup.Id !== action.Id);
            return {
                ...state,
                WindowPopup: {
                    ...state.WindowPopup,
                    PopupList,
                },
            };
        }
        case exports.POPUP_SHOW_FORM: {
            const showWindowAction = action;
            const windowSettings = {
                Id: showWindowAction.Id,
                FormProps: showWindowAction.FormProps,
                Form: showWindowAction.Form,
                prepareContext: showWindowAction.prepareContext,
            };
            if (state.FormPopup.FormList.some((windowPopup) => windowPopup.Id === windowSettings.Id)) {
                return state;
            }
            const FormList = [...state.FormPopup.FormList, windowSettings];
            return {
                ...state,
                FormPopup: {
                    ...state.WindowPopup,
                    FormList,
                },
            };
        }
        case exports.POPUP_HIDE_FORM: {
            const FormList = state.FormPopup.FormList.filter((form) => form.Id !== action.Id);
            return {
                ...state,
                FormPopup: {
                    ...state.WindowPopup,
                    FormList,
                },
            };
        }
        case exports.PROGRESS_INDICATOR_SHOW: {
            const actionTypedAdd = action;
            const updatedState = {
                ProgressIndicator: {
                    active: true,
                    text: actionTypedAdd.progressIndicatorConfig.text,
                    render: actionTypedAdd.progressIndicatorConfig.render,
                    frameworkComponent: actionTypedAdd.progressIndicatorConfig.frameworkComponent,
                    renderMode: actionTypedAdd.progressIndicatorConfig.renderMode,
                },
            };
            return Object.assign({}, state, updatedState);
        }
        case exports.PROGRESS_INDICATOR_HIDE: {
            const updatedState = {
                ProgressIndicator: {
                    active: false,
                },
            };
            return Object.assign({}, state, updatedState);
        }
        default:
            return state;
    }
};
exports.PopupReducer = PopupReducer;
