'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var types = require('../../../_dashboards/dashboard-visualisation/types.js');
var Chart = require('../Chart.js');
var validate = require('./validate.js');

class BarChart extends Chart.Chart {
    definition;
    measures;
    keys;
    options;
    responseData = [];
    isList = false;
    barCount = 0;
    withDefinition = (definition) => {
        this.definition = validate.BarSchema.parse(definition);
        this.initFromDefinitionData();
        return this;
    };
    initFromDefinitionData = () => {
        this.measures = this.definition.columns.measures;
        this.options = this.definition.options;
        this.keys = this.definition.columns.keys || [];
        this.isList = !!this.measures.find((col) => col.axis);
        this.initUnit(this.measures);
    };
    getCanvasHeight = () => {
        this.barCount = this.datasets.length * this.datasets[0].data.length;
        return this.options?.horizontal ? this.barCount : 5;
    };
    build = () => {
        if (!this.isList) {
            this.createDatasets(this.measures, this.keys); // Chart.ts
        }
        else {
            this.createListDatasets(this.measures, this.keys);
        }
        // Augment the datasets with chart specific config
        this.augmentDataset();
        // Set the bespoke chart.js options for the chart
        this.setBespokeOptions();
        const height = this.getCanvasHeight();
        return {
            type: types.DashboardVisualisationType.BAR,
            options: {
                height,
                unit: this.unit,
                timeseries: false,
            },
            data: {
                labels: this.labels,
                datasets: this.datasets,
                config: this.config,
            },
        };
    };
    augmentDataset = (datasets) => {
        const ds = datasets || this.datasets;
        this.datasets = ds.map((set) => {
            return {
                ...set,
                borderWidth: [0, 0],
                datalabels: {
                    align: 'center',
                    anchor: 'bottom',
                },
            };
        });
        return this.datasets;
    };
    setBespokeOptions = () => {
        // Initialise the scales
        this.setScales({ horizontal: this.options?.horizontal });
        let indexAxis = 'x';
        if (this.options) {
            const { horizontal, xStacked, yStacked } = this.options;
            indexAxis = horizontal ? 'y' : indexAxis;
            if (xStacked) {
                this.config.scales.x = {
                    ...this.config.scales.x,
                    stacked: xStacked,
                };
            }
            if (yStacked) {
                this.config.scales.y = {
                    ...this.config.scales.y,
                    stacked: yStacked,
                };
            }
        }
        this.config = {
            ...this.config,
            indexAxis,
        };
    };
}

exports.BarChart = BarChart;
exports.default = BarChart;
//# sourceMappingURL=BarChart.js.map
