"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PlusMinusReducer = exports.PlusMinusReady = exports.PlusMinusMoveDown = exports.PlusMinusMoveUp = exports.PlusMinusNudgeUnSuspendAll = exports.PlusMinusNudgeSuspendAll = exports.PlusMinusNudgeUnSuspend = exports.PlusMinusNudgeSuspend = exports.PlusMinusNudgeDelete = exports.PlusMinusNudgeEdit = exports.PlusMinusNudgeAdd = exports.PlusMinusApply = exports.PLUS_MINUS_READY = exports.PLUS_MINUS_RULE_MOVE_DOWN = exports.PLUS_MINUS_RULE_MOVE_UP = exports.PLUS_MINUS_RULE_UNSUSPEND_ALL = exports.PLUS_MINUS_RULE_SUSPEND_ALL = exports.PLUS_MINUS_RULE_UNSUSPEND = exports.PLUS_MINUS_RULE_SUSPEND = exports.PLUS_MINUS_RULE_DELETE = exports.PLUS_MINUS_RULE_EDIT = exports.PLUS_MINUS_RULE_ADD = exports.PLUS_MINUS_APPLY = 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 A Plus Minus Rule has been applied
 */
exports.PLUS_MINUS_APPLY = 'PLUS_MINUS_APPLY';
/**
 * @ReduxAction A Plus Minus Rule has been added
 */
exports.PLUS_MINUS_RULE_ADD = 'PLUS_MINUS_RULE_ADD';
/**
 * @ReduxAction A Plus Minus Rule has been edited
 */
exports.PLUS_MINUS_RULE_EDIT = 'PLUS_MINUS_RULE_EDIT';
/**
 * @ReduxAction A Plus Minus Rule has been deleted
 */
exports.PLUS_MINUS_RULE_DELETE = 'PLUS_MINUS_RULE_DELETE';
/**
 * @ReduxAction A Plus Minus Rule has been suspended
 */
exports.PLUS_MINUS_RULE_SUSPEND = 'PLUS_MINUS_RULE_SUSPEND';
/**
 * @ReduxAction A Plus Minus Rule has been unsuspended
 */
exports.PLUS_MINUS_RULE_UNSUSPEND = 'PLUS_MINUS_RULE_UNSUSPEND';
/**
 * @ReduxAction All Plus Minus Rules have been suspended
 */
exports.PLUS_MINUS_RULE_SUSPEND_ALL = 'PLUS_MINUS_RULE_SUSPEND_ALL';
/**
 * @ReduxAction All Plus Minus Rules have been unsuspended
 */
exports.PLUS_MINUS_RULE_UNSUSPEND_ALL = 'PLUS_MINUS_RULE_UNSUSPEND_ALL';
/**
 * @ReduxAction A Plus Minus Rule has been moved up
 */
exports.PLUS_MINUS_RULE_MOVE_UP = 'PLUS_MINUS_RULE_MOVE_UP';
/**
 * @ReduxAction A Plus Minus Rule has been moved down
 */
exports.PLUS_MINUS_RULE_MOVE_DOWN = 'PLUS_MINUS_RULE_MOVE_DOWN';
/**
 * @ReduxAction PlusMinus Module is ready
 */
