"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.LayoutDraftColumnFilterEdit = exports.LayoutDraftColumnFilterAdd = exports.LayoutUpdateCurrentDraft = exports.SetMenuModuleItems = exports.setSettingsPanelItems = exports.SetTreeModeOff = exports.SetTreeModeOn = exports.SetPivotModeOff = exports.SetPivotModeOn = exports.GridRefreshCells = exports.GridSetCellSummary = exports.GridCreateCellSummary = exports.GridSetSelectedRows = exports.GridSetSelectedCells = exports.GridClearSort = exports.GridSetSort = exports.GridEditColumn = exports.GridRemoveColumn = exports.GridAddColumns = exports.GridAddColumn = exports.GridSetColumns = exports.LAYOUT_DRAFT_GRID_FILTER_SET = exports.LAYOUT_DRAFT_COLUMN_FILTER_UNSUSPEND_ALL = exports.LAYOUT_DRAFT_COLUMN_FILTER_UNSUSPEND = exports.LAYOUT_DRAFT_COLUMN_FILTER_SUSPEND_ALL = exports.LAYOUT_DRAFT_COLUMN_FILTER_SUSPEND = exports.LAYOUT_DRAFT_COLUMN_FILTER_CLEAR = exports.LAYOUT_DRAFT_COLUMN_FILTER_CLEAR_ALL = exports.LAYOUT_DRAFT_COLUMN_FILTER_SET = exports.LAYOUT_DRAFT_COLUMN_FILTER_EDIT = exports.LAYOUT_DRAFT_COLUMN_FILTER_ADD = exports.LAYOUT_UPDATE_CURRENT_DRAFT = exports.GRID_SET_TREE_MODE_OFF = exports.GRID_SET_TREE_MODE_ON = exports.GRID_SET_PIVOT_MODE_OFF = exports.GRID_SET_PIVOT_MODE_ON = exports.GRID_SET_MENU_MODULE_ITEMS = exports.GRID_SET_SETTINGS_PANEL_ITEMS = exports.GRID_REFRESH_CELLS = exports.GRID_SET_CELLS_SUMMARY = exports.GRID_CREATE_CELLS_SUMMARY = exports.GRID_SET_SELECTED_ROWS = exports.GRID_SET_SELECTED_CELLS = exports.GRID_CLEAR_SORT = exports.GRID_SET_SORT = exports.GRID_EDIT_COLUMN = exports.GRID_REMOVE_COLUMN = exports.GRID_ADD_COLUMNS = exports.GRID_ADD_COLUMN = exports.GRID_SET_COLUMNS = void 0;
exports.GridReducer = exports.LayoutDraftGridFilterSet = exports.LayoutDraftColumnFilterUnsuspendAll = exports.LayoutDraftColumnFilterUnsuspend = exports.LayoutDraftColumnFilterSuspendAll = exports.LayoutDraftColumnFilterSuspend = exports.LayoutDraftColumnFilterClearAll = exports.LayoutDraftColumnFilterClear = exports.LayoutDraftColumnFilterSet = void 0;
const tslib_1 = require("tslib");
const GeneralConstants_1 = require("../../Utilities/Constants/GeneralConstants");
const LayoutRedux_1 = require("./LayoutRedux");
const AdaptableHelper_1 = tslib_1.__importDefault(require("../../Utilities/Helpers/AdaptableHelper"));
/**
 * @ReduxAction Columns have been set in the Grid
 */
exports.GRID_SET_COLUMNS = 'GRID_SET_COLUMNS';
/**
 * @ReduxAction A Column has been added to the Grid
 */
exports.GRID_ADD_COLUMN = 'GRID_ADD_COLUMN';
/**
 * @ReduxAction Columns have been added to the Grid
 */
exports.GRID_ADD_COLUMNS = 'GRID_ADD_COLUMNS';
/**
 * @ReduxAction A Column has been removed from the Grid
 */
exports.GRID_REMOVE_COLUMN = 'GRID_REMOVE_COLUMN';
/**
 * @ReduxAction A Column has been edited in the Grid
 */
exports.GRID_EDIT_COLUMN = 'GRID_EDIT_COLUMN';
/**
 * @ReduxAction Sorting has been set in the Grid
 */
