"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.NamedQueryReducer = exports.NamedQueryReady = exports.NamedQueryDelete = exports.NamedQueryEdit = exports.NamedQueriesAdd = exports.NamedQueryAdd = exports.NAMED_QUERY_DELETE = exports.NAMED_QUERY_EDIT = exports.NAMED_QUERIES_ADD = exports.NAMED_QUERY_ADD = exports.NAMED_QUERY_READY = 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 Named Query has been added
 */
exports.NAMED_QUERY_READY = 'NAMED_QUERY_READY';
/**
 * @ReduxAction A Named Query has been added
 */
exports.NAMED_QUERY_ADD = 'NAMED_QUERY_ADD';
/**
 * @ReduxAction Multiple Named Queries have been added
 */
exports.NAMED_QUERIES_ADD = 'NAMED_QUERY_ADD_MULTIPLE';
/**
 * @ReduxAction A Named Query has been edited
 */
exports.NAMED_QUERY_EDIT = 'NAMED_QUERY_EDIT';
/**
 * @ReduxAction A Named Query has been deleted
 */
exports.NAMED_QUERY_DELETE = 'NAMED_QUERY_DELETE';
const NamedQueryAdd = (namedQuery) => ({
    type: exports.NAMED_QUERY_ADD,
    namedQuery: namedQuery,
});
exports.NamedQueryAdd = NamedQueryAdd;
const NamedQueriesAdd = (namedQueries) => ({
    type: exports.NAMED_QUERIES_ADD,
    namedQueries: namedQueries,
});
exports.NamedQueriesAdd = NamedQueriesAdd;
const NamedQueryEdit = (namedQuery) => ({
    type: exports.NAMED_QUERY_EDIT,
    namedQuery: namedQuery,
});
exports.NamedQueryEdit = NamedQueryEdit;
const NamedQueryDelete = (namedQuery) => ({
    type: exports.NAMED_QUERY_DELETE,
    namedQuery: namedQuery,
});
exports.NamedQueryDelete = NamedQueryDelete;
const NamedQueryReady = (namedQueryState) => ({
    type: exports.NAMED_QUERY_READY,
    namedQueryState,
});
exports.NamedQueryReady = NamedQueryReady;
const initialState = {
    NamedQueries: GeneralConstants_1.EMPTY_ARRAY,
};
const NamedQueryReducer = (state = initialState, action) => {
    let namedQuerys;
    switch (action.type) {
        case exports.NAMED_QUERY_ADD: {
            const actionnamedQuery = action.namedQuery;
            AdaptableHelper_1.default.addAdaptableObjectPrimitives(actionnamedQuery);
            namedQuerys = [].concat(state.NamedQueries);
            namedQuerys.push(actionnamedQuery);
            return { ...state, NamedQueries: namedQuerys };
        }
        case exports.NAMED_QUERIES_ADD: {
            const actionnamedQueries = action.namedQueries;
            actionnamedQueries.forEach((namedQuery) => {
                AdaptableHelper_1.default.addAdaptableObjectPrimitives(namedQuery);
            });
            return { ...state, NamedQueries: [...state.NamedQueries, ...actionnamedQueries] };
        }
        case exports.NAMED_QUERY_EDIT: {
            const actionnamedQuery = action.namedQuery;
            return {
                ...state,
                NamedQueries: state.NamedQueries.map((abObject) => abObject.Uuid === actionnamedQuery.Uuid ? actionnamedQuery : abObject),
            };
        }
        case exports.NAMED_QUERY_DELETE: {
            const actionnamedQuery = action.namedQuery;
            return {
                ...state,
                NamedQueries: state.NamedQueries.filter((abObject) => abObject.Uuid !== actionnamedQuery.Uuid),
            };
        }
        default:
            return state;
    }
};
exports.NamedQueryReducer = NamedQueryReducer;
