"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const field_1 = require("../../types/field");
const item_1 = require("../../utils/item");
const numberMetricDependencies_1 = require("./numberMetricDependencies");
const makeMetric = (overrides) => ({
    ...overrides,
    fieldType: field_1.FieldType.METRIC,
    type: overrides.type,
    name: overrides.name,
    label: overrides.label ?? overrides.name,
    table: overrides.table ?? 'orders',
    tableLabel: overrides.tableLabel ?? 'Orders',
    sql: overrides.sql ?? 'count(*)',
    compiledSql: overrides.compiledSql ?? overrides.sql ?? 'count(*)',
    tablesReferences: overrides.tablesReferences ?? ['orders'],
    hidden: overrides.hidden ?? false,
});
const makeTables = (metrics) => metrics.reduce((acc, metric) => {
    acc[metric.table] = acc[metric.table] ?? {
        name: metric.table,
        dimensions: {},
        metrics: {},
    };
    acc[metric.table].metrics[metric.name] = metric;
    return acc;
}, {});
describe('analyzePreAggregateNumberMetricDependencies', () => {
    it('collects direct, transitive, and leaf metric dependencies', () => {
        const revenue = makeMetric({
            name: 'revenue',
            type: field_1.MetricType.SUM,
            sql: '${TABLE}.amount',
        });
        const cogs = makeMetric({
            name: 'cogs',
            type: field_1.MetricType.SUM,
            sql: '${TABLE}.cost',
        });
        const grossProfit = makeMetric({
            name: 'gross_profit',
            type: field_1.MetricType.NUMBER,
            sql: '${revenue} - ${cogs}',
        });
        const margin = makeMetric({
            name: 'margin',
            type: field_1.MetricType.NUMBER,
            sql: '${gross_profit} / NULLIF(${revenue}, 0)',
        });
        expect((0, numberMetricDependencies_1.analyzePreAggregateNumberMetricDependencies)({
            metric: margin,
            tables: makeTables([revenue, cogs, grossProfit, margin]),
        })).toEqual({
            isValid: true,
            directReferencedMetricFieldIds: [
                (0, item_1.getItemId)(grossProfit),
                (0, item_1.getItemId)(revenue),
            ],
            transitiveReferencedMetricFieldIds: [
                (0, item_1.getItemId)(grossProfit),
                (0, item_1.getItemId)(revenue),
                (0, item_1.getItemId)(cogs),
            ],
            leafMetricFieldIds: [(0, item_1.getItemId)(revenue), (0, item_1.getItemId)(cogs)],
        });
    });
    it('rejects number metrics that aggregate over metric references', () => {
        const maxValue = makeMetric({
            name: 'max_value',
            type: field_1.MetricType.MAX,
            sql: '${TABLE}.value',
        });
        const sumOfMaxValue = makeMetric({
            name: 'sum_of_max_value',
            type: field_1.MetricType.NUMBER,
            sql: 'sum(${max_value})',
        });
        expect((0, numberMetricDependencies_1.analyzePreAggregateNumberMetricDependencies)({
            metric: sumOfMaxValue,
            tables: makeTables([maxValue, sumOfMaxValue]),
        })).toEqual(expect.objectContaining({
            isValid: false,
            reason: numberMetricDependencies_1.PreAggregateNumberMetricDependencyIneligibilityReason.NESTED_AGGREGATE_METRIC_REFERENCE,
        }));
    });
    it('rejects number metrics whose leaf dependencies are not materializable', () => {
        const distinctCustomerCount = makeMetric({
            name: 'distinct_customer_count',
            type: field_1.MetricType.COUNT_DISTINCT,
            sql: '${TABLE}.customer_id',
        });
        const customerDelta = makeMetric({
            name: 'customer_delta',
            type: field_1.MetricType.NUMBER,
            sql: '${distinct_customer_count} - 1',
        });
        expect((0, numberMetricDependencies_1.analyzePreAggregateNumberMetricDependencies)({
            metric: customerDelta,
            tables: makeTables([distinctCustomerCount, customerDelta]),
        })).toEqual(expect.objectContaining({
            isValid: false,
            reason: numberMetricDependencies_1.PreAggregateNumberMetricDependencyIneligibilityReason.UNSUPPORTED_LEAF_METRIC_TYPE,
            ineligibleMetricFieldId: (0, item_1.getItemId)(distinctCustomerCount),
        }));
    });
});
//# sourceMappingURL=numberMetricDependencies.test.js.map