import { v4 as uuidv4 } from 'uuid';
import { getItemId } from '../utils/item';
import { timeFrameConfigs } from '../utils/timeFrames';
import { TimeFrames } from './timeFrames';
export const validPeriodOverPeriodGranularities = [
    TimeFrames.DAY,
    TimeFrames.WEEK,
    TimeFrames.MONTH,
    TimeFrames.QUARTER,
    TimeFrames.YEAR,
];
export const periodOverPeriodGranularityLabels = {
    [TimeFrames.YEAR]: 'Year',
    [TimeFrames.QUARTER]: 'Quarter',
    [TimeFrames.MONTH]: 'Month',
    [TimeFrames.WEEK]: 'Week',
    [TimeFrames.DAY]: 'Day',
    [TimeFrames.HOUR]: 'Hour',
    [TimeFrames.MINUTE]: 'Minute',
    [TimeFrames.SECOND]: 'Second',
    [TimeFrames.MILLISECOND]: 'Millisecond',
    [TimeFrames.RAW]: 'Raw',
    [TimeFrames.DAY_OF_WEEK_INDEX]: 'Day of Week Index',
    [TimeFrames.DAY_OF_MONTH_NUM]: 'Day of Month',
    [TimeFrames.DAY_OF_YEAR_NUM]: 'Day of Year',
    [TimeFrames.WEEK_NUM]: 'Week Number',
    [TimeFrames.MONTH_NUM]: 'Month Number',
    [TimeFrames.QUARTER_NUM]: 'Quarter Number',
    [TimeFrames.YEAR_NUM]: 'Year Number',
    [TimeFrames.DAY_OF_WEEK_NAME]: 'Day of Week Name',
    [TimeFrames.MONTH_NAME]: 'Month Name',
    [TimeFrames.QUARTER_NAME]: 'Quarter Name',
    [TimeFrames.HOUR_OF_DAY_NUM]: 'Hour of Day',
    [TimeFrames.MINUTE_OF_HOUR_NUM]: 'Minute of Hour',
};
export const isSupportedPeriodOverPeriodGranularity = (granularity) => validPeriodOverPeriodGranularities.includes(granularity);
export const buildPopAdditionalMetricName = (baseMetricName) => `${baseMetricName}__pop`;
export const getPopPeriodLabel = (granularity, periodOffset) => {
    const label = timeFrameConfigs[granularity]?.getLabel() || granularity;
    return periodOffset === 1
        ? `Previous ${String(label).toLowerCase()}`
        : `${periodOffset} ${String(label).toLowerCase()}s ago`;
};
export const buildPopAdditionalMetric = ({ metric, timeDimensionId, granularity, periodOffset, }) => {
    const baseMetricId = getItemId(metric);
    const popName = buildPopAdditionalMetricName(metric.name);
    const popMetricId = getItemId({ table: metric.table, name: popName });
    const additionalMetric = {
        uuid: uuidv4(),
        table: metric.table,
        name: popName,
        label: `${metric.label} (${getPopPeriodLabel(granularity, periodOffset)})`,
        description: metric.description,
        type: metric.type,
        sql: metric.sql,
        hidden: true,
        round: metric.round,
        compact: metric.compact,
        format: metric.format,
        generationType: 'periodOverPeriod',
        baseMetricId,
        timeDimensionId,
        granularity,
        periodOffset,
    };
    return { additionalMetric, metricId: popMetricId };
};
// Granularity order from finest to coarsest (lower index = finer)
const GRANULARITY_ORDER = [
    TimeFrames.DAY,
    TimeFrames.WEEK,
    TimeFrames.MONTH,
    TimeFrames.QUARTER,
    TimeFrames.YEAR,
];
export const getGranularityRank = (granularity) => {
    const index = GRANULARITY_ORDER.indexOf(granularity);
    return index === -1 ? Infinity : index;
};
//# sourceMappingURL=periodOverPeriodComparison.js.map