"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getEntityMetrics = getEntityMetrics;
const get_prop_1 = require("../objects/get-prop");
const util_1 = require("../util");
/**
 * Get the metrics from an entity applying some pre-processing
 * and adding source info.
 * @param entity
 * @returns
 */
function getEntityMetrics(entity) {
    // Create the source info that will attach into each metric
    const sourceInfo = {
        id: entity.id,
        name: entity.name,
        type: entity.type,
    };
    const entityMetrics = entity.metrics || [];
    const metrics = entityMetrics.map((metric) => {
        // assign the source info to the metric
        metric.entityInfo = Object.assign({}, sourceInfo);
        // at this point the source references should have been resolved
        // so we can cast the source to a MetricSource
        const source = metric.source;
        // If the metric is an item query and the catalog key is defined
        // get the scope from the specified collection in the catalog.
        if (source.type === "item-query" && source.collectionKey) {
            const key = source.collectionKey;
            const query = (0, get_prop_1.getProp)(entity.catalog, `collections[findBy(key,'${key}')].scope`);
            if (query) {
                source.scope = (0, util_1.cloneObject)(query);
            }
        }
        // reassign the source with updates
        metric.source = source;
        return metric;
    });
    return metrics;
}
//# sourceMappingURL=getEntityMetrics.js.map