exports.PLUS_MINUS_READY = 'PLUS_MINUS_READY';
const PlusMinusApply = (cellUpdateRequests) => ({
    type: exports.PLUS_MINUS_APPLY,
    cellUpdateRequests,
});
exports.PlusMinusApply = PlusMinusApply;
const PlusMinusNudgeAdd = (plusMinusNudge) => ({
    type: exports.PLUS_MINUS_RULE_ADD,
    plusMinusNudge: plusMinusNudge,
});
exports.PlusMinusNudgeAdd = PlusMinusNudgeAdd;
const PlusMinusNudgeEdit = (plusMinusNudge) => ({
    type: exports.PLUS_MINUS_RULE_EDIT,
    plusMinusNudge: plusMinusNudge,
});
exports.PlusMinusNudgeEdit = PlusMinusNudgeEdit;
const PlusMinusNudgeDelete = (plusMinusNudge) => ({
    type: exports.PLUS_MINUS_RULE_DELETE,
    plusMinusNudge: plusMinusNudge,
});
exports.PlusMinusNudgeDelete = PlusMinusNudgeDelete;
const PlusMinusNudgeSuspend = (plusMinusNudge) => ({
    type: exports.PLUS_MINUS_RULE_SUSPEND,
    plusMinusNudge: plusMinusNudge,
});
exports.PlusMinusNudgeSuspend = PlusMinusNudgeSuspend;
const PlusMinusNudgeUnSuspend = (plusMinusNudge) => ({
    type: exports.PLUS_MINUS_RULE_UNSUSPEND,
    plusMinusNudge: plusMinusNudge,
});
exports.PlusMinusNudgeUnSuspend = PlusMinusNudgeUnSuspend;
const PlusMinusNudgeSuspendAll = () => ({
    type: exports.PLUS_MINUS_RULE_SUSPEND_ALL,
});
exports.PlusMinusNudgeSuspendAll = PlusMinusNudgeSuspendAll;
const PlusMinusNudgeUnSuspendAll = () => ({
    type: exports.PLUS_MINUS_RULE_UNSUSPEND_ALL,
});
exports.PlusMinusNudgeUnSuspendAll = PlusMinusNudgeUnSuspendAll;
const PlusMinusMoveUp = (plusMinusNudge) => ({
    type: exports.PLUS_MINUS_RULE_MOVE_UP,
    plusMinusNudge,
});
exports.PlusMinusMoveUp = PlusMinusMoveUp;
const PlusMinusMoveDown = (plusMinusNudge) => ({
    type: exports.PLUS_MINUS_RULE_MOVE_DOWN,
    plusMinusNudge,
});
exports.PlusMinusMoveDown = PlusMinusMoveDown;
const PlusMinusReady = (plusMinusState) => ({
    type: exports.PLUS_MINUS_READY,
    plusMinusState,
});
exports.PlusMinusReady = PlusMinusReady;
const initialState = {
    PlusMinusNudges: GeneralConstants_1.EMPTY_ARRAY,
};
const PlusMinusReducer = (state = initialState, action) => {
    let plusMinusNudges;
    switch (action.type) {
        case exports.PLUS_MINUS_APPLY:
            //we apply logic in the middleware since it's an Api call
            return Object.assign({}, state);
        case exports.PLUS_MINUS_RULE_ADD: {
            const actionPlusMinusNudge = action.plusMinusNudge;
            AdaptableHelper_1.default.addAdaptableObjectPrimitives(actionPlusMinusNudge);
            plusMinusNudges = [].concat(state.PlusMinusNudges);
            plusMinusNudges.push(actionPlusMinusNudge);
            return { ...state, PlusMinusNudges: plusMinusNudges };
        }
        case exports.PLUS_MINUS_RULE_EDIT: {
            const actionPlusMinusNudge = action.plusMinusNudge;
            return {
                ...state,
                PlusMinusNudges: state.PlusMinusNudges.map((abObject) => abObject.Uuid === actionPlusMinusNudge.Uuid ? actionPlusMinusNudge : abObject),
            };
        }
        case exports.PLUS_MINUS_RULE_DELETE: {
            const actionPlusMinusNudge = action.plusMinusNudge;
            return {
                ...state,
                PlusMinusNudges: state.PlusMinusNudges.filter((abObject) => abObject.Uuid !== actionPlusMinusNudge.Uuid),
            };
        }
        case exports.PLUS_MINUS_RULE_SUSPEND_ALL: {
            return {
                ...state,
                PlusMinusNudges: (0, utils_1.suspendAllInList)(state.PlusMinusNudges),
            };
        }
        case exports.PLUS_MINUS_RULE_UNSUSPEND_ALL: {
            return {
                ...state,
                PlusMinusNudges: (0, utils_1.unsuspendAllInList)(state.PlusMinusNudges),
            };
        }
        case exports.PLUS_MINUS_RULE_SUSPEND: {
            return {
                ...state,
                PlusMinusNudges: (0, utils_1.changeIsSuspendInList)(action.plusMinusNudge, state.PlusMinusNudges, true),
            };
        }
        case exports.PLUS_MINUS_RULE_UNSUSPEND: {
            return {
                ...state,
                PlusMinusNudges: (0, utils_1.changeIsSuspendInList)(action.plusMinusNudge, state.PlusMinusNudges, false),
            };
        }
        case exports.PLUS_MINUS_RULE_MOVE_UP: {
            const actionPlusMinusNudge = action.plusMinusNudge;
            const plusMinusNudges = [...(state.PlusMinusNudges ?? [])];
            const index = plusMinusNudges.findIndex((plusMinus) => plusMinus.Uuid === actionPlusMinusNudge.Uuid);
            let swapIndex = index - 1;
            if (swapIndex < 0) {
                swapIndex = plusMinusNudges.length - 1;
            }
            const temp = plusMinusNudges[index];
            plusMinusNudges[index] = plusMinusNudges[swapIndex];
            plusMinusNudges[swapIndex] = temp;
            return {
                ...state,
                PlusMinusNudges: plusMinusNudges,
            };
        }
        case exports.PLUS_MINUS_RULE_MOVE_DOWN: {
            const actionPlusMinusNudge = action
                .plusMinusNudge;
            const plusMinusNudges = [...(state.PlusMinusNudges ?? [])];
            const index = plusMinusNudges.findIndex((plusMinus) => plusMinus.Uuid === actionPlusMinusNudge.Uuid);
            let swapIndex = index + 1;
            if (swapIndex >= plusMinusNudges.length) {
                swapIndex = 0;
            }
            const temp = plusMinusNudges[index];
            plusMinusNudges[index] = plusMinusNudges[swapIndex];
            plusMinusNudges[swapIndex] = temp;
            return {
                ...state,
                PlusMinusNudges: plusMinusNudges,
            };
        }
        default:
            return state;
    }
};
exports.PlusMinusReducer = PlusMinusReducer;
