import { BinType, friendlyName, isCustomBinDimension, isCustomDimension, isCustomSqlDimension, } from './field';
export const isAdditionalMetric = (value) => value?.table &&
    value?.name &&
    !value?.fieldType &&
    !isCustomDimension(value);
/**
 * 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.
 */
export 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');
};
export const isPeriodOverPeriodAdditionalMetric = (value) => isAdditionalMetric(value) &&
    hasPeriodOverPeriodGeneratedMetricMetadata(value);
export const hasFormatOptions = (value) => !!value.formatOptions;
export const getCustomMetricDimensionId = (metric) => `${metric.table}_${metric.baseDimensionName}`;
export const getAdditionalMetricLabel = (item) => `${friendlyName(item.table)} ${item.label}`;
export const countCustomDimensionsInMetricQuery = (metricQuery) => ({
    numFixedWidthBinCustomDimensions: metricQuery.customDimensions?.filter((dimension) => isCustomBinDimension(dimension) &&
        dimension.binType === BinType.FIXED_NUMBER).length || 0,
    numFixedBinsBinCustomDimensions: metricQuery.customDimensions?.filter((dimension) => isCustomBinDimension(dimension) &&
        dimension.binType === BinType.FIXED_WIDTH).length || 0,
    numCustomRangeBinCustomDimensions: metricQuery.customDimensions?.filter((dimension) => isCustomBinDimension(dimension) &&
        dimension.binType === BinType.CUSTOM_RANGE).length || 0,
    numCustomGroupBinCustomDimensions: metricQuery.customDimensions?.filter((dimension) => isCustomBinDimension(dimension) &&
        dimension.binType === BinType.CUSTOM_GROUP).length || 0,
    numCustomSqlDimensions: metricQuery.customDimensions?.filter((dimension) => isCustomSqlDimension(dimension)).length || 0,
});
export const hasCustomBinDimension = (metricQuery) => metricQuery?.customDimensions &&
    metricQuery.customDimensions.some((dimension) => isCustomBinDimension(dimension));
//# sourceMappingURL=metricQuery.js.map