'use strict';

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

var VisualisationDatasetHelper = require('../../../utils/Dashboards/VisualisationDatasetHelper.js');
var validate = require('./validate.js');

const createList = (listDefinition, dashboardData) => {
    validate.ListSchema.parse(listDefinition);
    const { columns, options } = listDefinition;
    const listOptions = options;
    const showLatest = listOptions?.showLatest !== undefined ? listOptions.showLatest : true;
    const columnsAsList = listOptions?.columnsAsList;
    const { measures, keys } = columns;
    const showAllData = (!measures && !keys) || (!measures.length && !keys);
    let datasetData = [...dashboardData];
    if (showLatest) {
        datasetData = VisualisationDatasetHelper.default.getLastestDataset(datasetData);
    }
    let head;
    let rows;
    let ts;
    if (showAllData) {
        ({ head, rows, ts } = createFullList(datasetData));
    }
    else if (columnsAsList) {
        ({ head, rows, ts } = createListFromColumns(listDefinition, datasetData));
    }
    else {
        ({ head, rows, ts } = creatListFromRows(listDefinition, datasetData));
    }
    if (rows.length && measures)
        rows = sumColumns(rows, measures);
    return {
        table: {
            head,
            rows,
        },
        ts,
    };
};
const createListFromColumns = (listDefinition, dashboardData) => {
    const { columns } = listDefinition;
    const { keys, measures } = columns;
    const groupKey = VisualisationDatasetHelper.default.getGroupKey(dashboardData, keys || []);
    const timestampData = dashboardData[0]?.['ts']?.raw;
    const ts = timestampData ? `${timestampData}` : '';
    const head = [];
    head.push({
        html: '<p class="govuk-visually-hidden">list values</p>',
    });
    dashboardData.forEach((row) => {
        head.push({
            text: groupKey ? `${row[groupKey.id].raw}` : '',
        });
    });
    const rows = [];
    measures.forEach((measure) => {
        rows.push([{ text: measure.display }]);
    });
    measures.forEach((measure, index) => {
        dashboardData.forEach((row) => {
            rows[index].push({ text: `${row[measure.id].raw}` });
        });
    });
    return {
        rows,
        head,
        ts,
    };
};
const createTableRows = (data, measures) => {
    if (!data || data.length === 0 || !data[0])
        return [];
    return data.map((dataRow) => {
        const row = measures?.length ? Array(measures.length) : Array(Object.keys(data[0]).length);
        Object.keys(dataRow).forEach((key, index) => {
            const headIndex = measures?.length ? measures.findIndex((m) => m.id === key) : index;
            const text = dataRow[key].raw;
            row.splice(headIndex, 1, { text });
        });
        return row;
    });
};
const creatListFromRows = (listDefinition, dashboardData) => {
    const { measures } = listDefinition.columns;
    const head = measures.map((column) => {
        return { text: column.display || '' };
    });
    const dataSetRows = VisualisationDatasetHelper.default.getDatasetRows(listDefinition, dashboardData);
    const displayRows = VisualisationDatasetHelper.default.filterRowsByDisplayColumns(listDefinition, dataSetRows);
    const rows = createTableRows(displayRows, measures);
    const timestampData = dataSetRows[0]?.['ts']?.raw;
    const ts = timestampData ? `${timestampData}` : '';
    return {
        head,
        rows,
        ts,
    };
};
const createFullList = (dashboardData) => {
    if (!dashboardData || dashboardData.length === 0 || !dashboardData[0]) {
        return {
            head: [],
            rows: [],
            ts: '',
        };
    }
    const firstRow = dashboardData[0];
    const head = Object.keys(firstRow).map((key) => {
        return { text: key || '' };
    });
    const rows = createTableRows(dashboardData);
    const latestData = VisualisationDatasetHelper.default.getLastestDataset(dashboardData);
    const timestampData = latestData[0]?.['ts']?.raw;
    const ts = timestampData ? `${timestampData}` : '';
    return {
        head,
        rows,
        ts,
    };
};
const sumColumns = (rowsData, measures) => {
    const sumColumnIndexes = measures.flatMap((col, idx) => (col.aggregate ? [idx] : []));
    if (sumColumnIndexes.length) {
        const sumRow = [{ html: `<strong>Total<strong>` }];
        for (let index = 1; index < measures.length; index += 1) {
            sumRow[index] = { text: '' };
        }
        rowsData.push(sumRow);
        sumColumnIndexes.forEach((index) => {
            const total = rowsData.reduce((acc, row) => {
                const rowIndex = row[index];
                if (rowIndex && rowIndex.text) {
                    acc += Number(rowIndex.text);
                }
                return acc;
            }, 0);
            rowsData[rowsData.length - 1][index] = {
                html: `<strong>${total}<strong>`,
            };
        });
    }
    return rowsData;
};
var DashboardListUtils = {
    createList,
    createTableRows,
};

exports.createList = createList;
exports.createTableRows = createTableRows;
exports.default = DashboardListUtils;
//# sourceMappingURL=utils.js.map
