"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.LayoutReducer = exports.getColumnFilterSelector = exports.getCurrentLayoutSelector = exports.getColumnFiltersSelector = exports.LayoutReady = exports.LayoutSelect = exports.LayoutSave = exports.LayoutDelete = exports.LayoutAdd = exports.LayoutGridFilterClear = exports.LayoutGridFilterUnSuspend = exports.LayoutGridFilterSuspend = exports.LayoutGridFilterSet = exports.LayoutColumnFilterUnSuspendAll = exports.LayoutColumnFilterSuspendAll = exports.LayoutColumnFilterUnSuspend = exports.LayoutColumnFilterSuspend = exports.LayoutColumnFilterClear = exports.LayoutColumnFilterClearAll = exports.LayoutColumnFilterSet = exports.LayoutColumnFilterEdit = exports.LayoutColumnFilterAdd = exports.LAYOUT_GRID_FILTER_CLEAR = exports.LAYOUT_GRID_FILTER_SET = exports.LAYOUT_GRID_FILTER_UNSUSPEND = exports.LAYOUT_GRID_FILTER_SUSPEND = exports.LAYOUT_COLUMN_FILTER_UNSUSPEND_ALL = exports.LAYOUT_COLUMN_FILTER_SUSPEND_ALL = exports.LAYOUT_COLUMN_FILTER_UNSUSPEND = exports.LAYOUT_COLUMN_FILTER_SUSPEND = exports.LAYOUT_COLUMN_FILTER_CLEAR = exports.LAYOUT_COLUMN_FILTER_CLEAR_ALL = exports.LAYOUT_COLUMN_FILTER_SET = exports.LAYOUT_COLUMN_FILTER_EDIT = exports.LAYOUT_COLUMN_FILTER_ADD = exports.LAYOUT_SAVE = exports.LAYOUT_SELECT = exports.LAYOUT_DELETE = exports.LAYOUT_EDIT = exports.LAYOUT_ADD = exports.LAYOUT_READY = void 0;
const tslib_1 = require("tslib");
const GeneralConstants_1 = require("../../Utilities/Constants/GeneralConstants");
const AdaptableHelper_1 = tslib_1.__importDefault(require("../../Utilities/Helpers/AdaptableHelper"));
const LayoutHelpers_1 = require("../../Api/Implementation/LayoutHelpers");
/**
 * @ReduxAction Layout Module is ready
 */
exports.LAYOUT_READY = 'LAYOUT_READY';
/**
 * @ReduxAction A Layout has been added
 */
exports.LAYOUT_ADD = 'LAYOUT_ADD';
/**
 * @ReduxAction A Layout has been edited
 */
exports.LAYOUT_EDIT = 'LAYOUT_EDIT';
/**
 * @ReduxAction A Layout has been deleted
 */
exports.LAYOUT_DELETE = 'LAYOUT_DELETE';
/**
 * @ReduxAction A Layout has been selected
 */
exports.LAYOUT_SELECT = 'LAYOUT_SELECT';
/**
 * @ReduxAction A Layout has been (auto)saved
 */
exports.LAYOUT_SAVE = 'LAYOUT_SAVE';
/**
 * @ReduxAction A Column Filter has been added
 */
exports.LAYOUT_COLUMN_FILTER_ADD = 'LAYOUT_COLUMN_FILTER_ADD';
/**
 * @ReduxAction A Column Filter has been edited
 */
exports.LAYOUT_COLUMN_FILTER_EDIT = 'LAYOUT_COLUMN_FILTER_EDIT';
/**
 * @ReduxAction Column Filters have been set
 */
exports.LAYOUT_COLUMN_FILTER_SET = 'LAYOUT_COLUMN_FILTER_SET';
/**
 * @ReduxAction All Column Filters have been cleared
 */
exports.LAYOUT_COLUMN_FILTER_CLEAR_ALL = 'LAYOUT_COLUMN_FILTER_CLEAR_ALL';
/**
 * @ReduxAction A Column Filter has been cleared
 */
