"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getMetricsByReference = exports.getMetricReferences = exports.getDimensionReferences = exports.getDimensionBaseName = void 0;
const item_1 = require("../../utils/item");
const getDimensionBaseName = (dimension) => dimension.timeIntervalBaseDimensionName ?? dimension.name;
exports.getDimensionBaseName = getDimensionBaseName;
const getDimensionReferences = ({ dimension, baseTable, }) => {
    const baseName = (0, exports.getDimensionBaseName)(dimension);
    const tableQualifiedReference = `${dimension.table}.${baseName}`;
    if (dimension.table === baseTable) {
        return [baseName, tableQualifiedReference];
    }
    return [tableQualifiedReference];
};
exports.getDimensionReferences = getDimensionReferences;
const getMetricReferences = ({ metric, baseTable, }) => {
    const references = [`${metric.table}.${metric.name}`];
    if (metric.table === baseTable) {
        references.push(metric.name);
    }
    return references;
};
exports.getMetricReferences = getMetricReferences;
const getMetricsByReference = ({ tables, baseTable, }) => Object.values(tables).reduce((acc, table) => {
    Object.values(table.metrics).forEach((metric) => {
        const fieldId = (0, item_1.getItemId)(metric);
        new Set([
            fieldId,
            ...(0, exports.getMetricReferences)({
                metric,
                baseTable,
            }),
        ]).forEach((reference) => {
            acc.set(reference, { fieldId, metric });
        });
    });
    return acc;
}, new Map());
exports.getMetricsByReference = getMetricsByReference;
//# sourceMappingURL=references.js.map