exports.GRID_SET_SORT = 'GRID_SET_SORT';
/**
 * @ReduxAction Sorting has been cleared in the Grid
 */
exports.GRID_CLEAR_SORT = 'GRID_CLEAR_SORT';
/**
 * @ReduxAction Cells have been selected in the Grid
 */
exports.GRID_SET_SELECTED_CELLS = 'GRID_SET_SELECTED_CELLS';
/**
 * @ReduxAction Rows have been selected in the Grid
 */
exports.GRID_SET_SELECTED_ROWS = 'GRID_SET_SELECTED_ROWS';
/**
 * @ReduxAction Cell Summary Information has been created
 */
exports.GRID_CREATE_CELLS_SUMMARY = 'GRID_CREATE_CELLS_SUMMARY';
/**
 * @ReduxAction Grid Cell summary is set
 */
exports.GRID_SET_CELLS_SUMMARY = 'GRID_SET_CELLS_SUMMARY';
/**
 * @ReduxAction Grid cells have been refreshed
 */
exports.GRID_REFRESH_CELLS = 'GRID_REFRESH_CELLS';
/**
 * @ReduxAction Module Dropdown menu items have been set
 */
exports.GRID_SET_SETTINGS_PANEL_ITEMS = 'GRID_SET_SETTINGS_PANEL_ITEMS';
/**
 * @ReduxAction Module Button menu items have been set
 */
exports.GRID_SET_MENU_MODULE_ITEMS = 'GRID_SET_MENU_MODULE_ITEMS';
/**
 * @ReduxAction Grid is put in pivot mode
 */
exports.GRID_SET_PIVOT_MODE_ON = 'GRID_SET_PIVOT_MODE_ON';
/**
 * @ReduxAction Grid is taken out of pivot mode
 */
exports.GRID_SET_PIVOT_MODE_OFF = 'GRID_SET_PIVOT_MODE_OFF';
/**
 * @ReduxAction Grid is put in tree mode
 */
exports.GRID_SET_TREE_MODE_ON = 'GRID_SET_TREE_MODE_ON';
/**
 * @ReduxAction Grid is taken out of tree mode
 */
