"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const item_1 = require("../utils/item");
const field_1 = require("./field");
describe('field util functions', () => {
    describe('convertFieldRefToFieldId', () => {
        it('should convert field references to field ids', async () => {
            expect((0, field_1.convertFieldRefToFieldId)('table.field')).toEqual('table_field');
            expect((0, field_1.convertFieldRefToFieldId)('table.field', 'fallback')).toEqual('table_field');
            expect((0, field_1.convertFieldRefToFieldId)('field', 'fallback')).toEqual('fallback_field');
        });
        it('should throw error when field reference doesnt have a table and there is no fallback', async () => {
            expect(() => (0, field_1.convertFieldRefToFieldId)('field')).toThrowError('Table calculation contains an invalid reference: field. References must be of the format "table.field"');
        });
    });
    describe('getItemId', () => {
        it('should return item id based on table and name', async () => {
            expect((0, item_1.getItemId)({ table: 'table', name: 'field' })).toEqual('table_field');
            expect((0, item_1.getItemId)({ table: 'table', name: 'field.nested' })).toEqual('table_field__nested');
            expect((0, item_1.getItemId)({ table: 'table', name: 'field_test' })).toEqual('table_field_test');
            expect((0, item_1.getItemId)({ table: 'table_test', name: 'field' })).toEqual('table_test_field');
            expect((0, item_1.getItemId)({ table: 'table_test', name: 'field_test' })).toEqual('table_test_field_test');
            expect((0, item_1.getItemId)({ table: 'table_test', name: 'field.nested' })).toEqual('table_test_field__nested');
            expect((0, item_1.getItemId)({ table: 'table_test', name: 'field_test.nested' })).toEqual('table_test_field_test__nested');
        });
        it('should return item id for custom dimensions', async () => {
            const bin = {
                id: 'custom_dimension_id',
                name: 'custom_dimension_name',
                type: field_1.CustomDimensionType.BIN,
                dimensionId: 'table1_dim1', // Parent dimension id
                binType: field_1.BinType.FIXED_NUMBER,
                binNumber: 5,
                table: 'table1',
            };
            expect((0, item_1.getItemId)(bin)).toEqual('custom_dimension_id');
            const customSqlDimension = {
                id: 'custom_dimension_id',
                name: 'custom_dimension_name',
                type: field_1.CustomDimensionType.SQL,
                table: 'table1',
                sql: 'example',
                dimensionType: field_1.DimensionType.STRING,
            };
            expect((0, item_1.getItemId)(customSqlDimension)).toEqual('custom_dimension_id');
        });
        it('should return item id for table calculation', async () => {
            const tc = {
                name: 'calc2',
                displayName: '',
                sql: 'dim reference ${table1.dim1}',
            };
            expect((0, item_1.getItemId)(tc)).toEqual('calc2');
        });
        it('should return item id for table calculation', async () => {
            const tc = {
                name: 'calc2',
                displayName: '',
                sql: 'dim reference ${table1.dim1}',
            };
            expect((0, item_1.getItemId)(tc)).toEqual('calc2');
        });
        it('should return item id for additional metric', async () => {
            const additionalMetric = {
                type: field_1.MetricType.AVERAGE,
                sql: 'example',
                table: 'table1',
                name: 'calc2',
            };
            expect((0, item_1.getItemId)(additionalMetric)).toEqual('table1_calc2');
        });
    });
    describe('isFormat', () => {
        it('should correctly identify valid formats', () => {
            expect((0, field_1.isFormat)('km')).toBe(true);
            expect((0, field_1.isFormat)('mi')).toBe(true);
            expect((0, field_1.isFormat)('usd')).toBe(true);
            expect((0, field_1.isFormat)('gbp')).toBe(true);
            expect((0, field_1.isFormat)('eur')).toBe(true);
            expect((0, field_1.isFormat)('jpy')).toBe(true);
            expect((0, field_1.isFormat)('dkk')).toBe(true);
            expect((0, field_1.isFormat)('id')).toBe(true);
            expect((0, field_1.isFormat)('percent')).toBe(true);
        });
        it('should return false for invalid formats', () => {
            expect((0, field_1.isFormat)('invalid')).toBe(false);
            expect((0, field_1.isFormat)(undefined)).toBe(false);
            expect((0, field_1.isFormat)('')).toBe(false);
        });
    });
});
//# sourceMappingURL=field.test.js.map