"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.hasCustomBinDimension = exports.countCustomDimensionsInMetricQuery = exports.getAdditionalMetricLabel = exports.getCustomMetricDimensionId = exports.hasFormatOptions = exports.isPeriodOverPeriodAdditionalMetric = exports.hasPeriodOverPeriodGeneratedMetricMetadata = exports.isAdditionalMetric = void 0;
const field_1 = require("./field");
const isAdditionalMetric = (value) => value?.table &&
    value?.name &&
    !value?.fieldType &&
    !(0, field_1.isCustomDimension)(value);
exports.isAdditionalMetric = isAdditionalMetric;
/**
 * Type guard for Period-over-Period generated metric metadata.
 *
 * Note: This metadata can exist on both `AdditionalMetric` objects (query-time)
 * and the converted `Metric` objects in `ItemsMap` (render-time), since we
 * preserve these fields during conversion.
 */
const hasPeriodOverPeriodGeneratedMetricMetadata = (value) => {
    if (!value || typeof value !== 'object')
        return false;
    if (!('generationType' in value) ||
        !('baseMetricId' in value) ||
        !('timeDimensionId' in value) ||
        !('granularity' in value) ||
        !('periodOffset' in value))
        return false;
    const record = value;
    return (record.generationType === 'periodOverPeriod' &&
        typeof record.baseMetricId === 'string' &&
        typeof record.timeDimensionId === 'string' &&
        typeof record.granularity === 'string' &&
        typeof record.periodOffset === 'number');
};
exports.hasPeriodOverPeriodGeneratedMetricMetadata = hasPeriodOverPeriodGeneratedMetricMetadata;
const isPeriodOverPeriodAdditionalMetric = (value) => (0, exports.isAdditionalMetric)(value) &&
    (0, exports.hasPeriodOverPeriodGeneratedMetricMetadata)(value);
exports.isPeriodOverPeriodAdditionalMetric = isPeriodOverPeriodAdditionalMetric;
const hasFormatOptions = (value) => !!value.formatOptions;
exports.hasFormatOptions = hasFormatOptions;
const getCustomMetricDimensionId = (metric) => `${metric.table}_${metric.baseDimensionName}`;
exports.getCustomMetricDimensionId = getCustomMetricDimensionId;
const getAdditionalMetricLabel = (item) => `${(0, field_1.friendlyName)(item.table)} ${item.label}`;
exports.getAdditionalMetricLabel = getAdditionalMetricLabel;
const countCustomDimensionsInMetricQuery = (metricQuery) => ({
    numFixedWidthBinCustomDimensions: metricQuery.customDimensions?.filter((dimension) => (0, field_1.isCustomBinDimension)(dimension) &&
        dimension.binType === field_1.BinType.FIXED_NUMBER).length || 0,
    numFixedBinsBinCustomDimensions: metricQuery.customDimensions?.filter((dimension) => (0, field_1.isCustomBinDimension)(dimension) &&
        dimension.binType === field_1.BinType.FIXED_WIDTH).length || 0,
    numCustomRangeBinCustomDimensions: metricQuery.customDimensions?.filter((dimension) => (0, field_1.isCustomBinDimension)(dimension) &&
        dimension.binType === field_1.BinType.CUSTOM_RANGE).length || 0,
    numCustomGroupBinCustomDimensions: metricQuery.customDimensions?.filter((dimension) => (0, field_1.isCustomBinDimension)(dimension) &&
        dimension.binType === field_1.BinType.CUSTOM_GROUP).length || 0,
    numCustomSqlDimensions: metricQuery.customDimensions?.filter((dimension) => (0, field_1.isCustomSqlDimension)(dimension)).length || 0,
});
exports.countCustomDimensionsInMetricQuery = countCustomDimensionsInMetricQuery;
const hasCustomBinDimension = (metricQuery) => metricQuery?.customDimensions &&
    metricQuery.customDimensions.some((dimension) => (0, field_1.isCustomBinDimension)(dimension));
exports.hasCustomBinDimension = hasCustomBinDimension;
//# sourceMappingURL=metricQuery.js.map