import { EMPTY_ARRAY, EMPTY_STRING } from '../../Utilities/Constants/GeneralConstants';
import { CELL_SUMMARY_DEFAULT_OPERATION } from '../../Utilities/Constants/ReduxConstants';
import { MathOperation } from '../../AdaptableState/Common/Enums';
import { cloneObject } from '../../Utilities/Helpers/Helper';
import { AnnotationsService } from '../../Utilities/Services/AnnotationsService';
/*
Bit of a mixed bag of actions but essentially its those that are related to Strategies but where we DONT want to persist state
This allows us to keep the other reducers pure in terms of everything persists
*/
/*
ACTION NAMES
*/
// Column
export const SET_COLUMNS = 'SET_COLUMNS';
// Selection
export const SET_SELECTED_CELLS = 'SET_SELECTED_CELLS';
export const SET_SELECTED_ROWS = 'SET_SELECTED_ROWS';
// UI
export const SET_SETTINGS_PANEL_ITEMS = 'SET_SETTINGS_PANEL_ITEMS';
export const SET_DASHBOARD_MODULE_BUTTONS = 'SET_DASHBOARD_MODULE_BUTTONS';
// Cell Highlight
export const HIGHLIGHT_CELL_ADD = 'HIGHLIGHT_CELL_ADD';
export const HIGHLIGHT_CELL_DELETE = 'HIGHLIGHT_CELL_DELETE';
export const HIGHLIGHT_CELL_DELETE_ALL = 'HIGHLIGHT_CELL_DELETE_ALL';
// Row Highlight
export const HIGHLIGHT_ROW_ADD = 'HIGHLIGHT_ROW_ADD';
export const HIGHLIGHT_ROWS_ADD = 'HIGHLIGHT_ROWS_ADD';
export const HIGHLIGHT_ROW_DELETE = 'HIGHLIGHT_ROW_DELETE';
export const HIGHLIGHT_ROWS_DELETE = 'HIGHLIGHT_ROWS_DELETE';
export const HIGHLIGHT_ROW_DELETE_ALL = 'HIGHLIGHT_ROW_DELETE_ALL';
// Alerts
export const ALERT_ADD = 'ALERT_ADD';
export const ALERT_DELETE = 'ALERT_DELETE';
export const ALERT_DELETE_ALL = 'ALERT_DELETE_ALL';
export const ALERT_REMOVE_CELL_HIGHLIGHT = 'ALERT_REMOVE_CELL_HIGHLIGHT';
export const ALERT_REMOVE_ROW_HIGHLIGHT = 'ALERT_REMOVE_ROW_HIGHLIGHT';
// Status Message
export const SYSTEM_STATUS_MESSAGE_INFO_ADD = 'SYSTEM_STATUS_MESSAGE_INFO_ADD';
export const SYSTEM_STATUS_MESSAGE_INFO_DELETE = 'SYSTEM_STATUS_MESSAGE_INFO_DELETE';
export const SYSTEM_STATUS_MESSAGE_INFO_DELETE_ALL = 'SYSTEM_STATUS_MESSAGE_INFO_DELETE_ALL';
// Smart Edit
export const SMARTEDIT_CHECK_CELL_SELECTION = 'SMARTEDIT_CHECK_CELL_SELECTION';
export const SMARTEDIT_FETCH_PREVIEW = 'SMARTEDIT_FETCH_PREVIEW';
export const SMARTEDIT_SET_VALID_SELECTION = 'SMARTEDIT_SET_VALID_SELECTION';
export const SMARTEDIT_SET_PREVIEW = 'SMARTEDIT_SET_PREVIEW';
export const SMART_EDIT_CHANGE_VALUE = 'SMART_EDIT_CHANGE_VALUE';
export const SMART_EDIT_CHANGE_OPERATION = 'SMART_EDIT_CHANGE_OPERATION';
// Bulk Update
export const BULK_UPDATE_CHECK_CELL_SELECTION = 'BULK_UPDATE_CHECK_CELL_SELECTION';
export const BULK_UPDATE_SET_VALID_SELECTION = 'BULK_UPDATE_SET_VALID_SELECTION';
export const BULK_UPDATE_SET_PREVIEW = 'BULK_UPDATE_SET_PREVIEW';
export const BULK_UPDATE_CHANGE_VALUE = 'BULK_UPDATE_CHANGE_VALUE';
// Cached Queries
export const CACHED_QUERY_ADD = 'CACHED_QUERY_ADD';
// Cell Summary
export const CELL_SUMMARY_CHANGE_OPERATION = 'CELL_SUMMARY_CHANGE_OPERATION';
export const SET_CELL_SUMMARY_INFO = 'SET_CELL_SUMMARY_INFO';
export const CREATE_CELL_SUMMARY_INFO = 'CREATE_CELL_SUMMARY_INFO';
// License
export const LICENSE_SHOW_WATERMARK = 'LICENSE_SHOW_WATERMARK';
export const LICENSE_DISABLE_PERSISTENCE = 'LICENSE_DISABLE_PERSISTENCE';
// Data Change History
export const DATA_CHANGE_HISTORY_ADD = 'DATA_CHANGE_HISTORY_ADD';
export const DATA_CHANGE_HISTORY_UNDO = 'DATA_CHANGE_HISTORY_UNDO';
export const DATA_CHANGE_HISTORY_CLEAR_ROW = 'DATA_CHANGE_HISTORY_CLEAR_ROW';
export const DATA_CHANGE_HISTORY_ENABLE = 'DATA_CHANGE_HISTORY_ENABLE';
export const DATA_CHANGE_HISTORY_DISABLE = 'DATA_CHANGE_HISTORY_DISABLE';
export const DATA_CHANGE_HISTORY_SUSPEND = 'DATA_CHANGE_HISTORY_SUSPEND';
export const DATA_CHANGE_HISTORY_RESUME = 'DATA_CHANGE_HISTORY_RESUME';
// Settings Panel
export const SETTINGS_PANEL_SET = 'SETTINGS_PANEL_SET';
// Quick Filter
export const QUICK_FILTER_BAR_SHOW = 'QUICK_FILTER_BAR_SHOW';
export const QUICK_FILTER_BAR_HIDE = 'QUICK_FILTER_BAR_HIDE';
// Delete warning
export const DISABLE_DELETE_CONFIRMATION = 'DISABLE_DELETE_CONFIRMATION';
// Layout
export const LAYOUT_SHOW_NOT_ASSOCIATED_OBJECTS = 'LAYOUT_SHOW_NOT_ASSOCIATED_OBJECTS';
export const DASHBOARD_REFRESH = 'DASHBOARD_REFRESH';
// DataSet
export const DATA_SET_SELECT = 'DATA_SET_SELECT';
// Charting
export const CHARTING_SET_CURRENT_CHART_MODELS = 'CHARTING_SET_CURRENT_CHART_MODELS';
// Quick Search
export const SET_QUICK_SEARCH_FLOATING_VISIBILITY = 'SET_QUICK_SEARCH_FLOATING_VISIBILITY';
// Data Export (Report)
export const SYSTEM_EXPORT_BEGIN = 'SYSTEM_EXPORT_BEGIN';
export const SYSTEM_EXPORT_END = 'SYSTEM_EXPORT_END';
// Notes and Comments
export const CELL_POPUP_SHOW = 'CELL_POPUP_SHOW';
export const CELL_POPUP_HIDE = 'CELL_POPUP_HIDE';
export const CELL_POPUP_EDIT_FOCUSED_ENTITY = 'CELL_POPUP_EDIT_FOCUSED_ENTITY';
// Row Summaries
export const SUMMARY_ROW_SET = 'SUMMARY_ROW_SET';
// Data Import
export const DATA_IMPORT_COMPLETED = 'DATA_IMPORT_COMPLETED';
export const HighlightCellAdd = (cellHighlightInfo) => ({
    type: HIGHLIGHT_CELL_ADD,
    cellHighlightInfo: cellHighlightInfo,
});
export const HighlightCellDelete = (primaryKeyValue, columnId) => ({
    type: HIGHLIGHT_CELL_DELETE,
    primaryKeyValue,
    columnId,
});
export const HighlightCellDeleteAll = () => ({
    type: HIGHLIGHT_CELL_DELETE_ALL,
});
export const HighlightRowAdd = (rowHighlightInfo) => ({
    type: HIGHLIGHT_ROW_ADD,
    rowHighlightInfo: rowHighlightInfo,
});
export const HighlightRowsAdd = (rowsHighlightInfo) => ({
    type: HIGHLIGHT_ROWS_ADD,
    rowsHighlightInfo: rowsHighlightInfo,
});
export const HighlightRowDelete = (primaryKeyValue) => ({
    type: HIGHLIGHT_ROW_DELETE,
    primaryKeyValue: primaryKeyValue,
});
export const GridHighlightRowsDelete = (primaryKeyValues) => ({
    type: HIGHLIGHT_ROWS_DELETE,
    primaryKeyValues: primaryKeyValues,
});
export const GridHighlightRowDeleteAll = () => ({
    type: HIGHLIGHT_ROW_DELETE_ALL,
});
export const SetColumns = (Columns) => ({
    type: SET_COLUMNS,
    columns: Columns,
});
export const SetSelectedCells = (SelectedCellInfo) => ({
    type: SET_SELECTED_CELLS,
    selectedCellInfo: SelectedCellInfo,
});
export const SetSelectedRows = (SelectedRowInfo) => ({
    type: SET_SELECTED_ROWS,
    selectedRowInfo: SelectedRowInfo,
});
export const SetSettingsPanelItems = (MenuItems) => ({
    type: SET_SETTINGS_PANEL_ITEMS,
    menuItems: MenuItems,
});
export const SetDasboardModuleButtons = (MenuItems) => ({
    type: SET_DASHBOARD_MODULE_BUTTONS,
    menuItems: MenuItems,
});
/*
Public Functions
*/
export const AdaptableAlertAdd = (alert, maxAlerts) => ({
    type: ALERT_ADD,
    alert: alert,
    maxAlerts: maxAlerts,
});
export const AdaptableAlertDelete = (alert) => ({
    type: ALERT_DELETE,
    alert: alert,
});
export const AdaptableAlertDeleteAll = (alerts) => ({
    type: ALERT_DELETE_ALL,
    alerts: alerts,
});
export const AdaptableAlertRemoveCellHighlight = (alert) => ({
    type: ALERT_REMOVE_CELL_HIGHLIGHT,
    alert: alert,
});
export const AdaptableAlertRemoveRowHighlight = (alert) => ({
    type: ALERT_REMOVE_ROW_HIGHLIGHT,
    alert: alert,
});
export const CreateCellSummaryInfo = () => ({
    type: CREATE_CELL_SUMMARY_INFO,
});
export const SetCellSummaryInfo = (CellSummary) => ({
    type: SET_CELL_SUMMARY_INFO,
    cellSummaryInfo: CellSummary,
});
export const SystemStatusMessageInfoAdd = (SystemStatusMessageInfo, MaxSystemStatusMessagesInStore) => ({
    type: SYSTEM_STATUS_MESSAGE_INFO_ADD,
    systemStatusMessageInfo: SystemStatusMessageInfo,
    maxSystemStatusMessagesInStore: MaxSystemStatusMessagesInStore,
});
export const SystemStatusMessageInfoDelete = (SystemStatusMessageInfo) => ({
    type: SYSTEM_STATUS_MESSAGE_INFO_DELETE,
    systemStatusMessageInfo: SystemStatusMessageInfo,
});
export const SystemStatusMessageInfoDeleteAll = () => ({
    type: SYSTEM_STATUS_MESSAGE_INFO_DELETE_ALL,
});
export const SmartEditChangeValue = (value) => ({
    type: SMART_EDIT_CHANGE_VALUE,
    value,
});
export const SmartEditChangeOperation = (smartEditOperation) => ({
    type: SMART_EDIT_CHANGE_OPERATION,
    smartEditOperation,
});
export const SmartEditCheckCellSelection = () => ({
    type: SMARTEDIT_CHECK_CELL_SELECTION,
});
export const SmartEditSetValidSelection = (IsValidSmartEditSelection) => ({
    type: SMARTEDIT_SET_VALID_SELECTION,
    isValidSmartEditSelection: IsValidSmartEditSelection,
});
export const SmartEditSetPreview = (SmartEditPreviewInfo) => ({
    type: SMARTEDIT_SET_PREVIEW,
    smartEditPreviewInfo: SmartEditPreviewInfo,
});
export const BulkUpdateCheckCellSelection = () => ({
    type: BULK_UPDATE_CHECK_CELL_SELECTION,
});
export const BulkUpdateSetValidSelection = (bulkUpdateValidationResult) => ({
    type: BULK_UPDATE_SET_VALID_SELECTION,
    bulkUpdateValidationResult,
});
export const BulkUpdateSetPreview = (BulkUpdatePreviewInfo) => ({
    type: BULK_UPDATE_SET_PREVIEW,
    bulkUpdatePreviewInfo: BulkUpdatePreviewInfo,
});
export const BulkUpdateChangeValue = (bulkUpdateValue) => ({
    type: BULK_UPDATE_CHANGE_VALUE,
    bulkUpdateValue,
});
export const CachedQueryAdd = (cachedQuery) => ({
    type: CACHED_QUERY_ADD,
    cachedQuery,
});
export const CellSummaryChangeOperation = (cellSummaryOperation) => ({
    type: CELL_SUMMARY_CHANGE_OPERATION,
    cellSummaryOperation: cellSummaryOperation,
});
export const LicenseShowWatermark = (text) => ({
    type: LICENSE_SHOW_WATERMARK,
    text,
});
export const LicenseDisablePersistence = () => ({
    type: LICENSE_DISABLE_PERSISTENCE,
});
export const DataChangeHistoryAdd = (changeInfo, uniqueKey) => ({
    type: DATA_CHANGE_HISTORY_ADD,
    changeInfo,
    uniqueKey,
});
export const DataChangeHistoryUndo = (changeInfo, uniqueKey) => ({
    type: DATA_CHANGE_HISTORY_UNDO,
    changeInfo,
    uniqueKey,
});
export const DataChangeHistoryClearRow = (changeInfo, uniqueKey) => ({
    type: DATA_CHANGE_HISTORY_CLEAR_ROW,
    changeInfo,
    uniqueKey,
});
export const DataChangeHistoryEnable = () => ({
    type: DATA_CHANGE_HISTORY_ENABLE,
});
export const DataChangeHistoryDisable = () => ({
    type: DATA_CHANGE_HISTORY_DISABLE,
});
export const DataChangeHistorySuspend = () => ({
    type: DATA_CHANGE_HISTORY_SUSPEND,
});
export const DataChangeHistoryResume = () => ({
    type: DATA_CHANGE_HISTORY_RESUME,
});
export const SettingsPanelSet = (name, settings) => ({
    type: SETTINGS_PANEL_SET,
    name,
    settings,
});
export const QuickFilterBarShow = () => ({
    type: QUICK_FILTER_BAR_SHOW,
});
export const QuickFilterBarHide = () => ({
    type: QUICK_FILTER_BAR_HIDE,
});
export const LayoutShowNotAssociatedObjects = (showNotAssociatedObjects) => ({
    type: LAYOUT_SHOW_NOT_ASSOCIATED_OBJECTS,
    showNotAssociatedObjects,
});
export const DashboardRefresh = () => ({
    type: DASHBOARD_REFRESH,
});
export const DataSetSelect = (dataSet) => ({
    type: DATA_SET_SELECT,
    dataSet: dataSet,
});
export const ChartingSetCurrentChartModels = (chartModels) => ({
    type: CHARTING_SET_CURRENT_CHART_MODELS,
    chartModels,
});
export const ChartingCurrentChartModelsSelector = (state) => {
    return state.Charting.currentChartModels;
};
export const DisableDeleteConfirmation = () => ({
    type: DISABLE_DELETE_CONFIRMATION,
});
export const SystemExportBegin = (reportName, reportFormat, exportDestination) => ({
    type: SYSTEM_EXPORT_BEGIN,
    reportName,
    reportFormat,
    exportDestination,
});
export const SystemExportEnd = () => ({
    type: SYSTEM_EXPORT_END,
});
export const QuickSearchFloatingVisibility = (visible) => ({
    type: SET_QUICK_SEARCH_FLOATING_VISIBILITY,
    visible,
});
export const CellPopupShow = (cellPosition, editMode) => ({
    type: CELL_POPUP_SHOW,
    editMode,
    cellPosition,
});
export const CellPopupEditFocusedEntity = (focusedEntity) => ({
    type: CELL_POPUP_EDIT_FOCUSED_ENTITY,
    focusedEntity,
});
export const RowSummarySet = (rowSummaries) => ({
    type: SUMMARY_ROW_SET,
    rowSummaries: rowSummaries,
});
export const CellPopupHide = () => ({
    type: CELL_POPUP_HIDE,
});
export const DisableDeleteConfirmationSelector = (state) => {
    return state.DisableDeleteConfirmation;
};
export const QuickSearchFloatingVisibilitySelector = (state) => {
    return state?.QuickSearch?.floatingVisible;
};
export const CommentsAndNotesSelector = (state) => {
    return state?.NotesAndComments?.popupPosition;
};
export const CommentsAndNotesEditModeSelector = (state) => {
    return state?.NotesAndComments?.editMode;
};
export const CommentsAndNotesFocusedEntitySelector = (state) => {
    return state?.NotesAndComments?.focusedEntity;
};
export const DataImportCompleted = (dataImportedInfo) => ({
    type: DATA_IMPORT_COMPLETED,
    dataImportedInfo: dataImportedInfo,
});
const initialState = {
    Columns: EMPTY_ARRAY,
    SelectedCellInfo: null,
    SelectedRowInfo: null,
    CellHighlightInfo: EMPTY_ARRAY,
    RowHighlightInfo: EMPTY_ARRAY,
    SettingsPanelModuleEntries: EMPTY_ARRAY,
    AdaptableAlerts: EMPTY_ARRAY,
    SystemStatusMessages: EMPTY_ARRAY,
    Dashboard: { DashboardModuleButtons: EMPTY_ARRAY, DashboardRevision: 1 },
    SmartEdit: {
        IsValidSmartEditSelection: false,
        SmartEditPreviewInfo: null,
        SmartEditValue: 1,
        SmartEditOperation: MathOperation.Multiply,
    },
    BulkUpdate: {
        BulkUpdateValidationResult: { IsValid: false, Column: null },
        BulkUpdatePreviewInfo: null,
        BulkUpdateValue: EMPTY_STRING,
    },
    CachedQueries: EMPTY_ARRAY,
    CellSummary: {
        CellSummaryOperation: CELL_SUMMARY_DEFAULT_OPERATION,
        CellSummaryInfo: null,
    },
    License: {
        watermark: { show: false, text: null },
        disablePersistence: false,
    },
    DataChangeHistory: {
        logs: {},
        currentMode: 'INACTIVE',
        enableTime: null,
        suspendTime: null,
    },
    SettingsPanel: {},
    IsQuickFilterVisible: true,
    CurrentDataSet: EMPTY_STRING,
    Charting: {
        currentChartModels: [],
        externalChartingDefinitions: [],
    },
    Layout: {
        ShowLayoutNotAssociatedObjects: false,
    },
    DisableDeleteConfirmation: false,
    Export: {
        inProgress: null,
    },
};
export const InternalReducer = (state = initialState, action) => {
    let alerts;
    let systemStatusMessages;
    switch (action.type) {
        case SET_COLUMNS:
            return Object.assign({}, state, {
                Columns: [].concat(action.columns),
            });
        case SET_SELECTED_CELLS:
            return Object.assign({}, state, {
                SelectedCellInfo: action.selectedCellInfo,
            });
        case SET_SELECTED_ROWS:
            return Object.assign({}, state, {
                SelectedRowInfo: action.selectedRowInfo,
            });
        case 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, { SettingsPanelModuleEntries: menuItems });
        }
        case SET_DASHBOARD_MODULE_BUTTONS: {
            const actionTyped = action;
            const menuItems = actionTyped.menuItems.sort((a, b) => a.label < b.label ? -1 : a.label > b.label ? 1 : 0);
            return {
                ...state,
                Dashboard: {
                    ...state.Dashboard,
                    DashboardModuleButtons: menuItems,
                },
            };
        }
        case HIGHLIGHT_CELL_ADD: {
            const actionTypedAdd = action;
            return Object.assign({}, state, {
                CellHighlightInfo: [...state.CellHighlightInfo, actionTypedAdd.cellHighlightInfo],
            });
        }
        case HIGHLIGHT_CELL_DELETE: {
            const actionTypedDelete = action;
            const actionId = `${actionTypedDelete.columnId}-${actionTypedDelete.primaryKeyValue}`;
            return Object.assign({}, state, {
                CellHighlightInfo: state.CellHighlightInfo.filter((celHighlightInfo) => {
                    const id = `${celHighlightInfo.columnId}-${celHighlightInfo.primaryKeyValue}`;
                    return id !== actionId;
                }),
            });
        }
        case HIGHLIGHT_CELL_DELETE_ALL: {
            return Object.assign({}, state, {
                CellHighlightInfo: [],
            });
        }
        case HIGHLIGHT_ROW_ADD: {
            const actionTypedAdd = action;
            return Object.assign({}, state, {
                RowHighlightInfo: [...state.RowHighlightInfo, actionTypedAdd.rowHighlightInfo],
            });
        }
        case HIGHLIGHT_ROWS_ADD: {
            const actionTypedAdd = action;
            const rowHighlightInfos = actionTypedAdd.rowsHighlightInfo.primaryKeyValues.map((primaryKeyValue) => {
                return {
                    primaryKeyValue,
                    highlightStyle: actionTypedAdd.rowsHighlightInfo.highlightStyle,
                    timeout: actionTypedAdd.rowsHighlightInfo.timeout,
                };
            });
            return Object.assign({}, state, {
                RowHighlightInfo: [...state.RowHighlightInfo, ...rowHighlightInfos],
            });
        }
        case HIGHLIGHT_ROW_DELETE: {
            const actionTypedDelete = action;
            const actionId = actionTypedDelete.primaryKeyValue;
            return Object.assign({}, state, {
                RowHighlightInfo: state.RowHighlightInfo.filter((rowHighlightInfo) => {
                    return rowHighlightInfo.primaryKeyValue !== actionId;
                }),
            });
        }
        case HIGHLIGHT_ROWS_DELETE: {
            const actionTypedDelete = action;
            return Object.assign({}, state, {
                RowHighlightInfo: state.RowHighlightInfo.filter((rowHighlightInfo) => {
                    return !actionTypedDelete.primaryKeyValues.includes(rowHighlightInfo.primaryKeyValue);
                }),
            });
        }
        case HIGHLIGHT_ROW_DELETE_ALL: {
            return Object.assign({}, state, {
                RowHighlightInfo: [],
            });
        }
        case ALERT_ADD: {
            const actionTypedAdd = action;
            alerts = [].concat(state.AdaptableAlerts);
            if (alerts.length == actionTypedAdd.maxAlerts) {
                // we have hit the maximum so remove first item (oldest)
                alerts.splice(0, 1);
            }
            alerts.push(actionTypedAdd.alert);
            return Object.assign({}, state, { AdaptableAlerts: alerts });
        }
        case ALERT_DELETE: {
            const adaptableAlert = action.alert;
            return {
                ...state,
                AdaptableAlerts: state.AdaptableAlerts.filter((abObject) => abObject.Uuid !== adaptableAlert.Uuid),
            };
        }
        case ALERT_DELETE_ALL: {
            return Object.assign({}, state, { AdaptableAlerts: [] });
        }
        case ALERT_REMOVE_CELL_HIGHLIGHT: {
            const adaptableAlert = action
                .alert;
            return {
                ...state,
                AdaptableAlerts: state.AdaptableAlerts.map((abObject) => {
                    if (abObject.Uuid === adaptableAlert.Uuid) {
                        // AdaptableAlerts are still linked to the live AlertDefinitions, so we have to decouple them
                        const newAbObject = {
                            ...abObject,
                        };
                        newAbObject.alertDefinition = cloneObject(abObject.alertDefinition);
                        delete newAbObject.alertDefinition.AlertProperties.HighlightCell;
                        return newAbObject;
                    }
                    return abObject;
                }),
            };
        }
        case ALERT_REMOVE_ROW_HIGHLIGHT: {
            const adaptableAlert = action
                .alert;
            return {
                ...state,
                AdaptableAlerts: state.AdaptableAlerts.map((abObject) => {
                    if (abObject.Uuid === adaptableAlert.Uuid) {
                        // AdaptableAlerts are still linked to the live AlertDefinitions, so we have to decouple them
                        const newAbObject = {
                            ...abObject,
                        };
                        newAbObject.alertDefinition = cloneObject(abObject.alertDefinition);
                        delete newAbObject.alertDefinition.AlertProperties.HighlightRow;
                        return newAbObject;
                    }
                    return abObject;
                }),
            };
        }
        case SYSTEM_STATUS_MESSAGE_INFO_ADD: {
            const actionTypedAdd = action;
            systemStatusMessages = [].concat(state.SystemStatusMessages);
            if (systemStatusMessages.length == actionTypedAdd.maxSystemStatusMessagesInStore) {
                // we have hit the maximum so remove first item (oldest)
                systemStatusMessages.splice(0, 1);
            }
            systemStatusMessages.push(actionTypedAdd.systemStatusMessageInfo);
            return Object.assign({}, state, {
                SystemStatusMessages: systemStatusMessages,
            });
        }
        case SYSTEM_STATUS_MESSAGE_INFO_DELETE_ALL: {
            return Object.assign({}, state, { SystemStatusMessages: [] });
        }
        case SMART_EDIT_CHANGE_VALUE:
            return {
                ...state,
                SmartEdit: {
                    ...state.SmartEdit,
                    SmartEditValue: action.value,
                },
            };
        case SMART_EDIT_CHANGE_OPERATION:
            return {
                ...state,
                SmartEdit: {
                    ...state.SmartEdit,
                    SmartEditOperation: action.smartEditOperation,
                },
            };
        case SMARTEDIT_SET_VALID_SELECTION:
            return {
                ...state,
                SmartEdit: {
                    ...state.SmartEdit,
                    IsValidSmartEditSelection: action
                        .isValidSmartEditSelection,
                },
            };
        case SMARTEDIT_SET_PREVIEW:
            return {
                ...state,
                SmartEdit: {
                    ...state.SmartEdit,
                    SmartEditPreviewInfo: action.smartEditPreviewInfo,
                },
            };
        case BULK_UPDATE_SET_VALID_SELECTION:
            return {
                ...state,
                BulkUpdate: {
                    ...state.BulkUpdate,
                    BulkUpdateValidationResult: action
                        .bulkUpdateValidationResult,
                },
            };
        case BULK_UPDATE_SET_PREVIEW:
            return {
                ...state,
                BulkUpdate: {
                    ...state.BulkUpdate,
                    BulkUpdatePreviewInfo: action.bulkUpdatePreviewInfo,
                },
            };
        case BULK_UPDATE_CHANGE_VALUE:
            return {
                ...state,
                BulkUpdate: {
                    ...state.BulkUpdate,
                    BulkUpdateValue: action.bulkUpdateValue,
                },
            };
        case CACHED_QUERY_ADD: {
            const actionTypedAdd = action;
            let cachedQueries = [].concat(state.CachedQueries);
            let queryToAdd = actionTypedAdd.cachedQuery;
            if (queryToAdd) {
                if (cachedQueries.length == 5) {
                    // we have hit the maximum so remove first item (oldest)
                    cachedQueries.splice(0, 1);
                }
                cachedQueries.push(actionTypedAdd.cachedQuery);
            }
            return Object.assign({}, state, { CachedQueries: cachedQueries });
        }
        case SET_CELL_SUMMARY_INFO:
            return {
                ...state,
                CellSummary: {
                    ...state.CellSummary,
                    CellSummaryInfo: action.cellSummaryInfo,
                },
            };
        case CELL_SUMMARY_CHANGE_OPERATION:
            return {
                ...state,
                CellSummary: {
                    ...state.CellSummary,
                    CellSummaryOperation: action.cellSummaryOperation,
                },
            };
        case LICENSE_SHOW_WATERMARK: {
            return {
                ...state,
                License: {
                    ...state.License,
                    watermark: {
                        show: true,
                        text: action.text,
                    },
                },
            };
        }
        case LICENSE_DISABLE_PERSISTENCE: {
            return {
                ...state,
                License: {
                    ...state.License,
                    disablePersistence: true,
                },
            };
        }
        case DATA_CHANGE_HISTORY_ADD: {
            const actionTypedAdd = action;
            const cellDataChangedInfo = actionTypedAdd.changeInfo;
            const uniqueKey = actionTypedAdd.uniqueKey;
            const updatedDataChangeHistoryLogs = { ...state.DataChangeHistory.logs };
            updatedDataChangeHistoryLogs[uniqueKey] = cellDataChangedInfo;
            return {
                ...state,
                DataChangeHistory: {
                    ...state.DataChangeHistory,
                    logs: updatedDataChangeHistoryLogs,
                },
            };
        }
        case DATA_CHANGE_HISTORY_UNDO: {
            const actionTypedUndo = action;
            const cellDataChangedInfo = actionTypedUndo.changeInfo;
            const uniqueKey = actionTypedUndo.uniqueKey;
            const updatedDataChangeHistoryLogs = { ...state.DataChangeHistory.logs };
            delete updatedDataChangeHistoryLogs[uniqueKey];
            return {
                ...state,
                DataChangeHistory: {
                    ...state.DataChangeHistory,
                    logs: updatedDataChangeHistoryLogs,
                },
            };
        }
        case DATA_CHANGE_HISTORY_CLEAR_ROW: {
            const actionTypedClearRow = action;
            const cellDataChangedInfo = actionTypedClearRow.changeInfo;
            const uniqueKey = actionTypedClearRow.uniqueKey;
            const updatedDataChangeHistoryLogs = { ...state.DataChangeHistory.logs };
            delete updatedDataChangeHistoryLogs[uniqueKey];
            return {
                ...state,
                DataChangeHistory: {
                    ...state.DataChangeHistory,
                    logs: updatedDataChangeHistoryLogs,
                },
            };
        }
        case DATA_CHANGE_HISTORY_ENABLE: {
            return {
                ...state,
                DataChangeHistory: {
                    ...state.DataChangeHistory,
                    currentMode: 'ACTIVE',
                    enableTime: new Date(),
                    suspendTime: null,
                },
            };
        }
        case DATA_CHANGE_HISTORY_DISABLE: {
            return {
                ...state,
                DataChangeHistory: {
                    ...state.DataChangeHistory,
                    logs: {},
                    currentMode: 'INACTIVE',
                    enableTime: null,
                    suspendTime: null,
                },
            };
        }
        case DATA_CHANGE_HISTORY_SUSPEND: {
            return {
                ...state,
                DataChangeHistory: {
                    ...state.DataChangeHistory,
                    currentMode: 'SUSPENDED',
                    suspendTime: new Date(),
                },
            };
        }
        case DATA_CHANGE_HISTORY_RESUME: {
            return {
                ...state,
                DataChangeHistory: {
                    ...state.DataChangeHistory,
                    currentMode: 'ACTIVE',
                    suspendTime: null,
                },
            };
        }
        case SETTINGS_PANEL_SET: {
            const setSettingsAction = action;
            return {
                ...state,
                SettingsPanel: {
                    ...state?.SettingsPanel,
                    [setSettingsAction.name]: setSettingsAction.settings,
                },
            };
        }
        case QUICK_FILTER_BAR_SHOW:
            return Object.assign({}, state, { IsQuickFilterVisible: true });
        case QUICK_FILTER_BAR_HIDE:
            return Object.assign({}, state, { IsQuickFilterVisible: false });
        case LAYOUT_SHOW_NOT_ASSOCIATED_OBJECTS: {
            const typedAction = action;
            return {
                ...state,
                Layout: {
                    ShowLayoutNotAssociatedObjects: typedAction.showNotAssociatedObjects,
                },
            };
        }
        case DATA_SET_SELECT:
            return Object.assign({}, state, {
                CurrentDataSet: action.dataSet.name,
            });
        case DASHBOARD_REFRESH: {
            return {
                ...state,
                Dashboard: {
                    ...state.Dashboard,
                    DashboardRevision: state.Dashboard.DashboardRevision + 1,
                },
            };
        }
        case CHARTING_SET_CURRENT_CHART_MODELS: {
            const typedAction = action;
            return {
                ...state,
                Charting: {
                    ...state.Charting,
                    currentChartModels: typedAction.chartModels,
                },
            };
        }
        case DISABLE_DELETE_CONFIRMATION: {
            return {
                ...state,
                DisableDeleteConfirmation: true,
            };
        }
        case SYSTEM_EXPORT_BEGIN: {
            const typedAction = action;
            return {
                ...state,
                Export: {
                    inProgress: {
                        reportName: typedAction.reportName,
                        reportFormat: typedAction.reportFormat,
                        exportDestination: typedAction.exportDestination,
                    },
                },
            };
        }
        case SYSTEM_EXPORT_END: {
            return {
                ...state,
                Export: {
                    inProgress: null,
                },
            };
        }
        case SET_QUICK_SEARCH_FLOATING_VISIBILITY: {
            const typedAction = action;
            return {
                ...state,
                QuickSearch: {
                    ...state.QuickSearch,
                    floatingVisible: typedAction.visible,
                },
            };
        }
        case CELL_POPUP_SHOW: {
            const typedAction = action;
            // reuse address reference if we are editing the same cell
            let newAddress = typedAction?.cellPosition;
            const oldAddress = state?.NotesAndComments?.popupPosition;
            if (AnnotationsService.isSameAddress(oldAddress, typedAction.cellPosition)) {
                newAddress = oldAddress;
                // if edit mode is the same, just exist
                if (typedAction.editMode === state.NotesAndComments.editMode) {
                    return state;
                }
            }
            return {
                ...state,
                NotesAndComments: {
                    ...state.NotesAndComments,
                    editMode: typedAction.editMode,
                    popupPosition: newAddress,
                },
            };
        }
        case CELL_POPUP_EDIT_FOCUSED_ENTITY: {
            const typedAction = action;
            return {
                ...state,
                NotesAndComments: {
                    ...state.NotesAndComments,
                    focusedEntity: typedAction.focusedEntity,
                },
            };
        }
        case CELL_POPUP_HIDE: {
            return {
                ...state,
                NotesAndComments: {
                    editMode: false,
                    popupPosition: null,
                },
            };
        }
        case SUMMARY_ROW_SET: {
            const typedAction = action;
            return {
                ...state,
                RowSummary: {
                    rowSummaries: typedAction.rowSummaries,
                },
            };
        }
        default:
            return state;
    }
};
