"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FreeTextColumnReducer = exports.FreeTextColumnReady = exports.FreeTextColumnAddEditStoredValue = exports.FreeTextColumnDelete = exports.FreeTextColumnEdit = exports.FreeTextColumnAdd = exports.FREE_TEXT_COLUMN_READY = exports.FREE_TEXT_COLUMN_ADD_EDIT_STORED_VALUE = exports.FREE_TEXT_COLUMN_DELETE = exports.FREE_TEXT_COLUMN_EDIT = exports.FREE_TEXT_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 Free Text Column has been added
 */
exports.FREE_TEXT_COLUMN_ADD = 'FREE_TEXT_COLUMN_ADD';
/**
 * @ReduxAction A Free Text Column has been edited
 */
exports.FREE_TEXT_COLUMN_EDIT = 'FREE_TEXT_COLUMN_EDIT';
/**
 * @ReduxAction A Free Text Column has been deleted
 */
exports.FREE_TEXT_COLUMN_DELETE = 'FREE_TEXT_COLUMN_DELETE';
/**
 * @ReduxAction A Free Text Column's stored values have changed
 */
exports.FREE_TEXT_COLUMN_ADD_EDIT_STORED_VALUE = 'FREE_TEXT_COLUMN_ADD_EDIT_STORED_VALUE';
/**
 * @ReduxAction FreeTextColumn Module is ready
 */
exports.FREE_TEXT_COLUMN_READY = 'FREE_TEXT_COLUMN_READY';
const FreeTextColumnAdd = (freeTextColumn) => ({
    type: exports.FREE_TEXT_COLUMN_ADD,
    freeTextColumn,
});
exports.FreeTextColumnAdd = FreeTextColumnAdd;
const FreeTextColumnEdit = (freeTextColumn) => ({
    type: exports.FREE_TEXT_COLUMN_EDIT,
    freeTextColumn,
});
exports.FreeTextColumnEdit = FreeTextColumnEdit;
const FreeTextColumnDelete = (freeTextColumn) => ({
    type: exports.FREE_TEXT_COLUMN_DELETE,
    freeTextColumn,
});
exports.FreeTextColumnDelete = FreeTextColumnDelete;
const FreeTextColumnAddEditStoredValue = (FreeTextColumn, FreeTextStoredValue) => ({
    type: exports.FREE_TEXT_COLUMN_ADD_EDIT_STORED_VALUE,
    FreeTextColumn,
    FreeTextStoredValue,
});
exports.FreeTextColumnAddEditStoredValue = FreeTextColumnAddEditStoredValue;
const FreeTextColumnReady = (freeTextColumnState) => ({
    type: exports.FREE_TEXT_COLUMN_READY,
    freeTextColumnState,
});
exports.FreeTextColumnReady = FreeTextColumnReady;
const initialState = {
    FreeTextColumns: GeneralConstants_1.EMPTY_ARRAY,
};
const FreeTextColumnReducer = (state = initialState, action) => {
    // let freeTextColumns: FreeTextColumn[];
    switch (action.type) {
        case exports.FREE_TEXT_COLUMN_ADD: {
            const actionFreeTextColumn = action.freeTextColumn;
            AdaptableHelper_1.default.addAdaptableObjectPrimitives(actionFreeTextColumn);
            const freeTextColumns = [...state.FreeTextColumns];
            freeTextColumns.push(actionFreeTextColumn);
            return { ...state, FreeTextColumns: freeTextColumns };
        }
        // !! MUTABLE STATE
        case exports.FREE_TEXT_COLUMN_EDIT:
            const actionFreeTextColumn = action.freeTextColumn;
            return {
                ...state,
                FreeTextColumns: state.FreeTextColumns.map((abObject) => abObject.Uuid === actionFreeTextColumn.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, actionFreeTextColumn)
                    : abObject),
            };
        case exports.FREE_TEXT_COLUMN_DELETE: {
            const actionFreeTextColumn = action.freeTextColumn;
            return {
                ...state,
                FreeTextColumns: state.FreeTextColumns.filter((abObject) => abObject.Uuid !== actionFreeTextColumn.Uuid),
            };
        }
        case exports.FREE_TEXT_COLUMN_ADD_EDIT_STORED_VALUE: {
            const actionTypedAddEditStoredValue = action;
            actionTypedAddEditStoredValue.FreeTextColumn.FreeTextStoredValues =
                actionTypedAddEditStoredValue.FreeTextColumn.FreeTextStoredValues ?? [];
            let existingIndex = actionTypedAddEditStoredValue.FreeTextColumn.FreeTextStoredValues.findIndex((ftsv) => ftsv.PrimaryKey == actionTypedAddEditStoredValue.FreeTextStoredValue.PrimaryKey);
            if (existingIndex != -1) {
                actionTypedAddEditStoredValue.FreeTextColumn.FreeTextStoredValues[existingIndex] =
                    actionTypedAddEditStoredValue.FreeTextStoredValue;
            }
            else {
                actionTypedAddEditStoredValue.FreeTextColumn.FreeTextStoredValues.push(actionTypedAddEditStoredValue.FreeTextStoredValue);
            }
            const freeTextColumns = [...state.FreeTextColumns];
            let index = freeTextColumns.findIndex((x) => x.ColumnId == actionTypedAddEditStoredValue.FreeTextColumn.ColumnId);
            freeTextColumns[index] = actionTypedAddEditStoredValue.FreeTextColumn;
            return Object.assign({}, state, { FreeTextColumns: freeTextColumns });
        }
        default:
            return state;
    }
};
exports.FreeTextColumnReducer = FreeTextColumnReducer;
