'use strict';

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

var logger = require('./logger.js');

const getFilter = (fields, fieldId) => {
    return fields.find((f) => f.name === fieldId)?.filter;
};
const getCurrentVariantDefinition = (definitions, reportId, variantId) => {
    if (!definitions) {
        logger.default.info('No definitions');
        return undefined;
    }
    let variantDef;
    const reportDef = definitions.find((report) => report.id === reportId);
    if (reportDef) {
        // TODO: this needs fixing, we should never be using `as unknown as`, but the types are very mixed up here
        variantDef = reportDef.variants.find((variant) => variantId === variant.id);
        return variantDef;
    }
    return undefined;
};
const getFieldDisplayName = (fields, fieldId) => {
    const ids = fieldId.split('.');
    const field = fields.find((f) => {
        return f.name === ids[0];
    });
    if (field) {
        return ids[1] ? `${field.display} ${ids[1]}` : field.display;
    }
    return fieldId;
};
const getField = (fields, fieldId) => {
    return fields.find((field) => {
        return field.name === fieldId;
    });
};
const getFields = (definition) => {
    return definition.variant.specification?.fields || [];
};
const getTemplate = (definition) => {
    return definition.variant.specification?.template || 'list';
};
const getFilters = (fields) => {
    const filters = fields
        .filter((field) => field.filter)
        .map((field) => field.filter);
    return filters.length ? filters : [];
};
const getFieldsByName = (names, fields) => {
    return names.map((s) => fields.find((field) => field.name === s)).filter((field) => field !== undefined);
};
const validateDefinition = (definition) => {
    const { variant } = definition;
    return validateVariant(variant);
};
const validateVariant = (variant) => {
    const { specification } = variant;
    if (!specification) {
        throw new Error('No specification in definition');
    }
    return { variant, specification };
};
const hasInteractiveFilters = (fields) => {
    const filters = getFilters(fields);
    return filters.some((filter) => filter.interactive);
};
const getReportSummary = (reportId, reportingService, token, definitionPath) => reportingService.getDefinitionSummary(token, reportId, definitionPath);
var DefinitionUtils = {
    getCurrentVariantDefinition,
    getFieldDisplayName,
    getField,
    getFields,
    getTemplate,
    getFilter,
    getFilters,
    getReportSummary,
    hasInteractiveFilters,
};

exports.default = DefinitionUtils;
exports.getCurrentVariantDefinition = getCurrentVariantDefinition;
exports.getField = getField;
exports.getFieldDisplayName = getFieldDisplayName;
exports.getFields = getFields;
exports.getFieldsByName = getFieldsByName;
exports.getFilter = getFilter;
exports.getFilters = getFilters;
exports.getReportSummary = getReportSummary;
exports.getTemplate = getTemplate;
exports.hasInteractiveFilters = hasInteractiveFilters;
exports.validateDefinition = validateDefinition;
exports.validateVariant = validateVariant;
//# sourceMappingURL=definitionUtils.js.map
