{"size": 3427, "ext": "js", "max_stars_repo_path": "src/js/processor/table/tableHeaderProcessor.js", "max_stars_repo_name": "BI-joe/report.js", "max_stars_repo_stars_event_min_datetime": "2015-01-03T05:43:46.000Z", "max_stars_repo_stars_event_max_datetime": "2021-08-02T18:05:28.000Z", "max_issues_repo_path": "src/js/processor/table/tableHeaderProcessor.js", "max_issues_repo_name": "BI-joe/report.js", "max_issues_repo_issues_event_min_datetime": "2015-12-28T16:19:44.000Z", "max_issues_repo_issues_event_max_datetime": "2016-10-13T22:20:10.000Z", "max_forks_repo_path": "src/js/processor/table/tableHeaderProcessor.js", "max_forks_repo_name": "BI-joe/report.js", "max_forks_repo_forks_event_min_datetime": "2015-01-26T13:03:36.000Z", "max_forks_repo_forks_event_max_datetime": "2018-09-26T06:06:03.000Z"}
{"max_stars_count": 20, "max_issues_count": 2, "max_forks_count": 4, "avg_line_length": 41.2891566265, "max_line_length": 149, "alphanum_fraction": 0.4893492851}
import {FIELD_MEASURES} from '../../constants';
import {TableRow}  from '../../result/table/tableRow';
import {TableCell} from '../../result/table/tableCell';

export class TableHeaderProcessor {

    constructor(measureFields, columnFields, options = {}) {
        this.measureFields = measureFields;
        this.columnFields = columnFields;
        this.options = options;
    }

    process(resultSet, headerCells = []) {
        if (this.options.hideHeaders) {
            return [];
        }

        let getHeaderRows = (outputRows, fields, currentRows) => {
                if (fields.length === 0) {
                    return 1;
                }

                let currentFieldId         = fields[0],
                    remainingFields        = fields.slice(1),
                    countCells             = 0,
                    currentRow;
                if (outputRows.has(currentFieldId)) {
                    currentRow = outputRows.get(currentFieldId);
                } else {
                    currentRow = new TableRow();
                    outputRows.set(currentFieldId, currentRow);
                }

                if (currentFieldId === FIELD_MEASURES) {
                    this.measureFields.forEach(measureFieldId => {
                        let measureField = resultSet.getField(measureFieldId);
                        let measureFieldIndex = resultSet.getFieldIndex(measureFieldId);
                        let subRows = currentRows.filter(row => row[measureFieldIndex].value !== null && row[measureFieldIndex].value !== undefined);
                        if (subRows.length) {
                            let childCellsCount = getHeaderRows(outputRows, remainingFields, subRows);
                            currentRow.addCell(new TableCell(measureField.caption, {
                                colspan: childCellsCount,
                                header: true
                            }));
                            countCells += childCellsCount;
                        }
                    });

                    return countCells;
                }

                let currentField           = resultSet.getField(currentFieldId),
                    currentFieldIndex      = resultSet.getFieldIndex(currentFieldId);
                resultSet.getFieldValues(currentField).forEach(fieldValue => {
                    let subRows = currentRows.filter(row => row[currentFieldIndex] === fieldValue);
                    if (subRows.length) {
                        let childCellsCount = getHeaderRows(outputRows, remainingFields, subRows);
                        currentRow.addCell(new TableCell(fieldValue.value, {
                            colspan: childCellsCount,
                            header: true
                        }));
                        countCells += childCellsCount;
                    }
                });

                return countCells;
            };

        let rowsMap = new Map();
        if (this.columnFields.length === 0) {
            return headerCells.concat([new TableRow([ new TableCell('', { header: true }) ])]);
        } else {
            getHeaderRows(rowsMap, this.columnFields, resultSet.rows);
            let rows = [];
            rowsMap.forEach(row => {
                row.cells = headerCells.concat(row.cells);
                rows.push(row);
            });
            return rows;
        }
    }
}
