'use strict';

var SummaryDataHelper = require('../SummaryDataHelper/SummaryDataHelper.js');
var DataTableBuilder = require('../../DataTableBuilder/DataTableBuilder.js');
var SectionedDataHelper = require('../SectionedDataHelper/SectionedDataHelper.js');
var TemplateBuilder = require('../TemplateBuilder.js');

class ReportBuilder extends TemplateBuilder.TemplateBuilder {
    dataTableBuilder;
    sectionBuilder;
    constructor(variant) {
        super(variant);
    }
    buildMainTable(section) {
        const collatedSummaryBuilder = new SummaryDataHelper.CollatedSummaryBuilder(this.specification, section.summaries);
        const tableSummaries = collatedSummaryBuilder.collateDataTableSummaries();
        this.dataTableBuilder = new DataTableBuilder.DataTableBuilder(this.fields);
        return this.dataTableBuilder
            .withSummaries(tableSummaries)
            .withHeaderOptions({
            columns: this.columns,
            reportQuery: this.reportQuery,
            interactive: true, // Always allow sorting regardless of interactive filters
        })
            .buildTable(section.data);
    }
    buildSummaryTables(summaries) {
        const summaryTables = summaries.map((summaryData) => {
            const fields = summaryData.fields;
            const { data, template } = summaryData;
            const summaryTableData = new DataTableBuilder.DataTableBuilder(fields)
                .withNoHeaderOptions(fields.map((f) => f.name))
                .buildTable(data);
            return {
                template,
                data: summaryTableData,
            };
        });
        return this.groupSummaryTables(summaryTables);
    }
    buildSummariesTables(section) {
        const { summaries } = section;
        const summaryTables = summaries.map((summaryData) => {
            const fields = summaryData.fields;
            const { data, template } = summaryData;
            const summaryTableData = new DataTableBuilder.DataTableBuilder(fields)
                .withNoHeaderOptions(fields.map((f) => f.name))
                .buildTable(data);
            return {
                template,
                data: summaryTableData,
            };
        });
        return this.groupSummaryTables(summaryTables);
    }
    groupSummaryTables(summaryTables) {
        return summaryTables.reduce((acc, item) => {
            if (!acc[item.template]) {
                acc[item.template] = [];
            }
            acc[item.template].push(item.data);
            return acc;
        }, {});
    }
    buildSectionedData() {
        const sectionData = new SectionedDataHelper.SectionedDataHelper()
            .withData(this.data)
            .withSections(this.sections)
            .withSummaries(this.summaries)
            .withFields(this.fields)
            .withReportQuery(this.reportQuery)
            .build();
        return sectionData.sections.map((section) => {
            const tableData = this.buildMainTable(section);
            const sectionSummaryTables = this.buildSummaryTables(section.summaries);
            return {
                ...section,
                summaries: sectionSummaryTables,
                data: tableData,
            };
        });
    }
    build() {
        const pageSummaryTables = this.buildSummaryTables(this.pageSummaries);
        const sections = this.buildSectionedData();
        return {
            rowCount: this.data.length,
            summaries: pageSummaryTables,
            sections,
        };
    }
}

exports.ReportBuilder = ReportBuilder;
//# sourceMappingURL=ReportBuilder.js.map