exports.LAYOUT_COLUMN_FILTER_CLEAR = 'LAYOUT_COLUMN_FILTER_CLEAR';
/**
 * @ReduxAction Column Filter is suspended
 */
exports.LAYOUT_COLUMN_FILTER_SUSPEND = 'LAYOUT_COLUMN_FILTER_SUSPEND';
/**
 * @ReduxAction Column Filter is un-suspended, or activated
 */
exports.LAYOUT_COLUMN_FILTER_UNSUSPEND = 'LAYOUT_COLUMN_FILTER_UNSUSPEND';
/**
 * @ReduxAction All Column Filters are suspended
 */
exports.LAYOUT_COLUMN_FILTER_SUSPEND_ALL = 'LAYOUT_COLUMN_FILTER_SUSPEND_ALL';
/**
 * @ReduxAction All Column Filters are un-suspended, or activated
 */
exports.LAYOUT_COLUMN_FILTER_UNSUSPEND_ALL = 'LAYOUT_COLUMN_FILTER_UNSUSPEND_ALL';
/**
 * @ReduxAction Grid Filter is un-suspended, or activated
 */
exports.LAYOUT_GRID_FILTER_SUSPEND = 'LAYOUT_GRID_FILTER_SUSPEND';
/**
 * @ReduxAction Grid Filter is un-suspended, or activated
 */
exports.LAYOUT_GRID_FILTER_UNSUSPEND = 'LAYOUT_GRID_FILTER_UNSUSPEND';
/**
 * @ReduxAction The Grid Filter has been edited
 */
exports.LAYOUT_GRID_FILTER_SET = 'LAYOUT_GRID_FILTER_SET';
/**
 * @ReduxAction The Grid Filter has been cleared
 */
