"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolveGranularityInLabel = exports.getGranularityMapFromItems = void 0;
const field_1 = require("../types/field");
const timeFrames_1 = require("../types/timeFrames");
const GRANULARITY_PATTERN = /\$\{([^}]+)\.granularity\}/g;
// Builds a base-field-id → active-granularity map from the query's items. The
// fields already reflect the effective grain (date zoom is applied server-side
// and the returned dimensions carry the resulting `timeInterval`), so labels
// resolve straight from each field without a separate date-zoom override.
const getGranularityMapFromItems = (itemsMap) => {
    if (!itemsMap)
        return {};
    const map = {};
    for (const field of Object.values(itemsMap)) {
        if ((0, field_1.isDimension)(field) &&
            field.timeIntervalBaseDimensionName &&
            (field.timeInterval || field.customTimeInterval)) {
            const baseId = `${field.table}_${field.timeIntervalBaseDimensionName}`;
            if (field.customTimeInterval) {
                map[baseId] = field.label;
            }
            else if (field.timeInterval) {
                const granularity = timeFrames_1.timeFrameToDateGranularityMap[field.timeInterval];
                if (granularity) {
                    // A project `granularity_labels` override is baked onto the
                    // dimension as `timeIntervalLabel`; render it verbatim.
                    map[baseId] = field.timeIntervalLabel
                        ? { verbatim: field.timeIntervalLabel }
                        : granularity;
                }
            }
        }
    }
    return map;
};
exports.getGranularityMapFromItems = getGranularityMapFromItems;
const resolveGranularityInLabel = (label, granularityMap) => {
    if (label === undefined)
        return undefined;
    if (Object.keys(granularityMap).length === 0)
        return label;
    return label.replaceAll(GRANULARITY_PATTERN, (match, fieldBaseId) => {
        const granularity = granularityMap[fieldBaseId];
        if (!granularity)
            return match;
        // Overridden standard grains carry a verbatim label; everything
        // else (DateGranularity, custom label) is lowercased as before.
        if (typeof granularity === 'object')
            return granularity.verbatim;
        return (0, timeFrames_1.getGranularityReferenceValue)(granularity);
    });
};
exports.resolveGranularityInLabel = resolveGranularityInLabel;
//# sourceMappingURL=bigNumber.js.map