"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const field_1 = require("../types/field");
const periodOverPeriodComparison_1 = require("../types/periodOverPeriodComparison");
const timeFrames_1 = require("../types/timeFrames");
const additionalMetrics_1 = require("./additionalMetrics");
const baseTable = {
    name: 'orders',
    label: 'Orders',
    database: 'db',
    schema: 'schema',
    sqlTable: 'orders',
    dimensions: {},
    metrics: {},
    lineageGraph: {},
};
describe('convertAdditionalMetric — PoP formatOptions inheritance', () => {
    it('preserves formatOptions when converting a PoP additional metric', () => {
        const baseMetric = {
            table: 'orders',
            name: 'total_revenue',
            label: 'Revenue',
            description: 'Total revenue',
            type: field_1.MetricType.SUM,
            sql: '${TABLE}.amount',
            formatOptions: {
                type: field_1.CustomFormatType.CURRENCY,
                currency: 'USD',
            },
        };
        const { additionalMetric: pop } = (0, periodOverPeriodComparison_1.buildPopAdditionalMetric)({
            metric: baseMetric,
            timeDimensionId: 'orders_order_date_week',
            granularity: timeFrames_1.TimeFrames.WEEK,
            periodOffset: 1,
        });
        expect(pop.formatOptions).toEqual({
            type: field_1.CustomFormatType.CURRENCY,
            currency: 'USD',
        });
        const result = (0, additionalMetrics_1.convertAdditionalMetric)({
            additionalMetric: pop,
            table: baseTable,
        });
        expect(result.formatOptions).toEqual({
            type: field_1.CustomFormatType.CURRENCY,
            currency: 'USD',
        });
    });
});
describe('convertAdditionalMetric — baseDimensionType plumbing', () => {
    const tableWithDims = {
        ...baseTable,
        dimensions: {
            order_date: {
                name: 'order_date',
                type: field_1.DimensionType.DATE,
            },
            order_date_day: {
                name: 'order_date_day',
                type: field_1.DimensionType.DATE,
                timeInterval: timeFrames_1.TimeFrames.DAY,
            },
            amount: {
                name: 'amount',
                type: field_1.DimensionType.NUMBER,
            },
            created_at: {
                name: 'created_at',
                type: field_1.DimensionType.TIMESTAMP,
            },
        },
    };
    it('resolves baseDimensionType/Interval for a MIN/MAX over a day-grain DATE dimension', () => {
        const result = (0, additionalMetrics_1.convertAdditionalMetric)({
            additionalMetric: {
                table: 'orders',
                name: 'max_day',
                type: field_1.MetricType.MAX,
                sql: "DATE_TRUNC('DAY', ${TABLE}.order_date)",
                baseDimensionName: 'order_date_day',
            },
            table: tableWithDims,
        });
        expect(result.baseDimensionType).toEqual(field_1.DimensionType.DATE);
        expect(result.baseDimensionTimeInterval).toEqual(timeFrames_1.TimeFrames.DAY);
    });
    it('leaves it undefined when there is no baseDimensionName (free-SQL custom metric)', () => {
        const result = (0, additionalMetrics_1.convertAdditionalMetric)({
            additionalMetric: {
                table: 'orders',
                name: 'max_expr',
                type: field_1.MetricType.MAX,
                sql: '${TABLE}.order_date',
            },
            table: tableWithDims,
        });
        expect(result.baseDimensionType).toBeUndefined();
        expect(result.baseDimensionTimeInterval).toBeUndefined();
    });
    it('leaves it undefined for a non MIN/MAX metric', () => {
        const result = (0, additionalMetrics_1.convertAdditionalMetric)({
            additionalMetric: {
                table: 'orders',
                name: 'count_day',
                type: field_1.MetricType.COUNT_DISTINCT,
                sql: "DATE_TRUNC('DAY', ${TABLE}.order_date)",
                baseDimensionName: 'order_date_day',
            },
            table: tableWithDims,
        });
        expect(result.baseDimensionType).toBeUndefined();
    });
    it('leaves it undefined for a non-temporal base (MAX over a number)', () => {
        const result = (0, additionalMetrics_1.convertAdditionalMetric)({
            additionalMetric: {
                table: 'orders',
                name: 'max_amount',
                type: field_1.MetricType.MAX,
                sql: '${TABLE}.amount',
                baseDimensionName: 'amount',
            },
            table: tableWithDims,
        });
        expect(result.baseDimensionType).toBeUndefined();
    });
    it('records a TIMESTAMP base for a MIN/MAX over a TIMESTAMP dimension', () => {
        const result = (0, additionalMetrics_1.convertAdditionalMetric)({
            additionalMetric: {
                table: 'orders',
                name: 'min_created_at',
                type: field_1.MetricType.MIN,
                sql: '${TABLE}.created_at',
                baseDimensionName: 'created_at',
            },
            table: tableWithDims,
        });
        expect(result.baseDimensionType).toEqual(field_1.DimensionType.TIMESTAMP);
        expect(result.baseDimensionTimeInterval).toBeUndefined();
    });
});
//# sourceMappingURL=additionalMetrics.test.js.map