"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ChartingService = void 0;
const tslib_1 = require("tslib");
const InternalRedux = tslib_1.__importStar(require("../../Redux/ActionsReducers/InternalRedux"));
const ObjectFactory_1 = tslib_1.__importDefault(require("../ObjectFactory"));
class ChartingService {
    api;
    constructor(api) {
        this.api = api;
    }
    onChartModelChange(models, eventType, params) {
        const allChartDefinitions = this.api.chartingApi.getChartDefinitions();
        const currentChartModel = models.find((model) => model?.chartId === params.chartId);
        const currentChartDefinition = allChartDefinitions.find((chartDefinition) => chartDefinition.Model?.chartId === params.chartId);
        switch (eventType) {
            case 'chartCreated':
                const createParams = params;
                if (allChartDefinitions.some((chartDefinition) => chartDefinition.Model?.chartId === createParams.chartId)) {
                    // chart already exists
                }
                else {
                    const chartModel = models.find((model) => model?.chartId === createParams.chartId);
                    this.saveNewChart(chartModel);
                }
                break;
            case 'chartOptionsChanged':
            case 'chartRangeSelectionChanged':
                if (!currentChartDefinition) {
                    // editing a non-saved chart
                    break;
                }
                const updatedChartDefinition = {
                    ...currentChartDefinition,
                    Model: currentChartModel,
                };
                this.api.chartingApi.editChartDefinition(updatedChartDefinition);
                break;
        }
        this.api.internalApi.dispatchReduxAction(InternalRedux.ChartingSetCurrentChartModels(models));
        this.emitAdaptableChartDefinitonChange();
    }
    /**
     * Emit events only for adaptable charting definition changes
     * - an adaptable chart opens
     * - an adaptable chart closes
     * - an adaptable chart is created
     * - an adaptable chart is deleted
     */
    emitAdaptableChartDefinitonChange() {
        const chartingOpenState = this.api.chartingApi.getChartingOpenState();
        const chartChangedInfo = {
            chartingOpenState: chartingOpenState,
            ...this.api.internalApi.buildBaseContext(),
        };
        this.api.eventApi.emit('ChartChanged', chartChangedInfo);
    }
    saveNewChart(chartModel) {
        const chartingOptions = this.api.optionsApi.getChartingOptions() ?? {};
        const saveStragegy = chartingOptions.saveChartBehaviour ?? 'manual';
        if (saveStragegy === 'none') {
            return;
        }
        if (saveStragegy === 'manual') {
            const alert = {
                alertType: 'generic',
                header: 'Save chart',
                message: 'Do you want to save this Chart?',
                alertDefinition: {
                    ...ObjectFactory_1.default.CreateEmptyAlertDefinition(),
                    MessageType: 'Info',
                    AlertProperties: {
                        DisplayNotification: true,
                        NotificationDuration: 'always',
                    },
                    AlertForm: {
                        Buttons: [
                            {
                                Label: 'Save',
                                Command: (context) => {
                                    const chartName = context.formData.name || chartModel.chartId;
                                    this.api.chartingApi.addChartDefinition(ObjectFactory_1.default.CreateEmptyChartDefinition({
                                        Name: chartName,
                                        Model: chartModel,
                                    }));
                                },
                                ButtonStyle: {
                                    tone: 'info',
                                    variant: 'raised',
                                },
                            },
                            {
                                Label: 'Dismiss',
                                ButtonStyle: {
                                    tone: 'neutral',
                                    variant: 'raised',
                                },
                            },
                        ],
                        fields: [
                            {
                                name: 'name',
                                label: 'Name',
                                fieldType: 'text',
                            },
                        ],
                    },
                },
            };
            this.api.alertApi.displayAdaptableAlert(alert);
        }
        else if (saveStragegy === 'auto') {
            this.api.chartingApi.addChartDefinition(ObjectFactory_1.default.CreateEmptyChartDefinition({
                Name: chartModel.chartId,
                Model: chartModel,
            }));
        }
    }
    destroy() {
        this.api = null;
    }
}
exports.ChartingService = ChartingService;
