'use strict';

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

var queryMappers = require('../../../../../utils/queryMappers.js');

const mandatoryColumns = (fields) => fields.filter((field) => field.mandatory).map((field) => field.name);
const visibleColumns = (fields) => fields.filter((field) => field.visible).map((field) => field.name);
const ensureMandatoryColumns = (fields, queryColumns) => {
    let queryCols;
    if (queryColumns) {
        queryCols = Array.isArray(queryColumns) ? queryColumns : [queryColumns];
    }
    const visibleCols = visibleColumns(fields);
    const mandatoryCols = mandatoryColumns(fields);
    const columns = queryCols || visibleCols;
    return Array.from(new Set([...mandatoryCols, ...columns]));
};
/**
 * Converts the columns from a DPD into a column readable format
 *
 * @param specification
 * @param requestedColumns
 */
const getColumns = (specification, req) => {
    const requestedColumns = queryMappers.normalizeQueryStringArray(req.query['columns']);
    const { fields } = specification;
    const options = fields.map((field) => ({
        text: field.display,
        value: field.name,
        disabled: field.mandatory,
    }));
    return {
        name: 'columns',
        options,
        text: 'Select report columns',
        value: ensureMandatoryColumns(fields, requestedColumns),
    };
};
var ColumnUtils = {
    getColumns,
    ensureMandatoryColumns,
    mandatoryColumns,
};

exports.default = ColumnUtils;
exports.ensureMandatoryColumns = ensureMandatoryColumns;
exports.getColumns = getColumns;
exports.mandatoryColumns = mandatoryColumns;
exports.visibleColumns = visibleColumns;
//# sourceMappingURL=utils.js.map
