import { ApiBase } from './ApiBase';
import * as ChartingRedux from './../../Redux/ActionsReducers/ChartingRedux';
import { ChartingGetChartModels } from './../../Redux/ActionsReducers/ChartingRedux';
import StringExtensions from '../../Utilities/Extensions/StringExtensions';
import { ChartingInternalApi } from '../Internal/ChartingInternalApi';
import ObjectFactory from '../../Utilities/ObjectFactory';
export class ChartingApiImpl extends ApiBase {
    constructor(_adaptable) {
        super(_adaptable);
        this.internalApi = new ChartingInternalApi(_adaptable);
    }
    isChartingEnabled() {
        return this._adaptable.canGenerateCharts();
    }
    getCurrentChartModels() {
        return this._adaptable.getChartModels();
    }
    getChartRef(chartId) {
        return this._adaptable.getChartRef(chartId);
    }
    saveCurrentCharts() {
        const currentCharts = this.getCurrentChartModels();
        const chartDefinitions = currentCharts.map((chart) => ObjectFactory.CreateEmptyChartDefinition({
            Name: chart.chartId,
            Model: chart,
        }));
        this.dispatchAction(ChartingRedux.ChartingSetChartModels(chartDefinitions));
    }
    getPersistedCharts() {
        return this.getChartDefinitions().map((chart) => chart.Model);
    }
    getChartDefinitions() {
        return ChartingGetChartModels(this.getAdaptableState()) ?? [];
    }
    getChartDefinitionByUuid(uuid) {
        return this.getChartDefinitions().find((c) => c.Uuid == uuid);
    }
    getChartDefinitionByName(name) {
        if (StringExtensions.IsNotNullOrEmpty(name)) {
            let chartDefinition = this.getChartDefinitions().find((c) => c.Name == name);
            if (this.checkItemExists(chartDefinition, name, 'Chart')) {
                return chartDefinition;
            }
        }
    }
    showPersistedCharts() {
        return this._adaptable.showCharts(this.getChartDefinitions());
    }
    showChartDefinition(chartDefiniton, container) {
        let containerElement = container;
        if (typeof containerElement === 'string') {
            containerElement = this.internalApi.getContainerElemetnByName(containerElement);
        }
        return this._adaptable.showChart(chartDefiniton, containerElement);
    }
    showChartDefinitionOnce(chartDefinition, container) {
        const chartRef = this.getChartRef(chartDefinition?.Model?.chartId);
        if (chartRef) {
            return;
        }
        return this.showChartDefinition(chartDefinition, container);
    }
    closeChartDefinition(chartDefinition) {
        const chartRef = this.getChartRef(chartDefinition.Model.chartId);
        if (chartRef) {
            chartRef.destroyChart();
        }
    }
    editChartDefinition(editedChartDefinition) {
        this.dispatchAction(ChartingRedux.ChartingEditChart(editedChartDefinition));
        this.updateAgChart(editedChartDefinition);
    }
    updateAgChart(chartDefinition) {
        this._adaptable.updateChart(chartDefinition);
    }
    addChartDefinition(chart) {
        this.dispatchAction(ChartingRedux.ChartingAddChart(chart));
    }
    getChartingOpenState() {
        return {
            charts: this.getChartDefinitions().map((chartDefinition) => {
                return {
                    chartDefinition,
                    isOpen: this.getChartRef(chartDefinition?.Model?.chartId) != null,
                    containerName: this.getOpenChartContainer(chartDefinition)?.name,
                };
            }),
        };
    }
    getOpenChartContainer(chartDefinition) {
        const containerOptions = this.getChartingOptions().chartContainers;
        if (!containerOptions || !containerOptions.length) {
            return null;
        }
        return (containerOptions.find((container) => {
            const chartRef = this.getChartRef(chartDefinition.Model.chartId);
            const containerElement = this.internalApi.getContainerElement(container);
            if (!chartRef?.chartElement || !containerElement) {
                return false;
            }
            return containerElement.contains(chartRef.chartElement);
        }) ?? null);
    }
    setChartReadOnly(chartDefinition) {
        this.dispatchAction(ChartingRedux.ChartingEditChart({ ...chartDefinition, IsReadOnly: true }));
    }
    setChartEditable(chartDefinition) {
        this.dispatchAction(ChartingRedux.ChartingEditChart({ ...chartDefinition, IsReadOnly: false }));
    }
    addExternalChartDefinition(chartDefinition, options) {
        const saveChartBehaviour = options?.saveChartBehaviour ?? 'manual';
        if (saveChartBehaviour === 'auto') {
            this.dispatchAction(ChartingRedux.ChartingAddExternalChart({
                ...chartDefinition,
                Name: chartDefinition.Name ?? this.internalApi.getUniqueChartName(),
            }));
        }
        else {
            const alert = {
                alertType: 'generic',
                header: 'Save chart',
                message: 'Do you want to save this Chart?',
                alertDefinition: {
                    ...ObjectFactory.CreateEmptyAlertDefinition(),
                    MessageType: 'Info',
                    AlertProperties: {
                        DisplayNotification: true,
                        NotificationDuration: 'always',
                    },
                    AlertForm: {
                        Buttons: [
                            {
                                Label: 'Save',
                                Command: (context) => {
                                    this.dispatchAction(ChartingRedux.ChartingAddExternalChart({
                                        ...chartDefinition,
                                        Name: context.formData.name || this.internalApi.getUniqueChartName(),
                                    }));
                                },
                                ButtonStyle: {
                                    tone: 'info',
                                    variant: 'raised',
                                },
                            },
                            {
                                Label: 'Dismiss',
                                ButtonStyle: {
                                    tone: 'neutral',
                                    variant: 'raised',
                                },
                            },
                        ],
                        fields: [
                            {
                                name: 'name',
                                label: 'Name',
                                fieldType: 'text',
                                defaultValue: chartDefinition.Name ?? '',
                            },
                        ],
                    },
                },
            };
            this.getAlertApi().displayAdaptableAlert(alert);
        }
    }
    editExternalChartDefinition(chartDefinition) {
        this.dispatchAction(ChartingRedux.ChartingEditExternalChart(chartDefinition));
    }
    deleteExternalChartDefinition(chartDefinition) {
        this.dispatchAction(ChartingRedux.ChartingDeleteExternalChart(chartDefinition));
    }
    getExternalChartDefinitions() {
        return this.getAdaptableState().Charting.ExternalChartDefinitions;
    }
    getExternalChartDefinitionByName(name) {
        if (StringExtensions.IsNotNullOrEmpty(name)) {
            let chartDefinition = this.getExternalChartDefinitions().find((c) => c.Name == name);
            if (this.checkItemExists(chartDefinition, name, 'Chart')) {
                return chartDefinition;
            }
        }
    }
}
