"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const dayjs_1 = tslib_1.__importDefault(require("dayjs"));
const field_1 = require("../types/field");
const filter_1 = require("../types/filter");
const timeFrames_1 = require("../types/timeFrames");
const metricsExplorer_1 = require("./metricsExplorer");
const dimRegion = {
    name: 'region',
    table: 'orders',
    fieldType: field_1.FieldType.DIMENSION,
    type: field_1.DimensionType.STRING,
    label: 'Region',
    sql: '${TABLE}.region',
    compiledSql: '"orders".region',
    tablesReferences: ['orders'],
    tableLabel: 'Orders',
    hidden: false,
};
const formatDate = (date) => (0, dayjs_1.default)(date).format('YYYY-MM-DD');
describe('getDateCalcUtils', () => {
    test('day shift', () => {
        const { back, forward } = (0, metricsExplorer_1.getDateCalcUtils)(timeFrames_1.TimeFrames.DAY, timeFrames_1.TimeFrames.DAY);
        const base = new Date('2026-03-31T00:00:00Z');
        expect(formatDate(back(base))).toBe('2026-03-30');
        expect(formatDate(forward(base))).toBe('2026-04-01');
    });
    test('week shift', () => {
        const { back, forward } = (0, metricsExplorer_1.getDateCalcUtils)(timeFrames_1.TimeFrames.WEEK, timeFrames_1.TimeFrames.WEEK);
        const base = new Date('2026-03-31T00:00:00Z');
        expect(formatDate(back(base))).toBe('2026-03-24');
        expect(formatDate(forward(base))).toBe('2026-04-07');
    });
    test('month shift clamps end of month', () => {
        const { back, forward } = (0, metricsExplorer_1.getDateCalcUtils)(timeFrames_1.TimeFrames.MONTH, timeFrames_1.TimeFrames.MONTH);
        const base = new Date('2026-03-31T00:00:00Z');
        expect(formatDate(back(base))).toBe('2026-02-28');
        expect(formatDate(forward(new Date('2026-01-31T00:00:00Z')))).toBe('2026-02-28');
    });
    test('year shift clamps leap day', () => {
        const { back } = (0, metricsExplorer_1.getDateCalcUtils)(timeFrames_1.TimeFrames.YEAR, timeFrames_1.TimeFrames.YEAR);
        const leap = new Date('2024-02-29T00:00:00Z');
        expect(formatDate(back(leap))).toBe('2023-02-28');
    });
});
describe('getInitialDefaultSegment', () => {
    test('resolves the default segment to the available dimension fieldId', () => {
        expect((0, metricsExplorer_1.getInitialDefaultSegment)({
            spotlightDefaultSegment: 'region',
            tableName: 'orders',
        }, [dimRegion])).toBe('orders_region');
    });
    test('resolves a joined (table-qualified) default segment fieldId', () => {
        expect((0, metricsExplorer_1.getInitialDefaultSegment)({
            spotlightDefaultSegment: 'orders.region',
            tableName: 'metrics',
        }, [dimRegion])).toBe('orders_region');
    });
    test('returns null when the default segment is not available', () => {
        expect((0, metricsExplorer_1.getInitialDefaultSegment)({
            spotlightDefaultSegment: 'missing',
            tableName: 'orders',
        }, [dimRegion])).toBeNull();
    });
    test('returns null when no default is set', () => {
        expect((0, metricsExplorer_1.getInitialDefaultSegment)({ tableName: 'orders' }, [
            dimRegion,
        ])).toBeNull();
    });
});
describe('getInitialDefaultFilterRule', () => {
    test('builds a FilterRule targeting the available dimension fieldId', () => {
        const rule = (0, metricsExplorer_1.getInitialDefaultFilterRule)({
            tableName: 'orders',
            spotlightDefaultFilter: {
                id: 'x',
                target: { fieldRef: 'region' },
                operator: filter_1.FilterOperator.EQUALS,
                values: ['EMEA'],
                required: false,
            },
        }, [dimRegion]);
        expect(rule?.operator).toBe(filter_1.FilterOperator.EQUALS);
        expect(rule?.values).toEqual(['EMEA']);
        expect(rule?.target.fieldId).toBe('orders_region');
        expect(rule?.required).toBe(false);
    });
    test('resolves a joined (table-qualified) fieldRef to its fieldId', () => {
        const rule = (0, metricsExplorer_1.getInitialDefaultFilterRule)({
            tableName: 'metrics',
            spotlightDefaultFilter: {
                id: 'x',
                target: { fieldRef: 'orders.region' },
                operator: filter_1.FilterOperator.NOT_EQUALS,
                values: ['EMEA'],
            },
        }, [dimRegion]);
        expect(rule?.target.fieldId).toBe('orders_region');
        expect(rule?.operator).toBe(filter_1.FilterOperator.NOT_EQUALS);
    });
    test('returns undefined when the filter dimension is unavailable', () => {
        expect((0, metricsExplorer_1.getInitialDefaultFilterRule)({
            tableName: 'orders',
            spotlightDefaultFilter: {
                id: 'x',
                target: { fieldRef: 'missing' },
                operator: filter_1.FilterOperator.EQUALS,
                values: ['v'],
            },
        }, [dimRegion])).toBeUndefined();
    });
    test('returns undefined when no default filter is set', () => {
        expect((0, metricsExplorer_1.getInitialDefaultFilterRule)({ tableName: 'orders' }, [dimRegion])).toBeUndefined();
    });
});
describe('getDefaultTimeDimension', () => {
    const metric = {
        name: 'revenue',
        table: 'orders',
        fieldType: field_1.FieldType.METRIC,
        type: field_1.MetricType.SUM,
        label: 'Revenue',
        tableLabel: 'Orders',
        sql: '${TABLE}.revenue',
        compiledSql: 'SUM("orders".revenue)',
        tablesReferences: ['orders'],
        hidden: false,
    };
    // Malformed dbt YAML (wrong keys inside default_time_dimension) compiles
    // to an empty object; treat it as absent instead of returning
    // { field: undefined } that crashes downstream getItemId calls.
    test('ignores a metric-level default time dimension without a field', () => {
        expect((0, metricsExplorer_1.getDefaultTimeDimension)({
            ...metric,
            defaultTimeDimension: {},
        })).toBeUndefined();
    });
    test('ignores a model-level default time dimension without a field', () => {
        const table = {
            name: 'orders',
            defaultTimeDimension: {},
        };
        expect((0, metricsExplorer_1.getDefaultTimeDimension)(metric, table)).toBeUndefined();
    });
    test('returns the metric-level default time dimension when valid', () => {
        expect((0, metricsExplorer_1.getDefaultTimeDimension)({
            ...metric,
            defaultTimeDimension: {
                field: 'created_at',
                interval: timeFrames_1.TimeFrames.DAY,
            },
        })).toEqual({ field: 'created_at', interval: timeFrames_1.TimeFrames.DAY });
    });
});
//# sourceMappingURL=metricsExplorer.test.js.map