exports.GRID_SET_TREE_MODE_OFF = 'GRID_SET_TREE_MODE_OFF';
exports.LAYOUT_UPDATE_CURRENT_DRAFT = 'LAYOUT_UPDATE_CURRENT_DRAFT';
exports.LAYOUT_DRAFT_COLUMN_FILTER_ADD = 'LAYOUT_DRAFT_COLUMN_FILTER_ADD';
exports.LAYOUT_DRAFT_COLUMN_FILTER_EDIT = 'LAYOUT_DRAFT_COLUMN_FILTER_EDIT';
exports.LAYOUT_DRAFT_COLUMN_FILTER_SET = 'LAYOUT_DRAFT_COLUMN_FILTER_SET';
exports.LAYOUT_DRAFT_COLUMN_FILTER_CLEAR_ALL = 'LAYOUT_DRAFT_COLUMN_FILTER_CLEAR_ALL';
exports.LAYOUT_DRAFT_COLUMN_FILTER_CLEAR = 'LAYOUT_DRAFT_COLUMN_FILTER_CLEAR';
exports.LAYOUT_DRAFT_COLUMN_FILTER_SUSPEND = 'LAYOUT_DRAFT_COLUMN_FILTER_SUSPEND';
exports.LAYOUT_DRAFT_COLUMN_FILTER_SUSPEND_ALL = 'LAYOUT_DRAFT_COLUMN_FILTER_SUSPEND_ALL';
exports.LAYOUT_DRAFT_COLUMN_FILTER_UNSUSPEND = 'LAYOUT_DRAFT_COLUMN_FILTER_UNSUSPEND';
exports.LAYOUT_DRAFT_COLUMN_FILTER_UNSUSPEND_ALL = 'LAYOUT_DRAFT_COLUMN_FILTER_UNSUSPEND_ALL';
exports.LAYOUT_DRAFT_GRID_FILTER_SET = 'LAYOUT_DRAFT_GRID_FILTER_SET';
const GridSetColumns = (Columns) => ({
    type: exports.GRID_SET_COLUMNS,
    columns: Columns,
});
exports.GridSetColumns = GridSetColumns;
const GridAddColumn = (Column) => ({
    type: exports.GRID_ADD_COLUMN,
    column: Column,
});
exports.GridAddColumn = GridAddColumn;
const GridAddColumns = (Columns) => ({
    type: exports.GRID_ADD_COLUMNS,
    columns: Columns,
});
exports.GridAddColumns = GridAddColumns;
const GridRemoveColumn = (Column) => ({
    type: exports.GRID_REMOVE_COLUMN,
    column: Column,
});
exports.GridRemoveColumn = GridRemoveColumn;
const GridEditColumn = (Column) => ({
    type: exports.GRID_EDIT_COLUMN,
    column: Column,
});
exports.GridEditColumn = GridEditColumn;
const GridSetSort = (ColumnSorts) => ({
    type: exports.GRID_SET_SORT,
    columnSorts: ColumnSorts,
});
exports.GridSetSort = GridSetSort;
const GridClearSort = () => ({
    type: exports.GRID_CLEAR_SORT,
});
exports.GridClearSort = GridClearSort;
const GridSetSelectedCells = (SelectedCellInfo) => ({
    type: exports.GRID_SET_SELECTED_CELLS,
    selectedCellInfo: SelectedCellInfo,
});
exports.GridSetSelectedCells = GridSetSelectedCells;
const GridSetSelectedRows = (SelectedRowInfo) => ({
    type: exports.GRID_SET_SELECTED_ROWS,
    selectedRowInfo: SelectedRowInfo,
});
exports.GridSetSelectedRows = GridSetSelectedRows;
const GridCreateCellSummary = () => ({
    type: exports.GRID_CREATE_CELLS_SUMMARY,
});
exports.GridCreateCellSummary = GridCreateCellSummary;
const GridSetCellSummary = (CellSummary) => ({
    type: exports.GRID_SET_CELLS_SUMMARY,
    cellSummary: CellSummary,
});
exports.GridSetCellSummary = GridSetCellSummary;
const GridRefreshCells = (rowNodes, columnIds) => ({
    type: exports.GRID_REFRESH_CELLS,
    rowNodes,
    columnIds,
});
exports.GridRefreshCells = GridRefreshCells;
const SetPivotModeOn = () => ({
    type: exports.GRID_SET_PIVOT_MODE_ON,
});
exports.SetPivotModeOn = SetPivotModeOn;
const SetPivotModeOff = () => ({
    type: exports.GRID_SET_PIVOT_MODE_OFF,
});
exports.SetPivotModeOff = SetPivotModeOff;
const SetTreeModeOn = () => ({
    type: exports.GRID_SET_TREE_MODE_ON,
});
exports.SetTreeModeOn = SetTreeModeOn;
const SetTreeModeOff = () => ({
    type: exports.GRID_SET_TREE_MODE_OFF,
});
exports.SetTreeModeOff = SetTreeModeOff;
const setSettingsPanelItems = (MenuItems) => ({
    type: exports.GRID_SET_SETTINGS_PANEL_ITEMS,
    menuItems: MenuItems,
});
exports.setSettingsPanelItems = setSettingsPanelItems;
const SetMenuModuleItems = (MenuItems) => ({
    type: exports.GRID_SET_MENU_MODULE_ITEMS,
    menuItems: MenuItems,
});
exports.SetMenuModuleItems = SetMenuModuleItems;
const LayoutUpdateCurrentDraft = (layout) => ({
    type: exports.LAYOUT_UPDATE_CURRENT_DRAFT,
    layout,
});
exports.LayoutUpdateCurrentDraft = LayoutUpdateCurrentDraft;
const LayoutDraftColumnFilterAdd = (columnFilter) => ({
    type: exports.LAYOUT_DRAFT_COLUMN_FILTER_ADD,
    columnFilter,
});
exports.LayoutDraftColumnFilterAdd = LayoutDraftColumnFilterAdd;
const LayoutDraftColumnFilterEdit = (columnFilter) => ({
    type: exports.LAYOUT_DRAFT_COLUMN_FILTER_EDIT,
    columnFilter,
});
exports.LayoutDraftColumnFilterEdit = LayoutDraftColumnFilterEdit;
const LayoutDraftColumnFilterSet = (columnFilter) => ({
    type: exports.LAYOUT_DRAFT_COLUMN_FILTER_SET,
    columnFilter,
});
exports.LayoutDraftColumnFilterSet = LayoutDraftColumnFilterSet;
const LayoutDraftColumnFilterClear = (columnFilter) => ({
    type: exports.LAYOUT_DRAFT_COLUMN_FILTER_CLEAR,
    columnFilter,
});
exports.LayoutDraftColumnFilterClear = LayoutDraftColumnFilterClear;
const LayoutDraftColumnFilterClearAll = () => ({
    type: exports.LAYOUT_DRAFT_COLUMN_FILTER_CLEAR_ALL,
});
exports.LayoutDraftColumnFilterClearAll = LayoutDraftColumnFilterClearAll;
const LayoutDraftColumnFilterSuspend = (columnFilter) => ({
    type: exports.LAYOUT_DRAFT_COLUMN_FILTER_SUSPEND,
    columnFilter,
});
exports.LayoutDraftColumnFilterSuspend = LayoutDraftColumnFilterSuspend;
const LayoutDraftColumnFilterSuspendAll = () => ({
    type: exports.LAYOUT_DRAFT_COLUMN_FILTER_SUSPEND_ALL,
});
exports.LayoutDraftColumnFilterSuspendAll = LayoutDraftColumnFilterSuspendAll;
const LayoutDraftColumnFilterUnsuspend = (columnFilter) => ({
    type: exports.LAYOUT_DRAFT_COLUMN_FILTER_UNSUSPEND,
    columnFilter,
});
exports.LayoutDraftColumnFilterUnsuspend = LayoutDraftColumnFilterUnsuspend;
const LayoutDraftColumnFilterUnsuspendAll = () => ({
    type: exports.LAYOUT_DRAFT_COLUMN_FILTER_UNSUSPEND_ALL,
});
exports.LayoutDraftColumnFilterUnsuspendAll = LayoutDraftColumnFilterUnsuspendAll;
const LayoutDraftGridFilterSet = (gridFilter) => ({
    type: exports.LAYOUT_DRAFT_GRID_FILTER_SET,
    gridFilter,
});
exports.LayoutDraftGridFilterSet = LayoutDraftGridFilterSet;
const initialState = {
    Columns: GeneralConstants_1.EMPTY_ARRAY,
    CurrentLayout: null,
    ColumnSorts: GeneralConstants_1.EMPTY_ARRAY,
    SelectedCellInfo: null,
    SelectedRowInfo: null,
    CellSummary: null,
    SettingPanelModuleMenuItems: GeneralConstants_1.EMPTY_ARRAY,
    ModuleButtonMenuItems: GeneralConstants_1.EMPTY_ARRAY,
    IsGridInPivotMode: false,
    IsGridInTreeMode: false,
};
const GridReducer = (state = initialState, action) => {
    var _a, _b, _c, _d, _e, _f, _g, _h;
    switch (action.type) {
        case exports.GRID_SET_COLUMNS:
            return Object.assign({}, state, {
                Columns: [].concat(action.columns),
            });
        case exports.GRID_ADD_COLUMN:
            const actionTypedAdd = action;
            let columns = [].concat(state.Columns);
            columns.push(actionTypedAdd.column);
            return Object.assign({}, state, { Columns: columns });
        case exports.GRID_ADD_COLUMNS:
            const actionTypedAddMany = action;
            return Object.assign({}, state, {
                Columns: [...state.Columns, ...actionTypedAddMany.columns],
            });
        case exports.GRID_EDIT_COLUMN:
            const actionGridColumn = action.column;
            return Object.assign(Object.assign({}, state), { Columns: state.Columns.map((abObject) => abObject.Uuid === actionGridColumn.Uuid ? actionGridColumn : abObject) });
        case exports.GRID_REMOVE_COLUMN:
            const removeColumn = action.column;
            return Object.assign(Object.assign({}, state), { Columns: state.Columns.filter((abObject) => abObject.Uuid !== removeColumn.Uuid) });
        case exports.GRID_SET_SORT:
            return Object.assign({}, state, {
                ColumnSorts: action.columnSorts,
            });
        case exports.GRID_CLEAR_SORT:
            return Object.assign({}, state, { ColumnSorts: undefined });
        case exports.GRID_SET_SELECTED_CELLS:
            return Object.assign({}, state, {
                SelectedCellInfo: action.selectedCellInfo,
            });
        case exports.GRID_SET_SELECTED_ROWS:
            return Object.assign({}, state, {
                SelectedRowInfo: action.selectedRowInfo,
            });
        case exports.GRID_SET_CELLS_SUMMARY:
            return Object.assign({}, state, {
                CellSummary: action.cellSummary,
            });
        case exports.GRID_SET_SETTINGS_PANEL_ITEMS: {
            const actionTyped = action;
            const menuItems = actionTyped.menuItems.sort((a, b) => a.label < b.label ? -1 : a.label > b.label ? 1 : 0);
            return Object.assign({}, state, { SettingPanelModuleMenuItems: menuItems });
        }
        case exports.GRID_SET_MENU_MODULE_ITEMS: {
            const actionTyped = action;
            const menuItems = actionTyped.menuItems.sort((a, b) => a.label < b.label ? -1 : a.label > b.label ? 1 : 0);
            return Object.assign({}, state, { ModuleButtonMenuItems: menuItems });
        }
        case exports.LAYOUT_UPDATE_CURRENT_DRAFT: {
            const currentDraftLayout = action.layout;
            return Object.assign({}, state, {
                CurrentLayout: currentDraftLayout,
            });
        }
        case exports.LAYOUT_DRAFT_COLUMN_FILTER_SET: {
            const currentDraftLayout = state.CurrentLayout;
            if (currentDraftLayout) {
                let columnFilters = (_a = currentDraftLayout.ColumnFilters) !== null && _a !== void 0 ? _a : [];
                const columnFilterAction = action
                    .columnFilter;
                AdaptableHelper_1.default.addUuidAndSource(columnFilterAction);
                columnFilters = columnFilters
                    .filter((colFilter) => {
                    return colFilter.ColumnId !== columnFilterAction.ColumnId;
                })
                    .concat(columnFilterAction);
                return Object.assign({}, state, {
                    CurrentLayout: Object.assign(Object.assign({}, currentDraftLayout), { ColumnFilters: columnFilters }),
                });
            }
        }
        case exports.LAYOUT_DRAFT_COLUMN_FILTER_ADD: {
            const currentDraftLayout = state.CurrentLayout;
            if (currentDraftLayout) {
                let columnFilters = [...((_b = currentDraftLayout.ColumnFilters) !== null && _b !== void 0 ? _b : [])];
                const columnFilterAction = action
                    .columnFilter;
                AdaptableHelper_1.default.addUuidAndSource(columnFilterAction);
                columnFilters.push(columnFilterAction);
                return Object.assign({}, state, {
                    CurrentLayout: Object.assign(Object.assign({}, currentDraftLayout), { ColumnFilters: columnFilters }),
                });
            }
        }
        case exports.LAYOUT_DRAFT_COLUMN_FILTER_EDIT: {
            const currentDraftLayout = state.CurrentLayout;
            if (currentDraftLayout) {
                const columnFilterAction = action
                    .columnFilter;
                let columnFilters = ((_c = currentDraftLayout.ColumnFilters) !== null && _c !== void 0 ? _c : []).map((abObject) => abObject.Uuid === columnFilterAction.Uuid ? columnFilterAction : abObject);
                return Object.assign({}, state, {
                    CurrentLayout: Object.assign(Object.assign({}, currentDraftLayout), { ColumnFilters: columnFilters }),
                });
            }
        }
        case exports.LAYOUT_DRAFT_COLUMN_FILTER_CLEAR: {
            const currentDraftLayout = state.CurrentLayout;
            if (currentDraftLayout) {
                let columnFilters = [...((_d = currentDraftLayout.ColumnFilters) !== null && _d !== void 0 ? _d : [])];
                const actionTypedDelete = action;
                const index = actionTypedDelete.columnFilter
                    ? columnFilters.findIndex((i) => i.Uuid == actionTypedDelete.columnFilter.Uuid)
                    : -1;
                if (index != -1) {
                    columnFilters.splice(index, 1);
                }
                return Object.assign({}, state, {
                    CurrentLayout: Object.assign(Object.assign({}, currentDraftLayout), { ColumnFilters: columnFilters }),
                });
            }
        }
        case exports.LAYOUT_DRAFT_COLUMN_FILTER_CLEAR_ALL: {
            const currentDraftLayout = state.CurrentLayout;
            if (currentDraftLayout) {
                return Object.assign({}, state, {
                    CurrentLayout: Object.assign(Object.assign({}, currentDraftLayout), { ColumnFilters: [] }),
                });
            }
        }
        case exports.LAYOUT_DRAFT_COLUMN_FILTER_SUSPEND: {
            const currentDraftLayout = state.CurrentLayout;
            if (currentDraftLayout) {
                const columnFilterAction = action
                    .columnFilter;
                let columnFilters = ((_e = currentDraftLayout.ColumnFilters) !== null && _e !== void 0 ? _e : []).map((abObject) => abObject.Uuid === columnFilterAction.Uuid ? Object.assign(Object.assign({}, abObject), { IsSuspended: true }) : abObject);
                return Object.assign({}, state, {
                    CurrentLayout: Object.assign(Object.assign({}, currentDraftLayout), { ColumnFilters: columnFilters }),
                });
            }
        }
        case exports.LAYOUT_DRAFT_COLUMN_FILTER_SUSPEND_ALL: {
            const currentDraftLayout = state.CurrentLayout;
            if (currentDraftLayout) {
                let columnFilters = ((_f = currentDraftLayout.ColumnFilters) !== null && _f !== void 0 ? _f : []).map((abObject) => (Object.assign(Object.assign({}, abObject), { IsSuspended: true })));
                return Object.assign({}, state, {
                    CurrentLayout: Object.assign(Object.assign({}, currentDraftLayout), { ColumnFilters: columnFilters }),
                });
            }
        }
        case exports.LAYOUT_DRAFT_COLUMN_FILTER_UNSUSPEND: {
            const currentDraftLayout = state.CurrentLayout;
            if (currentDraftLayout) {
                const columnFilterAction = action
                    .columnFilter;
                let columnFilters = ((_g = currentDraftLayout.ColumnFilters) !== null && _g !== void 0 ? _g : []).map((abObject) => abObject.Uuid === columnFilterAction.Uuid ? Object.assign(Object.assign({}, abObject), { IsSuspended: false }) : abObject);
                return Object.assign({}, state, {
                    CurrentLayout: Object.assign(Object.assign({}, currentDraftLayout), { ColumnFilters: columnFilters }),
                });
            }
        }
        case exports.LAYOUT_DRAFT_COLUMN_FILTER_UNSUSPEND_ALL: {
            const currentDraftLayout = state.CurrentLayout;
            if (currentDraftLayout) {
                let columnFilters = ((_h = currentDraftLayout.ColumnFilters) !== null && _h !== void 0 ? _h : []).map((abObject) => (Object.assign(Object.assign({}, abObject), { IsSuspended: false })));
                return Object.assign({}, state, {
                    CurrentLayout: Object.assign(Object.assign({}, currentDraftLayout), { ColumnFilters: columnFilters }),
                });
            }
        }
        case exports.LAYOUT_DRAFT_GRID_FILTER_SET: {
            const currentDraftLayout = state.CurrentLayout;
            if (currentDraftLayout) {
                const gridFilter = action.gridFilter;
                const CurrentLayout = Object.assign(Object.assign({}, currentDraftLayout), { GridFilter: gridFilter });
                if (!gridFilter) {
                    delete CurrentLayout.GridFilter;
                }
                return Object.assign({}, state, {
                    CurrentLayout,
                });
            }
        }
        case LayoutRedux_1.LAYOUT_SELECT: {
            return Object.assign({}, state, {
                CurrentLayout: null,
            });
        }
        case exports.GRID_SET_PIVOT_MODE_ON:
            return Object.assign({}, state, { IsGridInPivotMode: true });
        case exports.GRID_SET_PIVOT_MODE_OFF:
            return Object.assign({}, state, { IsGridInPivotMode: false });
        case exports.GRID_SET_TREE_MODE_ON:
            return Object.assign({}, state, { IsGridInTreeMode: true });
        case exports.GRID_SET_TREE_MODE_OFF:
            return Object.assign({}, state, { IsGridInTreeMode: false });
        default:
            return state;
    }
};
exports.GridReducer = GridReducer;
