"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CalculatedColumnReducer = exports.CalculatedColumnReady = exports.CalculatedColumnDelete = exports.CalculatedColumnEdit = exports.CalculatedColumnAdd = exports.CALCULATED_COLUMN_READY = exports.CALCULATED_COLUMN_DELETE = exports.CALCULATED_COLUMN_EDIT = exports.CALCULATED_COLUMN_ADD = void 0;
const tslib_1 = require("tslib");
const GeneralConstants_1 = require("../../Utilities/Constants/GeneralConstants");
const AdaptableHelper_1 = tslib_1.__importDefault(require("../../Utilities/Helpers/AdaptableHelper"));
/**
 * @ReduxAction A Calculated Column has been added
 */
exports.CALCULATED_COLUMN_ADD = 'CALCULATED_COLUMN_ADD';
/**
 * @ReduxAction A Calculated Column has been edited
 */
exports.CALCULATED_COLUMN_EDIT = 'CALCULATED_COLUMN_EDIT';
/**
 * @ReduxAction A Calculated Column has been deleted
 */
exports.CALCULATED_COLUMN_DELETE = 'CALCULATED_COLUMN_DELETE';
/**
 * @ReduxAction Calculated Column Module is ready
 */
exports.CALCULATED_COLUMN_READY = 'CALCULATED_COLUMN_READY';
const CalculatedColumnAdd = (calculatedColumn) => ({
    type: exports.CALCULATED_COLUMN_ADD,
    calculatedColumn,
});
exports.CalculatedColumnAdd = CalculatedColumnAdd;
const CalculatedColumnEdit = (calculatedColumn) => ({
    type: exports.CALCULATED_COLUMN_EDIT,
    calculatedColumn,
});
exports.CalculatedColumnEdit = CalculatedColumnEdit;
const CalculatedColumnDelete = (calculatedColumn) => ({
    type: exports.CALCULATED_COLUMN_DELETE,
    calculatedColumn,
});
exports.CalculatedColumnDelete = CalculatedColumnDelete;
const CalculatedColumnReady = (calculatedColumnState) => ({
    type: exports.CALCULATED_COLUMN_READY,
    calculatedColumnState,
});
exports.CalculatedColumnReady = CalculatedColumnReady;
const initialState = {
    CalculatedColumns: GeneralConstants_1.EMPTY_ARRAY,
};
const CalculatedColumnReducer = (state = initialState, action) => {
    let calculatedColumns;
    switch (action.type) {
        case exports.CALCULATED_COLUMN_ADD: {
            const actionCalculatedColumn = action
                .calculatedColumn;
            AdaptableHelper_1.default.addAdaptableObjectPrimitives(actionCalculatedColumn);
            calculatedColumns = [].concat(state.CalculatedColumns);
            calculatedColumns.push(actionCalculatedColumn);
            return { ...state, CalculatedColumns: calculatedColumns };
        }
        // !! MUTABLE STATE
        case exports.CALCULATED_COLUMN_EDIT: {
            const actionCalculatedColumn = action
                .calculatedColumn;
            return {
                ...state,
                CalculatedColumns: state.CalculatedColumns.map((abObject) => abObject.Uuid === actionCalculatedColumn.Uuid
                    ? // we need to assign and return the same object
                        // in order to make working with aggrid easier, as the col getter
                        // references this object
                        Object.assign(abObject, actionCalculatedColumn)
                    : abObject),
            };
        }
        case exports.CALCULATED_COLUMN_DELETE: {
            const actionCalculatedColumn = action
                .calculatedColumn;
            return {
                ...state,
                CalculatedColumns: state.CalculatedColumns.filter((abObject) => abObject.Uuid !== actionCalculatedColumn.Uuid),
            };
        }
        default:
            return state;
    }
};
exports.CalculatedColumnReducer = CalculatedColumnReducer;
