"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getGranularityRank = exports.buildPopAdditionalMetric = exports.getPopPeriodLabel = exports.buildPopAdditionalMetricName = exports.isSupportedPeriodOverPeriodGranularity = exports.periodOverPeriodGranularityLabels = exports.validPeriodOverPeriodGranularities = void 0;
const uuid_1 = require("uuid");
const item_1 = require("../utils/item");
const timeFrames_1 = require("../utils/timeFrames");
const timeFrames_2 = require("./timeFrames");
exports.validPeriodOverPeriodGranularities = [
    timeFrames_2.TimeFrames.DAY,
    timeFrames_2.TimeFrames.WEEK,
    timeFrames_2.TimeFrames.MONTH,
    timeFrames_2.TimeFrames.QUARTER,
    timeFrames_2.TimeFrames.YEAR,
];
exports.periodOverPeriodGranularityLabels = {
    [timeFrames_2.TimeFrames.YEAR]: 'Year',
    [timeFrames_2.TimeFrames.QUARTER]: 'Quarter',
    [timeFrames_2.TimeFrames.MONTH]: 'Month',
    [timeFrames_2.TimeFrames.WEEK]: 'Week',
    [timeFrames_2.TimeFrames.DAY]: 'Day',
    [timeFrames_2.TimeFrames.HOUR]: 'Hour',
    [timeFrames_2.TimeFrames.MINUTE]: 'Minute',
    [timeFrames_2.TimeFrames.SECOND]: 'Second',
    [timeFrames_2.TimeFrames.MILLISECOND]: 'Millisecond',
    [timeFrames_2.TimeFrames.RAW]: 'Raw',
    [timeFrames_2.TimeFrames.DAY_OF_WEEK_INDEX]: 'Day of Week Index',
    [timeFrames_2.TimeFrames.DAY_OF_MONTH_NUM]: 'Day of Month',
    [timeFrames_2.TimeFrames.DAY_OF_YEAR_NUM]: 'Day of Year',
    [timeFrames_2.TimeFrames.WEEK_NUM]: 'Week Number',
    [timeFrames_2.TimeFrames.MONTH_NUM]: 'Month Number',
    [timeFrames_2.TimeFrames.QUARTER_NUM]: 'Quarter Number',
    [timeFrames_2.TimeFrames.YEAR_NUM]: 'Year Number',
    [timeFrames_2.TimeFrames.DAY_OF_WEEK_NAME]: 'Day of Week Name',
    [timeFrames_2.TimeFrames.MONTH_NAME]: 'Month Name',
    [timeFrames_2.TimeFrames.QUARTER_NAME]: 'Quarter Name',
    [timeFrames_2.TimeFrames.HOUR_OF_DAY_NUM]: 'Hour of Day',
    [timeFrames_2.TimeFrames.MINUTE_OF_HOUR_NUM]: 'Minute of Hour',
};
const isSupportedPeriodOverPeriodGranularity = (granularity) => exports.validPeriodOverPeriodGranularities.includes(granularity);
exports.isSupportedPeriodOverPeriodGranularity = isSupportedPeriodOverPeriodGranularity;
const buildPopAdditionalMetricName = (baseMetricName) => `${baseMetricName}__pop`;
exports.buildPopAdditionalMetricName = buildPopAdditionalMetricName;
const getPopPeriodLabel = (granularity, periodOffset) => {
    const label = timeFrames_1.timeFrameConfigs[granularity]?.getLabel() || granularity;
    return periodOffset === 1
        ? `Previous ${String(label).toLowerCase()}`
        : `${periodOffset} ${String(label).toLowerCase()}s ago`;
};
exports.getPopPeriodLabel = getPopPeriodLabel;
const buildPopAdditionalMetric = ({ metric, timeDimensionId, granularity, periodOffset, }) => {
    const baseMetricId = (0, item_1.getItemId)(metric);
    const popName = (0, exports.buildPopAdditionalMetricName)(metric.name);
    const popMetricId = (0, item_1.getItemId)({ table: metric.table, name: popName });
    const additionalMetric = {
        uuid: (0, uuid_1.v4)(),
        table: metric.table,
        name: popName,
        label: `${metric.label} (${(0, exports.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 };
};
exports.buildPopAdditionalMetric = buildPopAdditionalMetric;
// Granularity order from finest to coarsest (lower index = finer)
const GRANULARITY_ORDER = [
    timeFrames_2.TimeFrames.DAY,
    timeFrames_2.TimeFrames.WEEK,
    timeFrames_2.TimeFrames.MONTH,
    timeFrames_2.TimeFrames.QUARTER,
    timeFrames_2.TimeFrames.YEAR,
];
const getGranularityRank = (granularity) => {
    const index = GRANULARITY_ORDER.indexOf(granularity);
    return index === -1 ? Infinity : index;
};
exports.getGranularityRank = getGranularityRank;
//# sourceMappingURL=periodOverPeriodComparison.js.map