"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FormatColumnReducer = exports.FormatColumnReady = exports.FormatColumnUnSuspendAll = exports.FormatColumnSuspendAll = exports.FormatColumnUnSuspend = exports.FormatColumnSuspend = exports.FormatColumnMoveDown = exports.FormatColumnMoveUp = exports.FormatColumnDeleteAll = exports.FormatColumnDelete = exports.FormatColumnEdit = exports.FormatColumnAdd = exports.FormatColumnsAdd = exports.FORMAT_COLUMN_UNSUSPEND_ALL = exports.FORMAT_COLUMN_UNSUSPEND = exports.FORMAT_COLUMN_SUSPEND_ALL = exports.FORMAT_COLUMN_SUSPEND = exports.FORMAT_COLUMN_READY = exports.FORMAT_COLUMN_DELETE_ALL = exports.FORMAT_COLUMN_DELETE = exports.FORMAT_COLUMN_EDIT = exports.FORMAT_COLUMN_MOVE_DOWN = exports.FORMAT_COLUMN_MOVE_UP = exports.FORMAT_COLUMN_ADD = exports.FORMAT_COLUMN_ADD_BATCH = 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 utils_1 = require("./utils");
/**
 * @ReduxAction Many Format Columns have been added
 */
exports.FORMAT_COLUMN_ADD_BATCH = 'FORMAT_COLUMN_ADD_BATCH';
/**
 * @ReduxAction A Format Column has been added
 */
exports.FORMAT_COLUMN_ADD = 'FORMAT_COLUMN_ADD';
/**
 * @ReduxAction Format column was moved up
 */
exports.FORMAT_COLUMN_MOVE_UP = 'FORMAT_COLUMN_MOVE_UP';
/**
 * @ReduxAction Format column was moved down
 */
exports.FORMAT_COLUMN_MOVE_DOWN = 'FORMAT_COLUMN_MOVE_DOWN';
/**
 * @ReduxAction A Format Column has been edited
 */
exports.FORMAT_COLUMN_EDIT = 'FORMAT_COLUMN_EDIT';
/**
 * @ReduxAction A Format Column has been deleted
 */
exports.FORMAT_COLUMN_DELETE = 'FORMAT_COLUMN_DELETE';
/**
 * @ReduxAction Delete ALL Format Columns
 */
exports.FORMAT_COLUMN_DELETE_ALL = 'FORMAT_COLUMN_DELETE_ALL';
/**
 * @ReduxAction FormatColumn Module is ready
 */
exports.FORMAT_COLUMN_READY = 'FORMAT_COLUMN_READY';
/**
 * @ReduxAction FormatColumn Module is suspended
 */
exports.FORMAT_COLUMN_SUSPEND = 'FORMAT_COLUMN_SUSPEND';
/**
 * @ReduxAction Suspend all FormatColumn Modules
 */
exports.FORMAT_COLUMN_SUSPEND_ALL = 'FORMAT_COLUMN_SUSPEND_ALL';
/**
 * @ReduxAction FormatColumn Module is unsuspended, or activated
 */
exports.FORMAT_COLUMN_UNSUSPEND = 'FORMAT_COLUMN_UNSUSPEND';
/**
 * @ReduxAction FormatColumn Module is unsuspended, or activated
 */
