"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getDateZoomCapabilities = exports.resolveToBaseTimeDimension = exports.getAllDimensionsMap = exports.resolveBaseDimension = exports.getTimeDimensionsMap = void 0;
const field_1 = require("../types/field");
const item_1 = require("./item");
const timeFrames_1 = require("./timeFrames");
/**
 * Collects all DATE and TIMESTAMP dimensions from an explore, keyed by item ID.
 */
const getTimeDimensionsMap = (explore) => {
    const map = {};
    for (const table of Object.values(explore.tables)) {
        for (const dim of Object.values(table.dimensions)) {
            if (dim.type === field_1.DimensionType.TIMESTAMP ||
                dim.type === field_1.DimensionType.DATE) {
                map[(0, item_1.getItemId)(dim)] = dim;
            }
        }
    }
    return map;
};
exports.getTimeDimensionsMap = getTimeDimensionsMap;
/**
 * Resolves a time-interval dimension (e.g. `order_date_month`) to its base
 * dimension (e.g. `order_date`). If the dimension has no `timeInterval` or the
 * base can't be parsed from the ID, returns the dimension itself.
 *
 * When the base dimension ID is resolved but not found in the map, returns
 * `undefined` — callers should handle this as an unexpected state.
 */
const resolveBaseDimension = (dimId, dim, timeDimensionsMap) => {
    const { baseDimensionId } = (0, timeFrames_1.getDateDimension)(dimId);
    if (dim.timeInterval && baseDimensionId) {
        return timeDimensionsMap[baseDimensionId];
    }
    return dim;
};
exports.resolveBaseDimension = resolveBaseDimension;
/**
 * Collects all dimensions from an explore, keyed by item ID.
 */
const getAllDimensionsMap = (explore) => {
    const map = {};
    for (const table of Object.values(explore.tables)) {
        for (const dim of Object.values(table.dimensions)) {
            map[(0, item_1.getItemId)(dim)] = dim;
        }
    }
    return map;
};
exports.getAllDimensionsMap = getAllDimensionsMap;
/**
 * Resolves any dimension from the metric query to its base time dimension.
 * Handles three cases:
 * - Standard time-interval dims (e.g. order_date_month) → resolves via getDateDimension
 * - Custom granularity dims (e.g. order_date_fiscal_quarter) → resolves via timeIntervalBaseDimensionName
 * - Plain date/timestamp dims → returns the dimension itself
 */
const resolveToBaseTimeDimension = (dimId, allDimensionsMap, timeDimensionsMap) => {
    const dim = allDimensionsMap[dimId];
    if (!dim)
        return undefined;
    // Custom granularity or non-date time-interval dimension (e.g. QUARTER_NAME
    // is STRING-typed) — resolve via timeIntervalBaseDimensionName
    if (dim.timeIntervalBaseDimensionName) {
        return timeDimensionsMap[(0, item_1.getItemId)({
            table: dim.table,
            name: dim.timeIntervalBaseDimensionName,
        })];
    }
    // Only process DATE/TIMESTAMP dimensions from here
    const timeDim = timeDimensionsMap[dimId];
    if (!timeDim)
        return undefined;
    return (0, exports.resolveBaseDimension)(dimId, timeDim, timeDimensionsMap);
};
exports.resolveToBaseTimeDimension = resolveToBaseTimeDimension;
const getDateZoomCapabilities = (explore, metricQuery) => {
    const allDimensions = Object.values(explore.tables).flatMap((t) => Object.values(t.dimensions));
    const allDimensionsMap = (0, exports.getAllDimensionsMap)(explore);
    const timeDimensionsMap = (0, exports.getTimeDimensionsMap)(explore);
    let hasTimestampDimension = false;
    let hasDateDimension = false;
    // hasDateDimension / hasTimestampDimension reflect the chart's own usage
    // (used to gate sub-day standard granularities on DATE-only dashboards),
    // so they stay scoped to dims referenced by the chart's metricQuery.
    for (const dimId of metricQuery.dimensions) {
        const baseDim = (0, exports.resolveToBaseTimeDimension)(dimId, allDimensionsMap, timeDimensionsMap);
        if (baseDim) {
            if (baseDim.type === field_1.DimensionType.TIMESTAMP) {
                hasTimestampDimension = true;
            }
            if (baseDim.type === field_1.DimensionType.DATE) {
                hasDateDimension = true;
            }
        }
    }
    // Custom granularities are sourced from any `customTimeInterval` dim in
    // the explore, not just siblings of the chart's currently-queried base.
    // The previous chart-query intersection here stripped configured customs
    // whenever a dashboard tile's query didn't currently reference the owning
    // base dim — and was also timing-sensitive, since metricQuery loads
    // asynchronously alongside the explore (PROD-7514, regression of
    // PROD-6788).
    const availableCustomGranularities = {};
    for (const dim of allDimensions) {
        if (dim.customTimeInterval) {
            availableCustomGranularities[dim.customTimeInterval] = dim.label;
        }
    }
    return {
        availableCustomGranularities,
        hasTimestampDimension,
        hasDateDimension,
    };
};
exports.getDateZoomCapabilities = getDateZoomCapabilities;
//# sourceMappingURL=dateZoom.js.map