exports.LAYOUT_GRID_FILTER_CLEAR = 'LAYOUT_GRID_FILTER_CLEAR';
// Column Filter Methods
const LayoutColumnFilterAdd = (columnFilter) => ({
    type: exports.LAYOUT_COLUMN_FILTER_ADD,
    columnFilter,
});
exports.LayoutColumnFilterAdd = LayoutColumnFilterAdd;
const LayoutColumnFilterEdit = (columnFilter) => ({
    type: exports.LAYOUT_COLUMN_FILTER_EDIT,
    columnFilter,
});
exports.LayoutColumnFilterEdit = LayoutColumnFilterEdit;
const LayoutColumnFilterSet = (columnFilter) => ({
    type: exports.LAYOUT_COLUMN_FILTER_SET,
    columnFilter,
});
exports.LayoutColumnFilterSet = LayoutColumnFilterSet;
const LayoutColumnFilterClearAll = () => ({
    type: exports.LAYOUT_COLUMN_FILTER_CLEAR_ALL,
});
exports.LayoutColumnFilterClearAll = LayoutColumnFilterClearAll;
const LayoutColumnFilterClear = (columnId) => ({
    type: exports.LAYOUT_COLUMN_FILTER_CLEAR,
    columnId,
});
exports.LayoutColumnFilterClear = LayoutColumnFilterClear;
const LayoutColumnFilterSuspend = (columnFilter) => ({
    type: exports.LAYOUT_COLUMN_FILTER_SUSPEND,
    columnFilter,
});
exports.LayoutColumnFilterSuspend = LayoutColumnFilterSuspend;
const LayoutColumnFilterUnSuspend = (columnFilter) => ({
    type: exports.LAYOUT_COLUMN_FILTER_UNSUSPEND,
    columnFilter,
});
exports.LayoutColumnFilterUnSuspend = LayoutColumnFilterUnSuspend;
const LayoutColumnFilterSuspendAll = () => ({
    type: exports.LAYOUT_COLUMN_FILTER_SUSPEND_ALL,
});
exports.LayoutColumnFilterSuspendAll = LayoutColumnFilterSuspendAll;
const LayoutColumnFilterUnSuspendAll = () => ({
    type: exports.LAYOUT_COLUMN_FILTER_UNSUSPEND_ALL,
});
exports.LayoutColumnFilterUnSuspendAll = LayoutColumnFilterUnSuspendAll;
const LayoutGridFilterSet = (gridFilter) => ({
    type: exports.LAYOUT_GRID_FILTER_SET,
    gridFilter,
});
exports.LayoutGridFilterSet = LayoutGridFilterSet;
const LayoutGridFilterSuspend = () => ({
    type: exports.LAYOUT_GRID_FILTER_SUSPEND,
});
exports.LayoutGridFilterSuspend = LayoutGridFilterSuspend;
const LayoutGridFilterUnSuspend = () => ({
    type: exports.LAYOUT_GRID_FILTER_UNSUSPEND,
});
exports.LayoutGridFilterUnSuspend = LayoutGridFilterUnSuspend;
const LayoutGridFilterClear = () => ({
    type: exports.LAYOUT_GRID_FILTER_CLEAR,
});
exports.LayoutGridFilterClear = LayoutGridFilterClear;
const LayoutAdd = (layout) => ({
    type: exports.LAYOUT_ADD,
    layout: (0, LayoutHelpers_1.normalizeLayout)(layout),
});
exports.LayoutAdd = LayoutAdd;
const LayoutDelete = (layout) => ({
    type: exports.LAYOUT_DELETE,
    layout,
});
exports.LayoutDelete = LayoutDelete;
const LayoutSave = (layout) => ({
    type: exports.LAYOUT_SAVE,
    layout: (0, LayoutHelpers_1.normalizeLayout)(layout),
});
exports.LayoutSave = LayoutSave;
const LayoutSelect = (layoutName) => ({
    type: exports.LAYOUT_SELECT,
    layoutName,
});
exports.LayoutSelect = LayoutSelect;
const LayoutReady = (layoutState) => ({
    type: exports.LAYOUT_READY,
    layoutState,
});
exports.LayoutReady = LayoutReady;
const getColumnFiltersSelector = (state) => {
    const currentLayout = (0, exports.getCurrentLayoutSelector)(state);
    return currentLayout?.ColumnFilters ?? [];
};
exports.getColumnFiltersSelector = getColumnFiltersSelector;
const getCurrentLayoutSelector = (state) => {
    return state.Layout.Layouts?.find((layout) => layout.Name === state.Layout.CurrentLayout);
};
exports.getCurrentLayoutSelector = getCurrentLayoutSelector;
const getColumnFilterSelector = (state, columnId) => {
    return { ...(0, exports.getColumnFiltersSelector)(state).find((cf) => cf.ColumnId === columnId) };
};
exports.getColumnFilterSelector = getColumnFilterSelector;
const initialState = {
    CurrentLayout: GeneralConstants_1.EMPTY_STRING,
    Layouts: undefined,
};
const LayoutReducer = (state = initialState, action) => {
    let layouts;
    const currentLayout = state.Layouts?.find((layout) => layout.Name === state.CurrentLayout);
    switch (action.type) {
        case exports.LAYOUT_SELECT: {
            const newCurrentLayout = action.layoutName;
            if ((state.Layouts || []).find((l) => l.Name === newCurrentLayout)) {
                return Object.assign({}, state, { CurrentLayout: newCurrentLayout });
            }
            return state;
        }
        case exports.LAYOUT_ADD: {
            const actionLayout = action.layout;
            AdaptableHelper_1.default.addAdaptableObjectPrimitives(actionLayout);
            layouts = [].concat(state.Layouts || []);
            layouts.push(actionLayout);
            return { ...state, Layouts: layouts };
        }
        case exports.LAYOUT_SAVE: {
            const actionLayout = action.layout;
            let found = false;
            let Layouts = state.Layouts.map((abObject) => {
                if (found) {
                    // if already found, keep things as they were
                    return abObject;
                }
                found = abObject.Uuid === actionLayout.Uuid;
                return found ? actionLayout : abObject;
            });
            if (!found) {
                // let's try to find by name
                Layouts = state.Layouts.map((abObject) => {
                    if (found) {
                        // if already found, keep things as they were
                        return abObject;
                    }
                    found = abObject.Name === actionLayout.Name;
                    return found ? actionLayout : abObject;
                });
            }
            if (!found) {
                Layouts = [...state.Layouts, actionLayout];
            }
            return {
                ...state,
                Layouts,
            };
        }
        case exports.LAYOUT_DELETE: {
            const actionLayout = action.layout;
            const newLayouts = state.Layouts.filter((abObject) => abObject.Uuid !== actionLayout.Uuid);
            return {
                ...state,
                CurrentLayout: state.CurrentLayout === actionLayout.Name ? newLayouts[0].Name : state.CurrentLayout,
                Layouts: newLayouts,
            };
        }
        case exports.LAYOUT_COLUMN_FILTER_SET: {
            let columnFilters = currentLayout.ColumnFilters ?? [];
            const columnFilterAction = action.columnFilter;
            AdaptableHelper_1.default.addAdaptableObjectPrimitives(columnFilterAction);
            columnFilters = columnFilters
                .filter((colFilter) => {
                return colFilter.ColumnId !== columnFilterAction.ColumnId;
            })
                .concat(columnFilterAction)
                .map((cf) => ({ ...cf }));
            const Layouts = state.Layouts.map((layout) => {
                if (layout.Name === currentLayout.Name) {
                    return {
                        ...layout,
                        ColumnFilters: columnFilters,
                    };
                }
                return layout;
            });
            return { ...state, Layouts };
        }
        case exports.LAYOUT_COLUMN_FILTER_ADD: {
            const columnFilterAction = action.columnFilter;
            let columnFilters = (currentLayout.ColumnFilters ?? []).filter(
            // make sure we are not adding a filter that already exists
            (cf) => cf.ColumnId !== columnFilterAction.ColumnId);
            AdaptableHelper_1.default.addAdaptableObjectPrimitives(columnFilterAction);
            columnFilters.push(columnFilterAction);
            const Layouts = state.Layouts.map((layout) => {
                if (layout.Name === currentLayout.Name) {
                    return {
                        ...layout,
                        ColumnFilters: columnFilters,
                    };
                }
                return layout;
            });
            return { ...state, Layouts };
        }
        case exports.LAYOUT_COLUMN_FILTER_EDIT: {
            const columnFilterAction = action.columnFilter;
            // it is safer to change the col id, we cannot have multiple filter on the same id
            // first we filter out other filters with the same colId, to ensure we have only one
            const cleanColumnFilters = (currentLayout.ColumnFilters ?? []).filter((cf) => cf.ColumnId !== columnFilterAction.ColumnId);
            cleanColumnFilters.push(columnFilterAction);
            const Layouts = state.Layouts.map((layout) => {
                if (layout.Name === currentLayout.Name) {
                    return {
                        ...layout,
                        ColumnFilters: cleanColumnFilters,
                    };
                }
                return layout;
            });
            return { ...state, Layouts };
        }
        case exports.LAYOUT_COLUMN_FILTER_CLEAR_ALL: {
            const Layouts = state.Layouts.map((layout) => {
                if (layout.Name === currentLayout.Name) {
                    return {
                        ...layout,
                        ColumnFilters: [],
                    };
                }
                return layout;
            });
            return { ...state, Layouts };
        }
        case exports.LAYOUT_COLUMN_FILTER_CLEAR: {
            const actionTypedDelete = action;
            const columnFilters = (currentLayout.ColumnFilters ?? []).filter((cf) => {
                // make sure we are deleting all filters on the same column
                return cf.ColumnId !== actionTypedDelete.columnId;
            });
            const Layouts = state.Layouts.map((layout) => {
                if (layout.Name === currentLayout.Name) {
                    return {
                        ...layout,
                        ColumnFilters: columnFilters,
                    };
                }
                return layout;
            });
            return { ...state, Layouts };
        }
        case exports.LAYOUT_COLUMN_FILTER_SUSPEND: {
            const actionTypedSuspend = action;
            const columnFilters = (currentLayout.ColumnFilters ?? []).map((abObject) => abObject.Uuid === actionTypedSuspend.columnFilter.Uuid
                ? { ...abObject, IsSuspended: true }
                : abObject);
            const Layouts = state.Layouts.map((layout) => {
                if (layout.Name === currentLayout.Name) {
                    return {
                        ...layout,
                        ColumnFilters: columnFilters,
                    };
                }
                return layout;
            });
            return { ...state, Layouts };
        }
        case exports.LAYOUT_COLUMN_FILTER_UNSUSPEND: {
            const actionTypedSuspend = action;
            const columnFilters = (currentLayout.ColumnFilters ?? []).map((abObject) => abObject.Uuid === actionTypedSuspend.columnFilter.Uuid
                ? { ...abObject, IsSuspended: false }
                : abObject);
            const Layouts = state.Layouts.map((layout) => {
                if (layout.Name === currentLayout.Name) {
                    return {
                        ...layout,
                        ColumnFilters: columnFilters,
                    };
                }
                return layout;
            });
            return { ...state, Layouts };
        }
        case exports.LAYOUT_COLUMN_FILTER_SUSPEND_ALL: {
            const columnFilters = (currentLayout.ColumnFilters ?? []).map((abObject) => ({
                ...abObject,
                IsSuspended: true,
            }));
            const Layouts = state.Layouts.map((layout) => {
                if (layout.Name === currentLayout.Name) {
                    return {
                        ...layout,
                        ColumnFilters: columnFilters,
                    };
                }
                return layout;
            });
            return { ...state, Layouts };
        }
        case exports.LAYOUT_COLUMN_FILTER_UNSUSPEND_ALL: {
            const columnFilters = (currentLayout.ColumnFilters ?? []).map((abObject) => ({
                ...abObject,
                IsSuspended: false,
            }));
            const Layouts = state.Layouts.map((layout) => {
                if (layout.Name === currentLayout.Name) {
                    return {
                        ...layout,
                        ColumnFilters: columnFilters,
                    };
                }
                return layout;
            });
            return { ...state, Layouts };
        }
        case exports.LAYOUT_GRID_FILTER_SET: {
            const actionTyped = action;
            return {
                ...state,
                Layouts: state.Layouts.map((layout) => {
                    if (layout.Name === currentLayout.Name) {
                        return {
                            ...layout,
                            GridFilter: {
                                ...layout.GridFilter,
                                Expression: actionTyped.gridFilter,
                            },
                        };
                    }
                    return layout;
                }),
            };
        }
        case exports.LAYOUT_GRID_FILTER_SUSPEND: {
            const Layouts = state.Layouts.map((layout) => {
                if (layout.Name === currentLayout.Name) {
                    return {
                        ...layout,
                        GridFilter: {
                            ...layout.GridFilter,
                            IsSuspended: true,
                        },
                    };
                }
                return layout;
            });
            return { ...state, Layouts };
        }
        case exports.LAYOUT_GRID_FILTER_UNSUSPEND: {
            const Layouts = state.Layouts.map((layout) => {
                if (layout.Name === currentLayout.Name) {
                    return {
                        ...layout,
                        GridFilter: {
                            ...layout.GridFilter,
                            IsSuspended: false,
                        },
                    };
                }
                return layout;
            });
            return { ...state, Layouts };
        }
        case exports.LAYOUT_GRID_FILTER_CLEAR: {
            const Layouts = state.Layouts.map((layout) => {
                let gridFilter = currentLayout.GridFilter;
                if (gridFilter) {
                    gridFilter.IsSuspended = false;
                    gridFilter.Expression = '';
                }
                if (layout.Name === currentLayout.Name) {
                    return {
                        ...layout,
                        gridFilter,
                    };
                }
                return layout;
            });
            return { ...state, Layouts };
        }
        default:
            return state;
    }
};
exports.LayoutReducer = LayoutReducer;
