"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ChartingReducer = exports.ChartingGetExternalChartDefinitions = exports.ChartingGetChartModels = exports.initialState = exports.ChartingDeleteExternalChart = exports.ChartingAddExternalChart = exports.ChartingEditExternalChart = exports.ChartingDeleteChart = exports.ChartingAddChart = exports.ChartingEditChart = exports.ChartingReady = exports.ChartingSetChartModels = exports.CHARTING_DELETE_EXTERNAL_CHART = exports.CHARTING_ADD_EXTERNAL_CHART = exports.CHARTING_EDIT_EXTERNAL_CHART = exports.CHARTING_DELETE_CHART = exports.CHARTING_ADD_CHART = exports.CHARTING_EDIT_CHART = exports.CHARTING_READY = exports.SET_CHARTING_CHARTS = void 0;
const Uuid_1 = require("../../AdaptableState/Uuid");
const GeneralConstants_1 = require("../../Utilities/Constants/GeneralConstants");
/**
 * @ReduxAction Set AG Grid Charting Models
 */
exports.SET_CHARTING_CHARTS = 'SET_CHARTING_CHARTS';
/**
 * @ReduxAction Charting Module is ready
 */
exports.CHARTING_READY = 'CHARTING_READY';
/**
 * @ReduxAction Edit chart definition
 */
exports.CHARTING_EDIT_CHART = 'CHARTING_EDIT_CHART';
/**
 * @ReduxAction Add chart definition
 */
exports.CHARTING_ADD_CHART = 'CHARTING_ADD_CHART';
/**
 * @ReduxAction Deelte chart definition
 */
exports.CHARTING_DELETE_CHART = 'CHARTING_DELETE_CHART';
/**
 * @ReduxAction Edit chart definition
 */
exports.CHARTING_EDIT_EXTERNAL_CHART = 'CHARTING_EDIT_EXTERNAL_CHART';
/**
 * @ReduxAction Add chart definition
 */
exports.CHARTING_ADD_EXTERNAL_CHART = 'CHARTING_ADD_EXTERNAL_CHART';
/**
 * @ReduxAction Deelte chart definition
 */
exports.CHARTING_DELETE_EXTERNAL_CHART = 'CHARTING_DELETE_EXTERNAL_CHART';
const ChartingSetChartModels = (charts) => ({
    charts,
    type: exports.SET_CHARTING_CHARTS,
});
exports.ChartingSetChartModels = ChartingSetChartModels;
const ChartingReady = (chartingState) => ({
    type: exports.CHARTING_READY,
    chartingState,
});
exports.ChartingReady = ChartingReady;
const ChartingEditChart = (chartDefinition) => ({
    type: exports.CHARTING_EDIT_CHART,
    chartDefinition,
});
exports.ChartingEditChart = ChartingEditChart;
const ChartingAddChart = (chartDefinition) => ({
    type: exports.CHARTING_ADD_CHART,
    chartDefinition,
});
exports.ChartingAddChart = ChartingAddChart;
const ChartingDeleteChart = (chartDefinition) => ({
    type: exports.CHARTING_DELETE_CHART,
    chartDefinition,
});
exports.ChartingDeleteChart = ChartingDeleteChart;
const ChartingEditExternalChart = (chartDefinition) => ({
    type: exports.CHARTING_EDIT_EXTERNAL_CHART,
    chartDefinition,
});
exports.ChartingEditExternalChart = ChartingEditExternalChart;
const ChartingAddExternalChart = (chartDefinition) => ({
    type: exports.CHARTING_ADD_EXTERNAL_CHART,
    chartDefinition,
});
exports.ChartingAddExternalChart = ChartingAddExternalChart;
const ChartingDeleteExternalChart = (chartDefinition) => ({
    type: exports.CHARTING_DELETE_EXTERNAL_CHART,
    chartDefinition,
});
exports.ChartingDeleteExternalChart = ChartingDeleteExternalChart;
exports.initialState = {
    ChartDefinitions: GeneralConstants_1.EMPTY_ARRAY,
    ExternalChartDefinitions: GeneralConstants_1.EMPTY_ARRAY,
};
const ChartingGetChartModels = (state) => state.Charting.ChartDefinitions ?? [];
exports.ChartingGetChartModels = ChartingGetChartModels;
const ChartingGetExternalChartDefinitions = (state) => state.Charting.ExternalChartDefinitions ?? [];
exports.ChartingGetExternalChartDefinitions = ChartingGetExternalChartDefinitions;
const ChartingReducer = (state = exports.initialState, action) => {
    switch (action.type) {
        case exports.SET_CHARTING_CHARTS:
            return {
                ...state,
                ChartDefinitions: action.charts,
            };
        case exports.CHARTING_EDIT_CHART:
            return {
                ...state,
                ChartDefinitions: state.ChartDefinitions.map((chartDefinition) => {
                    if (chartDefinition.Uuid === action.chartDefinition.Uuid) {
                        return action.chartDefinition;
                    }
                    return chartDefinition;
                }),
            };
        case exports.CHARTING_ADD_CHART:
            let chartDefinition = action.chartDefinition;
            if (!('Uuid' in chartDefinition)) {
                chartDefinition = {
                    ...chartDefinition,
                    Uuid: (0, Uuid_1.createUuid)(),
                };
            }
            return {
                ...state,
                ChartDefinitions: [...state.ChartDefinitions, chartDefinition],
            };
        case exports.CHARTING_DELETE_CHART:
            return {
                ...state,
                ChartDefinitions: state.ChartDefinitions.filter((chartDefinition) => chartDefinition.Name !== action.chartDefinition.Name),
            };
        case exports.CHARTING_EDIT_EXTERNAL_CHART:
            return {
                ...state,
                ExternalChartDefinitions: state.ExternalChartDefinitions.map((chartDefinition) => {
                    if (chartDefinition.Uuid ===
                        action.chartDefinition.Uuid) {
                        return action.chartDefinition;
                    }
                    return chartDefinition;
                }),
            };
        case exports.CHARTING_ADD_EXTERNAL_CHART:
            let externalChartDefinition = action.chartDefinition;
            if (!('Uuid' in externalChartDefinition)) {
                externalChartDefinition = {
                    ...externalChartDefinition,
                    Uuid: (0, Uuid_1.createUuid)(),
                };
            }
            return {
                ...state,
                ExternalChartDefinitions: [...state.ExternalChartDefinitions, externalChartDefinition],
            };
        case exports.CHARTING_DELETE_EXTERNAL_CHART:
            return {
                ...state,
                ExternalChartDefinitions: state.ExternalChartDefinitions.filter((chartDefinition) => chartDefinition.Name !==
                    action.chartDefinition.Name),
            };
    }
    return state;
};
exports.ChartingReducer = ChartingReducer;