exports.FORMAT_COLUMN_UNSUSPEND_ALL = 'FORMAT_COLUMN_UNSUSPEND_ALL';
const FormatColumnsAdd = (formatColumns) => ({
    type: exports.FORMAT_COLUMN_ADD_BATCH,
    formatColumns,
});
exports.FormatColumnsAdd = FormatColumnsAdd;
const FormatColumnAdd = (formatColumn) => ({
    type: exports.FORMAT_COLUMN_ADD,
    formatColumn,
});
exports.FormatColumnAdd = FormatColumnAdd;
const FormatColumnEdit = (formatColumn) => ({
    type: exports.FORMAT_COLUMN_EDIT,
    formatColumn,
});
exports.FormatColumnEdit = FormatColumnEdit;
const FormatColumnDelete = (formatColumn) => ({
    type: exports.FORMAT_COLUMN_DELETE,
    formatColumn,
});
exports.FormatColumnDelete = FormatColumnDelete;
const FormatColumnDeleteAll = () => ({
    type: exports.FORMAT_COLUMN_DELETE_ALL,
});
exports.FormatColumnDeleteAll = FormatColumnDeleteAll;
const FormatColumnMoveUp = (formatColumn) => ({
    type: exports.FORMAT_COLUMN_MOVE_UP,
    formatColumn,
});
exports.FormatColumnMoveUp = FormatColumnMoveUp;
const FormatColumnMoveDown = (formatColumn) => ({
    type: exports.FORMAT_COLUMN_MOVE_DOWN,
    formatColumn,
});
exports.FormatColumnMoveDown = FormatColumnMoveDown;
const FormatColumnSuspend = (formatColumn) => ({
    type: exports.FORMAT_COLUMN_SUSPEND,
    formatColumn,
});
exports.FormatColumnSuspend = FormatColumnSuspend;
const FormatColumnUnSuspend = (formatColumn) => ({
    type: exports.FORMAT_COLUMN_UNSUSPEND,
    formatColumn,
});
exports.FormatColumnUnSuspend = FormatColumnUnSuspend;
const FormatColumnSuspendAll = () => ({
    type: exports.FORMAT_COLUMN_SUSPEND_ALL,
});
exports.FormatColumnSuspendAll = FormatColumnSuspendAll;
const FormatColumnUnSuspendAll = () => ({
    type: exports.FORMAT_COLUMN_UNSUSPEND_ALL,
});
exports.FormatColumnUnSuspendAll = FormatColumnUnSuspendAll;
const FormatColumnReady = (formatColumnState) => ({
    type: exports.FORMAT_COLUMN_READY,
    formatColumnState,
});
exports.FormatColumnReady = FormatColumnReady;
const initialState = {
    FormatColumns: GeneralConstants_1.EMPTY_ARRAY,
};
const FormatColumnReducer = (state = initialState, action) => {
    let formatColumns;
    switch (action.type) {
        case exports.FORMAT_COLUMN_ADD_BATCH: {
            const actionFormatColumns = action.formatColumns;
            actionFormatColumns.forEach(AdaptableHelper_1.default.addAdaptableObjectPrimitives);
            formatColumns = [].concat(state.FormatColumns);
            formatColumns.push(...actionFormatColumns);
            return { ...state, FormatColumns: formatColumns };
        }
        case exports.FORMAT_COLUMN_ADD: {
            const actionFormatColumn = action.formatColumn;
            AdaptableHelper_1.default.addAdaptableObjectPrimitives(actionFormatColumn);
            formatColumns = [].concat(state.FormatColumns);
            formatColumns.push(actionFormatColumn);
            return { ...state, FormatColumns: formatColumns };
        }
        case exports.FORMAT_COLUMN_EDIT:
            const actionFormatColumn = action.formatColumn;
            return {
                ...state,
                FormatColumns: state.FormatColumns.map((abObject) => abObject.Uuid === actionFormatColumn.Uuid ? actionFormatColumn : abObject),
            };
        case exports.FORMAT_COLUMN_DELETE: {
            const actionFormatColumn = action.formatColumn;
            return {
                ...state,
                FormatColumns: state.FormatColumns.filter((abObject) => abObject.Uuid !== actionFormatColumn.Uuid),
            };
        }
        case exports.FORMAT_COLUMN_DELETE_ALL: {
            return {
                ...state,
                FormatColumns: [],
            };
        }
        case exports.FORMAT_COLUMN_SUSPEND: {
            return {
                ...state,
                FormatColumns: (0, utils_1.changeIsSuspendInList)(action.formatColumn, state.FormatColumns, true),
            };
        }
        case exports.FORMAT_COLUMN_UNSUSPEND: {
            return {
                ...state,
                FormatColumns: (0, utils_1.changeIsSuspendInList)(action.formatColumn, state.FormatColumns, false),
            };
        }
        case exports.FORMAT_COLUMN_SUSPEND_ALL: {
            return {
                ...state,
                FormatColumns: (0, utils_1.suspendAllInList)(state.FormatColumns),
            };
        }
        case exports.FORMAT_COLUMN_UNSUSPEND_ALL: {
            return {
                ...state,
                FormatColumns: (0, utils_1.unsuspendAllInList)(state.FormatColumns),
            };
        }
        case exports.FORMAT_COLUMN_MOVE_UP: {
            const index = state.FormatColumns.findIndex((formatColumn) => formatColumn.Uuid === action.formatColumn.Uuid);
            let swapIndex = index - 1;
            if (swapIndex < 0) {
                swapIndex = state.FormatColumns.length - 1;
            }
            const formatColumnToSwap = state.FormatColumns[swapIndex];
            const newFormatColumns = [...state.FormatColumns];
            newFormatColumns[swapIndex] = state.FormatColumns[index];
            newFormatColumns[index] = formatColumnToSwap;
            return {
                ...state,
                FormatColumns: newFormatColumns,
            };
        }
        case exports.FORMAT_COLUMN_MOVE_DOWN: {
            const index = state.FormatColumns.findIndex((formatColumn) => formatColumn.Uuid === action.formatColumn.Uuid);
            let swapIndex = index + 1;
            if (swapIndex > state.FormatColumns.length - 1) {
                swapIndex = 0;
            }
            const formatColumnToSwap = state.FormatColumns[swapIndex];
            const newFormatColumns = [...state.FormatColumns];
            newFormatColumns[swapIndex] = state.FormatColumns[index];
            newFormatColumns[index] = formatColumnToSwap;
            return {
                ...state,
                FormatColumns: newFormatColumns,
            };
        }
        default:
            return state;
    }
};
exports.FormatColumnReducer = FormatColumnReducer;
