'use strict';

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

var VisualisationDatasetHelper = require('../../../utils/Dashboards/VisualisationDatasetHelper.js');
var ChartColours = require('./ChartColours.js');
var chartConfig = require('./chart-config.js');
var ChartLabels = require('./ChartLabels.js');

class TimeseriesChart {
    labels = [];
    datasets = [];
    unit;
    responseData = [];
    measures = [];
    keys = [];
    timeBlockData = [];
    groupKey;
    datasetCount = 0;
    hexColours = [];
    config = chartConfig;
    partialDate;
    chartColoursHelper;
    chartLabelsHelper;
    withData = (responseData) => {
        this.responseData = responseData;
        this.initHelpers();
        this.initFromData();
        return this;
    };
    initHelpers = () => {
        this.chartColoursHelper = new ChartColours();
        this.chartLabelsHelper = new ChartLabels();
    };
    initFromDefinition = (definition) => {
        this.measures = definition.columns.measures;
        this.keys = definition.columns.keys || [];
        this.unit = this.measures.find((m) => m.unit)?.unit;
    };
    withPartialDate = (partialDate) => {
        this.partialDate = partialDate;
        return this;
    };
    initFromData = () => {
        this.groupKey = VisualisationDatasetHelper.default.getGroupKey(this.responseData, this.keys);
        this.timeBlockData = VisualisationDatasetHelper.default.groupRowsByTimestamp(this.responseData);
        this.labels = this.getLabels();
        this.datasetCount = this.timeBlockData[0]?.length;
    };
    buildDatasets = () => {
        this.chartColoursHelper = new ChartColours();
        for (let index = 0; index < this.datasetCount; index += 1) {
            const data = this.timeBlockData.map((timeperiod) => {
                const valueId = this.measures[1].id;
                const period = timeperiod[index];
                return period && period[valueId].raw ? Number(period[valueId].raw) : 0;
            });
            const total = data.reduce((a, c) => a + c, 0);
            const label = this.chartLabelsHelper.getDatasetLabel(this.keys, this.timeBlockData[0][index]);
            this.datasets.push({
                data,
                label,
                total,
            });
        }
    };
    getLabels = () => {
        return this.timeBlockData.map((d) => d[0]['ts'].raw);
    };
}

exports.TimeseriesChart = TimeseriesChart;
exports.default = TimeseriesChart;
//# sourceMappingURL=ChartTimeseries.js.map
