"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ChartingApiImpl = void 0;
const tslib_1 = require("tslib");
const ApiBase_1 = require("./ApiBase");
const ChartingRedux = tslib_1.__importStar(require("./../../Redux/ActionsReducers/ChartingRedux"));
const ChartingRedux_1 = require("./../../Redux/ActionsReducers/ChartingRedux");
const StringExtensions_1 = tslib_1.__importDefault(require("../../Utilities/Extensions/StringExtensions"));
const ChartingInternalApi_1 = require("../Internal/ChartingInternalApi");
const ObjectFactory_1 = tslib_1.__importDefault(require("../../Utilities/ObjectFactory"));
class ChartingApiImpl extends ApiBase_1.ApiBase {
    constructor(_adaptable) {
        super(_adaptable);
        this.internalApi = new ChartingInternalApi_1.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_1.default.CreateEmptyChartDefinition({
            Name: chart.chartId,
            Model: chart,
        }));
        this.dispatchAction(ChartingRedux.ChartingSetChartModels(chartDefinitions));
    }
    getPersistedCharts() {
        return this.getChartDefinitions().map((chart) => chart.Model);
    }
    getChartDefinitions() {
        return (0, ChartingRedux_1.ChartingGetChartModels)(this.getAdaptableState()) ?? [];
    }
    getChartDefinitionByUuid(uuid) {
        return this.getChartDefinitions().find((c) => c.Uuid == uuid);
    }
    getChartDefinitionByName(name) {
        if (StringExtensions_1.default.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_1.default.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_1.default.IsNotNullOrEmpty(name)) {
            let chartDefinition = this.getExternalChartDefinitions().find((c) => c.Name == name);
            if (this.checkItemExists(chartDefinition, name, 'Chart')) {
                return chartDefinition;
            }
        }
    }
}
exports.ChartingApiImpl = ChartingApiImpl;
