"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const dayjs_1 = tslib_1.__importDefault(require("dayjs"));
const moment_1 = tslib_1.__importDefault(require("moment"));
const field_1 = require("../types/field");
const timeFrames_1 = require("../types/timeFrames");
const formatting_1 = require("./formatting");
const formatting_mock_1 = require("./formatting.mock");
describe('Formatting', () => {
    describe('convert legacy Format type', () => {
        [field_1.Format.KM, field_1.Format.MI].forEach((format) => {
            test(`when it is ${format.toUpperCase()} getCustomFormatFromLegacy should return the correct CustomFormat options`, () => {
                expect((0, formatting_1.getCustomFormatFromLegacy)({ format })).toEqual({
                    type: field_1.CustomFormatType.NUMBER,
                    suffix: ` ${format}`,
                    compact: undefined,
                    round: undefined,
                });
            });
        });
        [field_1.Format.EUR, field_1.Format.GBP, field_1.Format.USD].forEach((format) => {
            test(`when it is ${format.toUpperCase()} getCustomFormatFromLegacy should return the correct CustomFormat options`, () => {
                expect((0, formatting_1.getCustomFormatFromLegacy)({ format })).toEqual({
                    type: field_1.CustomFormatType.CURRENCY,
                    currency: format.toUpperCase(),
                    compact: undefined,
                    round: undefined,
                });
            });
        });
        test(`when it is ${field_1.Format.PERCENT.toUpperCase()} getCustomFormatFromLegacy should return the correct CustomFormat options`, () => {
            expect((0, formatting_1.getCustomFormatFromLegacy)({ format: field_1.Format.PERCENT })).toEqual({
                type: field_1.CustomFormatType.PERCENT,
                compact: undefined,
                round: undefined,
            });
        });
        test(`when it is ${field_1.Format.ID.toUpperCase()} getCustomFormatFromLegacy should return the correct CustomFormat options`, () => {
            expect((0, formatting_1.getCustomFormatFromLegacy)({ format: field_1.Format.ID })).toEqual({
                type: field_1.CustomFormatType.ID,
            });
        });
    });
    describe('applying CustomFormat to value', () => {
        describe('when using legacy format', () => {
            test('if Format is legacy distance unit it should return the right format', () => {
                expect((0, formatting_1.applyCustomFormat)(5, (0, formatting_1.getCustomFormatFromLegacy)({ format: field_1.Format.KM }))).toEqual('5 km');
                expect((0, formatting_1.applyCustomFormat)(5, (0, formatting_1.getCustomFormatFromLegacy)({
                    format: field_1.Format.MI,
                }))).toEqual('5 mi');
            });
            test('if Format is currency unit it should return the right format', () => {
                expect((0, formatting_1.applyCustomFormat)(5, {
                    type: field_1.CustomFormatType.CURRENCY,
                    currency: field_1.Format.USD,
                })).toEqual('$5.00');
                expect((0, formatting_1.applyCustomFormat)(5, {
                    type: field_1.CustomFormatType.CURRENCY,
                    currency: field_1.Format.GBP,
                })).toEqual('£5.00');
                expect((0, formatting_1.applyCustomFormat)(5, {
                    type: field_1.CustomFormatType.CURRENCY,
                    currency: field_1.Format.EUR,
                })).toEqual('€5.00');
                expect((0, formatting_1.applyCustomFormat)(5, {
                    type: field_1.CustomFormatType.CURRENCY,
                    currency: field_1.Format.JPY,
                })).toEqual('¥5');
                expect((0, formatting_1.applyCustomFormat)(5, {
                    type: field_1.CustomFormatType.CURRENCY,
                    currency: field_1.Format.DKK,
                })).toEqual('DKK 5.00');
            });
            test('if Format is percent it should return the right format', () => {
                const percentFormat = {
                    type: field_1.CustomFormatType.PERCENT,
                };
                expect((0, formatting_1.applyCustomFormat)(5, percentFormat)).toEqual('500%');
                expect((0, formatting_1.applyCustomFormat)(0.05, percentFormat)).toEqual('5%');
                expect((0, formatting_1.applyCustomFormat)('5', percentFormat)).toEqual('500%');
                expect((0, formatting_1.applyCustomFormat)('foo', percentFormat)).toEqual('foo');
                expect((0, formatting_1.applyCustomFormat)(false, percentFormat)).toEqual('false');
            });
            test('if Format is undefined it should return the right format', () => {
                expect((0, formatting_1.applyCustomFormat)(1103)).toEqual('1,103');
                expect((0, formatting_1.applyCustomFormat)(5)).toEqual('5');
            });
            test('if Format is id it should return the right format', () => {
                // ids are not comma separated
                expect((0, formatting_1.applyCustomFormat)(1019, { type: field_1.CustomFormatType.ID })).toEqual('1019');
            });
        });
        describe('when applying round', () => {
            test('if round is undefined and custom format is not number format it should keep up to 3 decimal places', () => {
                expect((0, formatting_1.applyCustomFormat)(5.9)).toEqual('5.9');
                expect((0, formatting_1.applyCustomFormat)(5.99)).toEqual('5.99');
                expect((0, formatting_1.applyCustomFormat)(5.999)).toEqual('5.999');
                expect((0, formatting_1.applyCustomFormat)(5.9999)).toEqual('6');
                expect((0, formatting_1.applyCustomFormat)(5.99999)).toEqual('6');
                expect((0, formatting_1.applyCustomFormat)(5.9, {
                    type: field_1.CustomFormatType.CURRENCY,
                    currency: field_1.Format.USD,
                })).toEqual('$5.90');
            });
            test('if round is undefined and format is number it should keep up to 3 decimal places', () => {
                expect((0, formatting_1.applyCustomFormat)(5.9, { type: field_1.CustomFormatType.NUMBER })).toEqual('5.9');
            });
            test('when round zero it should return the right round', () => {
                const roundZeroFormat = {
                    type: field_1.CustomFormatType.NUMBER,
                    round: 0,
                };
                expect((0, formatting_1.applyCustomFormat)(1, roundZeroFormat)).toEqual('1');
                expect((0, formatting_1.applyCustomFormat)(10, roundZeroFormat)).toEqual('10');
                expect((0, formatting_1.applyCustomFormat)(100, roundZeroFormat)).toEqual('100');
                expect((0, formatting_1.applyCustomFormat)(5, roundZeroFormat)).toEqual('5');
                expect((0, formatting_1.applyCustomFormat)(5.001, roundZeroFormat)).toEqual('5');
                expect((0, formatting_1.applyCustomFormat)(0.001, roundZeroFormat)).toEqual('0');
                expect((0, formatting_1.applyCustomFormat)(1000, roundZeroFormat)).toEqual('1,000');
                expect((0, formatting_1.applyCustomFormat)(10000, roundZeroFormat)).toEqual('10,000');
                expect((0, formatting_1.applyCustomFormat)(100000, roundZeroFormat)).toEqual('100,000');
                expect((0, formatting_1.applyCustomFormat)(1000000, roundZeroFormat)).toEqual('1,000,000');
                expect((0, formatting_1.applyCustomFormat)(5.9999999, roundZeroFormat)).toEqual('6');
            });
            test('when round is positive number it should return the right round', () => {
                const roundTwoFormat = {
                    type: field_1.CustomFormatType.NUMBER,
                    round: 2,
                };
                expect((0, formatting_1.applyCustomFormat)(5, roundTwoFormat)).toEqual('5.00');
                expect((0, formatting_1.applyCustomFormat)('foo', roundTwoFormat)).toEqual('foo');
                expect((0, formatting_1.applyCustomFormat)(5.001, roundTwoFormat)).toEqual('5.00');
                expect((0, formatting_1.applyCustomFormat)(5.555, roundTwoFormat)).toEqual('5.56');
                expect((0, formatting_1.applyCustomFormat)(5.5555, roundTwoFormat)).toEqual('5.56');
                expect((0, formatting_1.applyCustomFormat)(5.9999999, roundTwoFormat)).toEqual('6.00');
                expect((0, formatting_1.applyCustomFormat)(false, roundTwoFormat)).toEqual('false');
                const roundTenFormat = {
                    type: field_1.CustomFormatType.NUMBER,
                    round: 10,
                };
                expect((0, formatting_1.applyCustomFormat)(5, roundTenFormat)).toEqual('5.0000000000');
                expect((0, formatting_1.applyCustomFormat)(5.001, roundTenFormat)).toEqual('5.0010000000');
                expect((0, formatting_1.applyCustomFormat)(5.9999999, roundTenFormat)).toEqual('5.9999999000');
            });
            test('when round is negative number it should return the right round', () => {
                const number = 123456789.12345;
                expect((0, formatting_1.formatNumberValue)(number, {
                    type: field_1.CustomFormatType.DEFAULT,
                    round: -1,
                })).toEqual('123,456,790');
                expect((0, formatting_1.formatNumberValue)(number, {
                    type: field_1.CustomFormatType.DEFAULT,
                    round: -2,
                })).toEqual('123,456,800');
                expect((0, formatting_1.formatNumberValue)(number, {
                    type: field_1.CustomFormatType.DEFAULT,
                    round: -3,
                })).toEqual('123,457,000');
                expect((0, formatting_1.formatNumberValue)(number, {
                    type: field_1.CustomFormatType.DEFAULT,
                    round: -99,
                })).toEqual('100,000,000');
            });
        });
        test('applyCustomFormat should return the right format and round', () => {
            expect((0, formatting_1.applyCustomFormat)(5, {
                type: field_1.CustomFormatType.NUMBER,
                suffix: ` ${field_1.Format.KM}`,
                round: 2,
            })).toEqual('5.00 km');
            expect((0, formatting_1.applyCustomFormat)(5, {
                type: field_1.CustomFormatType.NUMBER,
                suffix: ` ${field_1.Format.KM}`,
                round: -2,
            })).toEqual('5 km');
            expect((0, formatting_1.applyCustomFormat)(5, {
                type: field_1.CustomFormatType.NUMBER,
                suffix: ` ${field_1.Format.MI}`,
                round: 4,
            })).toEqual('5.0000 mi');
            expect((0, formatting_1.applyCustomFormat)(5, {
                type: field_1.CustomFormatType.NUMBER,
                suffix: ` ${field_1.Format.MI}`,
                round: -4,
            })).toEqual('5 mi');
            expect((0, formatting_1.applyCustomFormat)(5, {
                type: field_1.CustomFormatType.CURRENCY,
                currency: field_1.Format.USD,
                round: 2,
            })).toEqual('$5.00');
            expect((0, formatting_1.applyCustomFormat)(5.0, {
                type: field_1.CustomFormatType.CURRENCY,
                currency: field_1.Format.USD,
                round: 0,
            })).toEqual('$5');
            expect((0, formatting_1.applyCustomFormat)(5, {
                type: field_1.CustomFormatType.CURRENCY,
                currency: field_1.Format.USD,
                round: -2,
            })).toEqual('$5');
            expect((0, formatting_1.applyCustomFormat)(5.25, {
                type: field_1.CustomFormatType.CURRENCY,
                currency: field_1.Format.USD,
                round: -1,
            })).toEqual('$5');
            expect((0, formatting_1.applyCustomFormat)('5.0000', {
                type: field_1.CustomFormatType.CURRENCY,
                currency: field_1.Format.USD,
                round: 2,
            })).toEqual('$5.00');
            expect((0, formatting_1.applyCustomFormat)(5, {
                type: field_1.CustomFormatType.CURRENCY,
                currency: field_1.Format.GBP,
                round: 2,
            })).toEqual('£5.00');
            expect((0, formatting_1.applyCustomFormat)(5, {
                type: field_1.CustomFormatType.CURRENCY,
                currency: field_1.Format.GBP,
                round: -2,
            })).toEqual('£5');
            expect((0, formatting_1.applyCustomFormat)(5.25, {
                type: field_1.CustomFormatType.CURRENCY,
                currency: field_1.Format.GBP,
                round: -2,
            })).toEqual('£5');
            expect((0, formatting_1.applyCustomFormat)(5, {
                type: field_1.CustomFormatType.CURRENCY,
                currency: field_1.Format.EUR,
                round: 2,
            })).toEqual('€5.00');
            expect((0, formatting_1.applyCustomFormat)(5, {
                type: field_1.CustomFormatType.CURRENCY,
                currency: field_1.Format.EUR,
                round: -2,
            })).toEqual('€5');
            expect((0, formatting_1.applyCustomFormat)(5.25, {
                type: field_1.CustomFormatType.CURRENCY,
                currency: field_1.Format.EUR,
                round: -1,
            })).toEqual('€5');
            expect((0, formatting_1.applyCustomFormat)(5, {
                type: field_1.CustomFormatType.PERCENT,
                round: 2,
            })).toEqual('500.00%');
            expect((0, formatting_1.applyCustomFormat)(0.05, {
                type: field_1.CustomFormatType.PERCENT,
                round: 2,
            })).toEqual('5.00%');
            expect((0, formatting_1.applyCustomFormat)('5', {
                type: field_1.CustomFormatType.PERCENT,
                round: 2,
            })).toEqual('500.00%');
            expect((0, formatting_1.applyCustomFormat)(0.0511, {
                type: field_1.CustomFormatType.PERCENT,
                round: 2,
            })).toEqual('5.11%');
            expect((0, formatting_1.applyCustomFormat)(0.0511, {
                type: field_1.CustomFormatType.PERCENT,
                round: 4,
            })).toEqual('5.1100%');
            expect((0, formatting_1.applyCustomFormat)('foo', {
                type: field_1.CustomFormatType.PERCENT,
                round: 2,
            })).toEqual('foo');
            expect((0, formatting_1.applyCustomFormat)(false, {
                type: field_1.CustomFormatType.PERCENT,
                round: 2,
            })).toEqual('false');
            expect((0, formatting_1.applyCustomFormat)(0.05, {
                type: field_1.CustomFormatType.PERCENT,
                round: -2,
            })).toEqual('5%');
            expect((0, formatting_1.applyCustomFormat)('5', {
                type: field_1.CustomFormatType.PERCENT,
                round: -2,
            })).toEqual('500%');
        });
        describe('when applying compact', () => {
            const K = field_1.Compact.THOUSANDS;
            const M = field_1.Compact.MILLIONS;
            const B = field_1.Compact.BILLIONS;
            const T = field_1.Compact.TRILLIONS;
            const thousandsConfig = {
                type: field_1.CustomFormatType.NUMBER,
                compact: K,
            };
            const millionsConfig = {
                type: field_1.CustomFormatType.NUMBER,
                compact: M,
            };
            const billionsConfig = {
                type: field_1.CustomFormatType.NUMBER,
                compact: B,
            };
            const trillionsConfig = {
                type: field_1.CustomFormatType.NUMBER,
                compact: T,
            };
            test('it should return the right style', () => {
                expect((0, formatting_1.applyCustomFormat)(5, thousandsConfig)).toEqual('0.005K');
                expect((0, formatting_1.applyCustomFormat)(5, millionsConfig)).toEqual('0M');
                expect((0, formatting_1.applyCustomFormat)(500000, billionsConfig)).toEqual('0.001B');
                expect((0, formatting_1.applyCustomFormat)(5, billionsConfig)).toEqual('0B');
                expect((0, formatting_1.applyCustomFormat)(5000000000, trillionsConfig)).toEqual('0.005T');
            });
            test('when applying round it should return the right style', () => {
                expect((0, formatting_1.applyCustomFormat)(5, { ...millionsConfig, round: 2 })).toEqual('0.00M');
                expect((0, formatting_1.applyCustomFormat)(5400000, { ...millionsConfig, round: 0 })).toEqual('5M');
                expect((0, formatting_1.applyCustomFormat)(4956789123, {
                    ...trillionsConfig,
                    round: -1,
                })).toEqual('0.005T');
            });
            test('with legacy distance format it should return the right format', () => {
                expect((0, formatting_1.applyCustomFormat)(5000, (0, formatting_1.getCustomFormatFromLegacy)({
                    compact: K,
                    round: 2,
                    format: field_1.Format.KM,
                }))).toEqual('5.00K km');
                expect((0, formatting_1.applyCustomFormat)(50000, (0, formatting_1.getCustomFormatFromLegacy)({
                    compact: K,
                    round: 4,
                    format: field_1.Format.MI,
                }))).toEqual('50.0000K mi');
            });
            test('with legacy currency format it should return the right format', () => {
                expect((0, formatting_1.applyCustomFormat)(5000, (0, formatting_1.getCustomFormatFromLegacy)({
                    compact: K,
                    round: 2,
                    format: field_1.Format.USD,
                }))).toEqual('$5.00K');
                expect((0, formatting_1.applyCustomFormat)(5000000, (0, formatting_1.getCustomFormatFromLegacy)({
                    compact: K,
                    round: 2,
                    format: field_1.Format.USD,
                }))).toEqual('$5,000.00K');
                expect((0, formatting_1.applyCustomFormat)(5000000, (0, formatting_1.getCustomFormatFromLegacy)({
                    compact: M,
                    round: 2,
                    format: field_1.Format.USD,
                }))).toEqual('$5.00M');
                expect((0, formatting_1.applyCustomFormat)(4, (0, formatting_1.getCustomFormatFromLegacy)({
                    compact: K,
                    round: 2,
                    format: field_1.Format.USD,
                }))).toEqual('$0.00K');
                expect((0, formatting_1.applyCustomFormat)(4, (0, formatting_1.getCustomFormatFromLegacy)({
                    compact: K,
                    round: 3,
                    format: field_1.Format.USD,
                }))).toEqual('$0.004K');
                expect((0, formatting_1.applyCustomFormat)(4000, (0, formatting_1.getCustomFormatFromLegacy)({
                    compact: K,
                    format: field_1.Format.USD,
                }))).toEqual('$4.00K');
                expect((0, formatting_1.applyCustomFormat)(5000000, (0, formatting_1.getCustomFormatFromLegacy)({
                    compact: M,
                    round: 2,
                    format: field_1.Format.USD,
                }))).toEqual('$5.00M');
                expect((0, formatting_1.applyCustomFormat)(5000000000, (0, formatting_1.getCustomFormatFromLegacy)({
                    compact: M,
                    round: 2,
                    format: field_1.Format.USD,
                }))).toEqual('$5,000.00M');
                expect((0, formatting_1.applyCustomFormat)(5000000000, (0, formatting_1.getCustomFormatFromLegacy)({
                    compact: B,
                    round: 2,
                    format: field_1.Format.USD,
                }))).toEqual('$5.00B');
                expect((0, formatting_1.applyCustomFormat)(5000.0, (0, formatting_1.getCustomFormatFromLegacy)({
                    compact: K,
                    round: 0,
                    format: field_1.Format.USD,
                }))).toEqual('$5K');
                expect((0, formatting_1.applyCustomFormat)('5000', (0, formatting_1.getCustomFormatFromLegacy)({
                    compact: K,
                    round: 2,
                    format: field_1.Format.USD,
                }))).toEqual('$5.00K');
                expect((0, formatting_1.applyCustomFormat)(5000, (0, formatting_1.getCustomFormatFromLegacy)({
                    compact: K,
                    round: 2,
                    format: field_1.Format.GBP,
                }))).toEqual('£5.00K');
                expect((0, formatting_1.applyCustomFormat)(5000, (0, formatting_1.getCustomFormatFromLegacy)({
                    compact: K,
                    round: 2,
                    format: field_1.Format.EUR,
                }))).toEqual('€5.00K');
            });
            test('with legacy percent format it should return the right format', () => {
                expect((0, formatting_1.applyCustomFormat)(0.05, (0, formatting_1.getCustomFormatFromLegacy)({
                    compact: K,
                    round: 2,
                    format: field_1.Format.PERCENT,
                }))).toEqual('5.00%');
            });
            test('suports compact alias', () => {
                expect((0, formatting_1.applyCustomFormat)(1000, {
                    type: field_1.CustomFormatType.NUMBER,
                    compact: 'K',
                })).toEqual('1K');
                expect((0, formatting_1.applyCustomFormat)(1000, {
                    type: field_1.CustomFormatType.NUMBER,
                    compact: 'thousand',
                })).toEqual('1K');
                expect((0, formatting_1.applyCustomFormat)(1000000, {
                    type: field_1.CustomFormatType.NUMBER,
                    compact: 'M',
                })).toEqual('1M');
                expect((0, formatting_1.applyCustomFormat)(1000000, {
                    type: field_1.CustomFormatType.NUMBER,
                    compact: 'million',
                })).toEqual('1M');
                expect((0, formatting_1.applyCustomFormat)(1000000000, {
                    type: field_1.CustomFormatType.NUMBER,
                    compact: 'B',
                })).toEqual('1B');
                expect((0, formatting_1.applyCustomFormat)(1000000000, {
                    type: field_1.CustomFormatType.NUMBER,
                    compact: 'billion',
                })).toEqual('1B');
                expect((0, formatting_1.applyCustomFormat)(1000000000000, {
                    type: field_1.CustomFormatType.NUMBER,
                    compact: 'T',
                })).toEqual('1T');
                expect((0, formatting_1.applyCustomFormat)(1000000000000, {
                    type: field_1.CustomFormatType.NUMBER,
                    compact: 'trillion',
                })).toEqual('1T');
            });
        });
    });
    describe('formatItemValue', () => {
        test('formatItemValue should return the right format when field is undefined', () => {
            expect((0, formatting_1.formatItemValue)(undefined, undefined)).toEqual('-');
            expect((0, formatting_1.formatItemValue)(undefined, null)).toEqual('∅');
            expect((0, formatting_1.formatItemValue)(undefined, '5')).toEqual('5');
            expect((0, formatting_1.formatItemValue)(undefined, 5)).toEqual('5');
        });
        test('formatItemValue should return the right format when field is Dimension', () => {
            expect((0, formatting_1.formatItemValue)(formatting_mock_1.dimension, undefined)).toEqual('-');
            expect((0, formatting_1.formatItemValue)(formatting_mock_1.dimension, null)).toEqual('∅');
            expect((0, formatting_1.formatItemValue)({ ...formatting_mock_1.dimension, type: field_1.DimensionType.STRING }, '5')).toEqual('5');
            expect((0, formatting_1.formatItemValue)({ ...formatting_mock_1.dimension, type: field_1.DimensionType.NUMBER }, 5)).toEqual('5');
            expect((0, formatting_1.formatItemValue)({ ...formatting_mock_1.dimension, type: field_1.DimensionType.STRING }, 132323123)).toEqual('132323123');
            expect((0, formatting_1.formatItemValue)({ ...formatting_mock_1.dimension, type: field_1.DimensionType.BOOLEAN }, true)).toEqual('True');
            expect((0, formatting_1.formatItemValue)({
                ...formatting_mock_1.dimension,
                type: field_1.DimensionType.DATE,
            }, new Date('2021-03-10T00:00:00.000Z'))).toEqual('2021-03-10');
            expect((0, formatting_1.formatItemValue)({
                ...formatting_mock_1.dimension,
                type: field_1.DimensionType.TIMESTAMP,
            }, new Date('2021-03-10T00:00:00.000Z'))).toEqual('2021-03-10, 00:00:00:000 (+00:00)');
            expect((0, formatting_1.formatItemValue)({
                ...formatting_mock_1.dimension,
                timeInterval: timeFrames_1.TimeFrames.YEAR_NUM,
                type: field_1.DimensionType.NUMBER,
            }, 2021)).toEqual('2021');
        });
        test('formatItemValue DATE ignores display timezone for DATE-backed dimensions', () => {
            const value = new Date('2026-03-03T00:00:00.000Z');
            // DATE base: timezone must be ignored (no day-shift in negative offsets)
            expect((0, formatting_1.formatItemValue)({
                ...formatting_mock_1.dimension,
                type: field_1.DimensionType.DATE,
                timeInterval: timeFrames_1.TimeFrames.DAY,
                timeIntervalBaseDimensionType: field_1.DimensionType.DATE,
            }, value, false, undefined, 'Pacific/Pago_Pago')).toEqual('2026-03-03');
            // TIMESTAMP base: timezone still applies
            expect((0, formatting_1.formatItemValue)({
                ...formatting_mock_1.dimension,
                type: field_1.DimensionType.DATE,
                timeInterval: timeFrames_1.TimeFrames.DAY,
                timeIntervalBaseDimensionType: field_1.DimensionType.TIMESTAMP,
            }, value, false, undefined, 'Pacific/Pago_Pago')).toEqual('2026-03-02');
        });
        test('formatItemValue ignores display timezone when skipTimezoneConversion is set', () => {
            const value = new Date('2026-03-03T01:30:00.000Z');
            // TIMESTAMP dim with skipTimezoneConversion renders in raw UTC
            expect((0, formatting_1.formatItemValue)({
                ...formatting_mock_1.dimension,
                type: field_1.DimensionType.TIMESTAMP,
                skipTimezoneConversion: true,
            }, value, false, undefined, 'Pacific/Pago_Pago')).toEqual('2026-03-03, 01:30:00:000 (+00:00)');
            // DATE-base-TIMESTAMP child also opts out
            expect((0, formatting_1.formatItemValue)({
                ...formatting_mock_1.dimension,
                type: field_1.DimensionType.DATE,
                timeInterval: timeFrames_1.TimeFrames.DAY,
                timeIntervalBaseDimensionType: field_1.DimensionType.TIMESTAMP,
                skipTimezoneConversion: true,
            }, value, false, undefined, 'Pacific/Pago_Pago')).toEqual('2026-03-03');
            // Sanity: same dim without the marker (default) still shifts
            expect((0, formatting_1.formatItemValue)({
                ...formatting_mock_1.dimension,
                type: field_1.DimensionType.TIMESTAMP,
            }, value, false, undefined, 'Pacific/Pago_Pago')).toEqual('2026-03-02, 14:30:00:000 (-11:00)');
        });
        test('shouldShiftItemTimezone respects skipTimezoneConversion', () => {
            // The function is part of the public formatting surface used by
            // pivot exports + Google Sheets — opt-out must short-circuit.
            const tsBase = {
                ...formatting_mock_1.dimension,
                type: field_1.DimensionType.TIMESTAMP,
            };
            const tsBaseOptOut = {
                ...tsBase,
                skipTimezoneConversion: true,
            };
            expect((0, formatting_1.shouldShiftItemTimezone)(tsBase)).toBe(true);
            expect((0, formatting_1.shouldShiftItemTimezone)(tsBaseOptOut)).toBe(false);
            const dateOverTs = {
                ...formatting_mock_1.dimension,
                type: field_1.DimensionType.DATE,
                timeIntervalBaseDimensionType: field_1.DimensionType.TIMESTAMP,
            };
            const dateOverTsOptOut = {
                ...dateOverTs,
                skipTimezoneConversion: true,
            };
            expect((0, formatting_1.shouldShiftItemTimezone)(dateOverTs)).toBe(true);
            expect((0, formatting_1.shouldShiftItemTimezone)(dateOverTsOptOut)).toBe(false);
        });
        test('formatItemValue should return the right format when field is Metric', () => {
            expect((0, formatting_1.formatItemValue)(formatting_mock_1.metric, undefined)).toEqual('-');
            expect((0, formatting_1.formatItemValue)(formatting_mock_1.metric, null)).toEqual('∅');
            expect((0, formatting_1.formatItemValue)({ ...formatting_mock_1.metric, type: field_1.MetricType.AVERAGE }, 5)).toEqual('5');
            expect((0, formatting_1.formatItemValue)({ ...formatting_mock_1.metric, type: field_1.MetricType.COUNT }, 5)).toEqual('5');
            expect((0, formatting_1.formatItemValue)({ ...formatting_mock_1.metric, type: field_1.MetricType.COUNT_DISTINCT }, 5)).toEqual('5');
            expect((0, formatting_1.formatItemValue)({ ...formatting_mock_1.metric, type: field_1.MetricType.SUM }, 5)).toEqual('5');
            expect((0, formatting_1.formatItemValue)({ ...formatting_mock_1.metric, type: field_1.MetricType.MIN }, 5000)).toEqual('5,000');
            expect((0, formatting_1.formatItemValue)({ ...formatting_mock_1.metric, type: field_1.MetricType.MAX }, 5000)).toEqual('5,000');
            expect((0, formatting_1.formatItemValue)({ ...formatting_mock_1.metric, type: field_1.MetricType.MIN }, new Date('2021-03-10T00:00:00.000Z'))).toEqual('2021-03-10, 00:00:00:000 (+00:00)');
            expect((0, formatting_1.formatItemValue)({ ...formatting_mock_1.metric, type: field_1.MetricType.MAX }, new Date('2021-03-10T00:00:00.000Z'))).toEqual('2021-03-10, 00:00:00:000 (+00:00)');
            expect((0, formatting_1.formatItemValue)({
                ...formatting_mock_1.metric,
                type: field_1.MetricType.NUMBER,
                round: 2,
            }, '1.123456123123')).toEqual('1.12');
            expect((0, formatting_1.formatItemValue)({
                ...formatting_mock_1.metric,
                type: field_1.MetricType.NUMBER,
                compact: field_1.Compact.THOUSANDS,
            }, 1000)).toEqual('1K');
        });
        describe('formatItemValue timestamp handling', () => {
            const mockTimestampField = {
                type: field_1.DimensionType.TIMESTAMP,
                name: 'test_timestamp',
                table: 'test',
                fieldType: field_1.FieldType.DIMENSION,
                label: 'Test Timestamp',
                sql: 'test.timestamp',
                tableLabel: 'Test',
                hidden: false,
            };
            test('should handle valid timestamp formats', () => {
                const validTimestamps = [
                    '2020-08-11T16:44:00Z',
                    '2020-08-11 16:44:00',
                    '2020-08-11T16:44:00.123Z',
                    new Date('2020-08-11T16:44:00Z'),
                    (0, moment_1.default)('2020-08-11T16:44:00Z'),
                ];
                validTimestamps.forEach((timestamp) => {
                    const result = (0, formatting_1.formatItemValue)(mockTimestampField, timestamp);
                    expect(result).not.toBe('NaT');
                    expect(result).not.toContain('Invalid');
                });
            });
            test('should return NaT for invalid formats', () => {
                const invalidTimestamps = [
                    'invalid-date-string',
                    '2020-13-45',
                    '2020-08-11T25:00:00',
                    '',
                    'not-a-date',
                    '2025-07-08 23:59:58.656000 Asia/Bangkok',
                ];
                invalidTimestamps.forEach((timestamp) => {
                    const result = (0, formatting_1.formatItemValue)(mockTimestampField, timestamp);
                    expect(result).toBe('NaT');
                });
            });
        });
    });
    describe('additional metric formatting', () => {
        test('format additional metric with custom format DATE', () => {
            expect((0, formatting_1.formatItemValue)({
                ...formatting_mock_1.additionalMetric,
                type: field_1.MetricType.MIN,
                formatOptions: { type: field_1.CustomFormatType.DATE },
            }, new Date('2021-03-10T00:00:00.000Z'))).toEqual('2021-03-10');
            expect((0, formatting_1.formatItemValue)({
                ...formatting_mock_1.additionalMetric,
                type: field_1.MetricType.MAX,
                formatOptions: {
                    type: field_1.CustomFormatType.DATE,
                    timeInterval: timeFrames_1.TimeFrames.DAY,
                },
            }, new Date('2021-03-10T00:00:00.000Z'))).toEqual('2021-03-10');
            expect((0, formatting_1.formatItemValue)({
                ...formatting_mock_1.additionalMetric,
                type: field_1.MetricType.MIN,
                formatOptions: {
                    type: field_1.CustomFormatType.DATE,
                    timeInterval: timeFrames_1.TimeFrames.YEAR,
                },
            }, new Date('2021-03-10T00:00:00.000Z'))).toEqual('2021');
            expect((0, formatting_1.formatItemValue)({
                ...formatting_mock_1.additionalMetric,
                type: field_1.MetricType.MAX,
                formatOptions: {
                    type: field_1.CustomFormatType.DATE,
                    timeInterval: timeFrames_1.TimeFrames.MONTH,
                },
            }, new Date('2021-03-10T00:00:00.000Z'))).toEqual('2021-03');
        });
        test('format additional metric with custom format TIMESTAMP', () => {
            expect((0, formatting_1.formatItemValue)({
                ...formatting_mock_1.additionalMetric,
                type: field_1.MetricType.MIN,
                formatOptions: { type: field_1.CustomFormatType.TIMESTAMP },
            }, new Date('2021-03-10T00:00:00.000Z'))).toEqual('2021-03-10, 00:00:00:000 (+00:00)');
            expect((0, formatting_1.formatItemValue)({
                ...formatting_mock_1.additionalMetric,
                type: field_1.MetricType.MAX,
                formatOptions: {
                    type: field_1.CustomFormatType.TIMESTAMP,
                    timeInterval: timeFrames_1.TimeFrames.HOUR,
                },
            }, new Date('2021-03-10T00:00:00.000Z'))).toEqual('2021-03-10, 00 (+00:00)');
            expect((0, formatting_1.formatItemValue)({
                ...formatting_mock_1.additionalMetric,
                type: field_1.MetricType.MIN,
                formatOptions: {
                    type: field_1.CustomFormatType.TIMESTAMP,
                    timeInterval: timeFrames_1.TimeFrames.MINUTE,
                },
            }, new Date('2021-03-10T00:00:00.000Z'))).toEqual('2021-03-10, 00:00 (+00:00)');
            expect((0, formatting_1.formatItemValue)({
                ...formatting_mock_1.additionalMetric,
                type: field_1.MetricType.MAX,
                formatOptions: {
                    type: field_1.CustomFormatType.TIMESTAMP,
                    timeInterval: timeFrames_1.TimeFrames.SECOND,
                },
            }, new Date('2021-03-10T00:00:00.000Z'))).toEqual('2021-03-10, 00:00:00 (+00:00)');
        });
    });
    describe('format item value', () => {
        test('formatItemValue should return the right format when field is undefined', () => {
            expect((0, formatting_1.formatItemValue)(undefined, undefined)).toEqual('-');
            expect((0, formatting_1.formatItemValue)(undefined, null)).toEqual('∅');
            expect((0, formatting_1.formatItemValue)(undefined, '5')).toEqual('5');
            expect((0, formatting_1.formatItemValue)(undefined, 5)).toEqual('5');
        });
        test('formatItemValue should return the right format when field is table calculation', () => {
            expect((0, formatting_1.formatItemValue)(formatting_mock_1.tableCalculation, undefined)).toEqual('-');
            expect((0, formatting_1.formatItemValue)(formatting_mock_1.tableCalculation, null)).toEqual('∅');
            expect((0, formatting_1.formatItemValue)(formatting_mock_1.tableCalculation, '5')).toEqual('5');
            expect((0, formatting_1.formatItemValue)(formatting_mock_1.tableCalculation, 5)).toEqual('5');
        });
    });
    describe('format table calculation', () => {
        test('table calculation with default format', () => {
            const defaultFormat = {
                ...formatting_mock_1.tableCalculation,
                format: { type: field_1.CustomFormatType.DEFAULT },
            };
            expect((0, formatting_1.formatItemValue)(defaultFormat, undefined)).toEqual('-');
            expect((0, formatting_1.formatItemValue)(defaultFormat, null)).toEqual('∅');
            expect((0, formatting_1.formatItemValue)(defaultFormat, '5')).toEqual('5');
            expect((0, formatting_1.formatItemValue)(defaultFormat, 5)).toEqual('5');
        });
        test('table calculation with default format and extra arguments', () => {
            // This round or separator should not affect default format
            const withExtraFormat = {
                ...formatting_mock_1.tableCalculation,
                format: {
                    type: field_1.CustomFormatType.DEFAULT,
                    round: 2,
                    separator: field_1.NumberSeparator.COMMA_PERIOD,
                },
            };
            expect((0, formatting_1.formatItemValue)(withExtraFormat, undefined)).toEqual('-');
            expect((0, formatting_1.formatItemValue)(withExtraFormat, null)).toEqual('∅');
            expect((0, formatting_1.formatItemValue)(withExtraFormat, '5')).toEqual('5');
            expect((0, formatting_1.formatItemValue)(withExtraFormat, 5)).toEqual('5');
        });
        test('table calculation with percent format', () => {
            const percentFormat = {
                ...formatting_mock_1.tableCalculation,
                format: { type: field_1.CustomFormatType.PERCENT },
            };
            expect((0, formatting_1.formatItemValue)(percentFormat, undefined)).toEqual('-');
            expect((0, formatting_1.formatItemValue)(percentFormat, null)).toEqual('∅');
            expect((0, formatting_1.formatItemValue)(percentFormat, '0.05')).toEqual('5%');
            expect((0, formatting_1.formatItemValue)(percentFormat, 0.05)).toEqual('5%');
            expect((0, formatting_1.formatItemValue)(percentFormat, 1)).toEqual('100%');
            expect((0, formatting_1.formatItemValue)(percentFormat, 5)).toEqual('500%');
            expect((0, formatting_1.formatItemValue)(percentFormat, '0.05123')).toEqual('5.123%');
        });
        test('table calculation with percent format and round', () => {
            const percentFormat = {
                ...formatting_mock_1.tableCalculation,
                format: { type: field_1.CustomFormatType.PERCENT, round: 1 },
            };
            expect((0, formatting_1.formatItemValue)(percentFormat, undefined)).toEqual('-');
            expect((0, formatting_1.formatItemValue)(percentFormat, null)).toEqual('∅');
            expect((0, formatting_1.formatItemValue)(percentFormat, '0.05')).toEqual('5.0%');
            expect((0, formatting_1.formatItemValue)(percentFormat, 0.05)).toEqual('5.0%');
            expect((0, formatting_1.formatItemValue)(percentFormat, 1)).toEqual('100.0%');
            expect((0, formatting_1.formatItemValue)(percentFormat, 5)).toEqual('500.0%');
            expect((0, formatting_1.formatItemValue)(percentFormat, '0.05123')).toEqual('5.1%');
        });
        test('table calculation with percent format and number separator', () => {
            const percentFormat = {
                ...formatting_mock_1.tableCalculation,
                format: {
                    type: field_1.CustomFormatType.PERCENT,
                    round: 2,
                    separator: field_1.NumberSeparator.PERIOD_COMMA,
                },
            };
            expect((0, formatting_1.formatItemValue)(percentFormat, undefined)).toEqual('-');
            expect((0, formatting_1.formatItemValue)(percentFormat, null)).toEqual('∅');
            expect((0, formatting_1.formatItemValue)(percentFormat, '0.05')).toEqual('5,00%');
            expect((0, formatting_1.formatItemValue)(percentFormat, '0.05123')).toEqual('5,12%');
        });
        test('format number separator', () => {
            const number = 123456789.12345;
            expect((0, formatting_1.formatNumberValue)(number, {
                type: field_1.CustomFormatType.DEFAULT,
                round: 0,
                separator: field_1.NumberSeparator.COMMA_PERIOD,
            })).toEqual('123,456,789');
            expect((0, formatting_1.formatNumberValue)(number, {
                type: field_1.CustomFormatType.DEFAULT,
                round: 0,
                separator: field_1.NumberSeparator.PERIOD_COMMA,
            })).toEqual('123.456.789');
            expect((0, formatting_1.formatNumberValue)(number, {
                type: field_1.CustomFormatType.DEFAULT,
                round: 2,
                separator: field_1.NumberSeparator.SPACE_PERIOD,
            })).toEqual('123 456 789.12');
            expect((0, formatting_1.formatNumberValue)(number, {
                type: field_1.CustomFormatType.DEFAULT,
                round: 2,
                separator: field_1.NumberSeparator.NO_SEPARATOR_PERIOD,
            })).toEqual('123456789.12');
        });
        test('available currencies', () => {
            const symbols = formatting_1.currencies.map((currency) => {
                const format = Intl.NumberFormat(undefined, {
                    style: 'currency',
                    currency,
                });
                return format.format(1).replace(' ', ' ');
            });
            expect(symbols).toEqual([
                '$1.00',
                '€1.00',
                '£1.00',
                '¥1',
                'DKK 1.00',
                'CHF 1.00',
                'CA$1.00',
                'A$1.00',
                'CN¥1.00',
                'ARS 1.00',
                'R$1.00',
                'CLP 1',
                'COP 1.00',
                'CZK 1.00',
                'HK$1.00',
                'HUF 1.00',
                '₹1.00',
                '₪1.00',
                '₩1',
                'MYR 1.00',
                'MX$1.00',
                'MAD 1.00',
                'NZ$1.00',
                'NOK 1.00',
                '₱1.00',
                'PLN 1.00',
                'RUB 1.00',
                'SAR 1.00',
                'SGD 1.00',
                'ZAR 1.00',
                'SEK 1.00',
                'NT$1.00',
                'THB 1.00',
                'TRY 1.00',
                '₫1',
            ]);
        });
        test('convert currencies with default settings', () => {
            expect(formatting_1.currencies.slice(0, 4).map((currency) => (0, formatting_1.applyCustomFormat)(12345.1235, {
                type: field_1.CustomFormatType.CURRENCY,
                currency,
            }))).toEqual(['$12,345.12', '€12,345.12', '£12,345.12', '¥12,345']);
            // Number as string
            expect(formatting_1.currencies.slice(0, 4).map((currency) => (0, formatting_1.applyCustomFormat)('12345.1235', {
                type: field_1.CustomFormatType.CURRENCY,
                currency,
            }))).toEqual(['$12,345.12', '€12,345.12', '£12,345.12', '¥12,345']);
        });
        test('convert currencies with round', () => {
            expect(formatting_1.currencies.slice(0, 4).map((currency) => (0, formatting_1.applyCustomFormat)(12345.1235, {
                type: field_1.CustomFormatType.CURRENCY,
                currency,
                round: 3,
            }))).toEqual([
                '$12,345.124',
                '€12,345.124',
                '£12,345.124',
                '¥12,345.124',
            ]);
        });
        test('convert currencies with separator ', () => {
            // Using PERIOD_COMMA changes the position of the currency symbol
            expect(formatting_1.currencies.slice(0, 4).map((currency) => (0, formatting_1.applyCustomFormat)(12345.1235, {
                type: field_1.CustomFormatType.CURRENCY,
                currency,
                separator: field_1.NumberSeparator.PERIOD_COMMA,
            }))).toEqual([
                '12.345,12 $',
                '12.345,12 €',
                '12.345,12 £',
                '12.345 ¥',
            ]);
        });
        test('convert currencies with compact ', () => {
            expect(formatting_1.currencies.slice(0, 4).map((currency) => (0, formatting_1.applyCustomFormat)(12345.1235, {
                type: field_1.CustomFormatType.CURRENCY,
                currency,
                compact: field_1.Compact.THOUSANDS,
            }))).toEqual(['$12.35K', '€12.35K', '£12.35K', '¥12K']);
            expect(formatting_1.currencies.slice(0, 4).map((currency) => (0, formatting_1.applyCustomFormat)(123456789.1235, {
                type: field_1.CustomFormatType.CURRENCY,
                currency,
                compact: field_1.Compact.MILLIONS,
                round: 0,
            }))).toEqual(['$123M', '€123M', '£123M', '¥123M']);
        });
        test('convert numbers ', () => {
            expect((0, formatting_1.applyCustomFormat)(12345.56789, {
                type: field_1.CustomFormatType.NUMBER,
            })).toEqual('12,345.568');
            expect((0, formatting_1.applyCustomFormat)(12345.1235, {
                type: field_1.CustomFormatType.NUMBER,
                round: 2,
                prefix: 'foo ',
                suffix: ' bar',
                compact: field_1.Compact.THOUSANDS,
            })).toEqual('foo 12.35K bar');
            // Number as string
            expect((0, formatting_1.applyCustomFormat)('12345.1235', {
                type: field_1.CustomFormatType.NUMBER,
                prefix: 'foo ',
                suffix: ' bar',
            })).toEqual('foo 12,345.124 bar');
        });
        test('convert table calculation formats with invalid numbers', () => {
            // This method should return the original value if the value is not a number
            const values = [
                'this is a string',
                '',
                undefined,
                null,
                true,
                false,
            ];
            const expectedValue = [
                'this is a string',
                '',
                '-',
                '∅',
                'true',
                'false',
            ];
            values.map((value, i) => expect((0, formatting_1.applyCustomFormat)(value, {
                type: field_1.CustomFormatType.DEFAULT,
            })).toEqual(expectedValue[i]));
            values.map((value, i) => expect((0, formatting_1.applyCustomFormat)(value, {
                type: field_1.CustomFormatType.CURRENCY,
                currency: field_1.Format.USD,
            })).toEqual(expectedValue[i]));
            values.map((value, i) => expect((0, formatting_1.applyCustomFormat)(value, {
                type: field_1.CustomFormatType.CURRENCY,
                currency: field_1.Format.USD,
                round: 2,
                compact: field_1.Compact.THOUSANDS,
                separator: field_1.NumberSeparator.PERIOD_COMMA,
            })).toEqual(expectedValue[i]));
            values.map((value, i) => expect((0, formatting_1.applyCustomFormat)(value, {
                type: field_1.CustomFormatType.NUMBER,
            })).toEqual(expectedValue[i]));
            values.map((value, i) => expect((0, formatting_1.applyCustomFormat)(value, {
                type: field_1.CustomFormatType.NUMBER,
                prefix: 'foo',
                suffix: 'bar',
                round: 2,
                compact: field_1.Compact.THOUSANDS,
                separator: field_1.NumberSeparator.PERIOD_COMMA,
            })).toEqual(expectedValue[i]));
            values.map((value, i) => expect((0, formatting_1.applyCustomFormat)(value, {
                type: field_1.CustomFormatType.PERCENT,
            })).toEqual(expectedValue[i]));
            values.map((value, i) => expect((0, formatting_1.applyCustomFormat)(value, {
                type: field_1.CustomFormatType.PERCENT,
                round: 2,
                separator: field_1.NumberSeparator.PERIOD_COMMA,
            })).toEqual(expectedValue[i]));
        });
        test('formatValueWithExpression handles binary byte units correctly', () => {
            // Test KIBIBYTES (should divide by 1024)
            const kibibyteExpression = (0, formatting_1.convertCustomFormatToFormatExpression)({
                type: field_1.CustomFormatType.BYTES_IEC,
                compact: field_1.Compact.KIBIBYTES,
            });
            expect(kibibyteExpression).toEqual('#,##0.00"KiB"');
            expect((0, formatting_1.formatValueWithExpression)(kibibyteExpression, 2048)).toEqual('2.00KiB');
            // Test MEBIBYTES (should divide by 1048576)
            const mebibyteExpression = (0, formatting_1.convertCustomFormatToFormatExpression)({
                type: field_1.CustomFormatType.BYTES_IEC,
                compact: field_1.Compact.MEBIBYTES,
            });
            expect(mebibyteExpression).toEqual('#,##0.00"MiB"');
            expect((0, formatting_1.formatValueWithExpression)(mebibyteExpression, 2097152)).toEqual('2.00MiB');
            // Test with rounding
            const roundedExpression = (0, formatting_1.convertCustomFormatToFormatExpression)({
                type: field_1.CustomFormatType.BYTES_IEC,
                compact: field_1.Compact.KIBIBYTES,
                round: 1,
            });
            expect(roundedExpression).toEqual('#,##0.0"KiB"');
            expect((0, formatting_1.formatValueWithExpression)(roundedExpression, 1536)).toEqual('1.5KiB');
        });
        test('backend scenario: formatOptions converted to format expression', () => {
            // This simulates the exact scenario from the backend PR
            // where formatOptions are converted to format expressions
            const formatOptions = {
                type: field_1.CustomFormatType.BYTES_IEC,
                compact: field_1.Compact.KIBIBYTES,
            };
            // Backend calls convertCustomFormatToFormatExpression
            const formatExpression = (0, formatting_1.convertCustomFormatToFormatExpression)(formatOptions);
            expect(formatExpression).toEqual('#,##0.00"KiB"');
            // Mock metric with format expression set by backend
            const mockMetric = {
                fieldType: field_1.FieldType.METRIC,
                type: field_1.MetricType.NUMBER,
                name: 'test_metric',
                label: 'Test Metric',
                table: 'test_table',
                tableLabel: 'Test Table',
                sql: 'test_sql',
                hidden: false,
                // Backend sets format expression instead of formatOptions
                format: formatExpression || undefined,
            };
            // Frontend uses formatItemValue which should call formatValueWithExpression
            const result = (0, formatting_1.formatItemValue)(mockMetric, 2048);
            expect(result).toEqual('2.00KiB'); // Should be correctly divided by 1024
            // Test larger values
            expect((0, formatting_1.formatItemValue)(mockMetric, 1024)).toEqual('1.00KiB');
            expect((0, formatting_1.formatItemValue)(mockMetric, 3072)).toEqual('3.00KiB');
        });
    });
    describe('isMomentInput', () => {
        test('should return true for moment object', () => {
            expect((0, formatting_1.isMomentInput)((0, moment_1.default)())).toBe(true);
        });
        test('should return true for dayjs object', () => {
            expect((0, formatting_1.isMomentInput)((0, dayjs_1.default)())).toBe(true);
        });
        test('should return true for dates', () => {
            expect((0, formatting_1.isMomentInput)(new Date())).toBe(true);
        });
        test('should return true for strings', () => {
            expect((0, formatting_1.isMomentInput)('2021-03-10')).toBe(true);
        });
        test('should return false for non-dates-strings-moment-dayjs types', () => {
            expect((0, formatting_1.isMomentInput)(undefined)).toBe(false);
            expect((0, formatting_1.isMomentInput)(null)).toBe(false);
        });
    });
    describe('convertCustomFormatToFormatExpression', () => {
        test('should convert format object to expression', () => {
            const customFormats = [
                {
                    type: field_1.CustomFormatType.CURRENCY,
                    currency: field_1.Format.USD,
                },
                {
                    type: field_1.CustomFormatType.CURRENCY,
                    currency: field_1.Format.JPY, // test JPY because it has no decimal
                },
                {
                    type: field_1.CustomFormatType.CURRENCY,
                    currency: field_1.Format.DKK, // test DKK because it has prefix instead of symbol
                },
                {
                    type: field_1.CustomFormatType.CURRENCY,
                    currency: field_1.Format.USD,
                    round: 0, // test with no round
                },
                {
                    type: field_1.CustomFormatType.CURRENCY,
                    currency: field_1.Format.USD,
                    round: 4, // test with extra round
                },
                {
                    type: field_1.CustomFormatType.CURRENCY,
                    currency: field_1.Format.USD,
                    compact: field_1.Compact.THOUSANDS, // test with compact
                },
                {
                    type: field_1.CustomFormatType.CURRENCY,
                    currency: field_1.Format.USD,
                    separator: field_1.NumberSeparator.COMMA_PERIOD, // test with separator
                },
                {
                    type: field_1.CustomFormatType.CURRENCY,
                    currency: field_1.Format.USD,
                    separator: field_1.NumberSeparator.NO_SEPARATOR_PERIOD, // test with no separator
                },
                {
                    type: field_1.CustomFormatType.CURRENCY,
                    currency: field_1.Format.USD,
                    // test with multiple options
                    compact: field_1.Compact.THOUSANDS,
                    round: 3,
                    separator: field_1.NumberSeparator.NO_SEPARATOR_PERIOD,
                },
                {
                    type: field_1.CustomFormatType.PERCENT,
                },
                {
                    type: field_1.CustomFormatType.PERCENT,
                    round: 0, // test with no round
                },
                {
                    type: field_1.CustomFormatType.PERCENT,
                    round: 3, // test with round
                },
                {
                    type: field_1.CustomFormatType.PERCENT,
                    separator: field_1.NumberSeparator.COMMA_PERIOD, // test with separator
                },
                {
                    type: field_1.CustomFormatType.NUMBER,
                },
                {
                    type: field_1.CustomFormatType.NUMBER,
                    round: 0, // test with no round
                },
                {
                    type: field_1.CustomFormatType.NUMBER,
                    round: 3, // test with round
                },
                {
                    type: field_1.CustomFormatType.NUMBER,
                    prefix: 'prefix', // test with prefix
                },
                {
                    type: field_1.CustomFormatType.NUMBER,
                    suffix: 'suffix', // test with suffix
                },
                {
                    type: field_1.CustomFormatType.NUMBER,
                    // test with multiple options
                    prefix: 'prefix',
                    compact: field_1.Compact.THOUSANDS,
                    round: 3,
                    separator: field_1.NumberSeparator.NO_SEPARATOR_PERIOD,
                    suffix: 'suffix',
                },
            ];
            customFormats.forEach((format) => {
                const value = 12345.1235;
                const formatExpression = (0, formatting_1.convertCustomFormatToFormatExpression)(format);
                // Check if the format expression was generated
                expect(formatExpression).not.toBe(null);
                const formattedValueWithCustomFormat = (0, formatting_1.applyCustomFormat)(value, format);
                const formattedValueWithFormatExpression = (0, formatting_1.formatValueWithExpression)(formatExpression, value);
                // Check if the formatted value is the same
                expect(formattedValueWithFormatExpression).toEqual(formattedValueWithCustomFormat);
            });
        });
    });
    describe('applyDefaultFormatting', () => {
        it('should handle boolean values', () => {
            expect((0, formatting_1.applyDefaultFormat)(true)).toBe('true');
            expect((0, formatting_1.applyDefaultFormat)(false)).toBe('false');
        });
        it('should handle falsey values', () => {
            expect((0, formatting_1.applyDefaultFormat)(undefined)).toBe('-');
            expect((0, formatting_1.applyDefaultFormat)(null)).toBe('∅');
            expect((0, formatting_1.applyDefaultFormat)(0)).toBe('0');
            expect((0, formatting_1.applyDefaultFormat)('')).toBe('');
        });
        it('should handle strings', () => {
            expect((0, formatting_1.applyDefaultFormat)('foo')).toBe('foo');
        });
        it('should handle numbers', () => {
            expect((0, formatting_1.applyDefaultFormat)(1234567)).toBe('1,234,567');
            expect((0, formatting_1.applyDefaultFormat)(1234)).toBe('1,234');
            expect((0, formatting_1.applyDefaultFormat)(-1234)).toBe('-1,234');
        });
    });
    describe('formatItemValue with parameters', () => {
        describe('conditional format expressions with ternary operators', () => {
            it('should format currency based on parameter value - USD', () => {
                const metricWithConditionalFormat = {
                    ...formatting_mock_1.metric,
                    type: field_1.MetricType.NUMBER,
                    format: '${ld.parameters.currency=="USD"?"$":"€"}0,0.00',
                };
                const parameters = { currency: 'USD' };
                expect((0, formatting_1.formatItemValue)(metricWithConditionalFormat, 1234.56, false, parameters)).toBe('$1,234.56');
            });
            it('should format currency based on parameter value - EUR', () => {
                const metricWithConditionalFormat = {
                    ...formatting_mock_1.metric,
                    type: field_1.MetricType.NUMBER,
                    format: '${ld.parameters.currency=="USD"?"$":"€"}0,0.00',
                };
                const parameters = { currency: 'EUR' };
                expect((0, formatting_1.formatItemValue)(metricWithConditionalFormat, 1234.56, false, parameters)).toBe('€1,234.56');
            });
            it('should handle simple currency parameter substitution', () => {
                const metricWithSimpleFormat = {
                    ...formatting_mock_1.metric,
                    type: field_1.MetricType.NUMBER,
                    format: '${ld.parameters.symbol}0,0.00',
                };
                expect((0, formatting_1.formatItemValue)(metricWithSimpleFormat, 1234.56, false, {
                    symbol: '£',
                })).toBe('£1,234.56');
                expect((0, formatting_1.formatItemValue)(metricWithSimpleFormat, 1234.56, false, {
                    symbol: '¥',
                })).toBe('¥1,234.56');
            });
            it('should fall back to default formatting when parameters are not provided', () => {
                const metricWithConditionalFormat = {
                    ...formatting_mock_1.metric,
                    type: field_1.MetricType.NUMBER,
                    format: '${ld.parameters.currency=="USD"?"$":"€"}0,0.00',
                };
                // Without parameters, should fall back to default number formatting
                expect((0, formatting_1.formatItemValue)(metricWithConditionalFormat, 1234.56, false, undefined)).toBe('1,234.56');
            });
            it('should handle conditional rounding precision', () => {
                const metricWithConditionalFormat = {
                    ...formatting_mock_1.metric,
                    type: field_1.MetricType.NUMBER,
                    format: '${ld.parameters.precision=="high"?"$":"$"}0,0${ld.parameters.precision=="high"?".00":""}',
                };
                expect((0, formatting_1.formatItemValue)(metricWithConditionalFormat, 1234.567, false, { precision: 'high' })).toBe('$1,234.57');
                expect((0, formatting_1.formatItemValue)(metricWithConditionalFormat, 1234.567, false, { precision: 'low' })).toBe('$1,235');
            });
            it('should work with both ld.parameters and lightdash.parameters prefixes', () => {
                const metricWithLdPrefix = {
                    ...formatting_mock_1.metric,
                    type: field_1.MetricType.NUMBER,
                    format: '${ld.parameters.symbol}0,0.00',
                };
                const metricWithLightdashPrefix = {
                    ...formatting_mock_1.metric,
                    type: field_1.MetricType.NUMBER,
                    format: '${lightdash.parameters.symbol}0,0.00',
                };
                expect((0, formatting_1.formatItemValue)(metricWithLdPrefix, 1234.56, false, {
                    symbol: '$',
                })).toBe('$1,234.56');
                expect((0, formatting_1.formatItemValue)(metricWithLightdashPrefix, 1234.56, false, {
                    symbol: '€',
                })).toBe('€1,234.56');
            });
            it('should handle different currency symbols', () => {
                const metricWithFormat = {
                    ...formatting_mock_1.metric,
                    type: field_1.MetricType.NUMBER,
                    format: '${ld.parameters.symbol}0,0.00',
                };
                expect((0, formatting_1.formatItemValue)(metricWithFormat, 1000.5, false, {
                    symbol: '$',
                })).toBe('$1,000.50');
                expect((0, formatting_1.formatItemValue)(metricWithFormat, 1000.5, false, {
                    symbol: '€',
                })).toBe('€1,000.50');
                expect((0, formatting_1.formatItemValue)(metricWithFormat, 1000.5, false, {
                    symbol: '£',
                })).toBe('£1,000.50');
            });
        });
    });
    describe('timezone-aware formatting', () => {
        const utcTimestamp = '2020-04-04T02:00:00.000Z';
        describe('formatDate', () => {
            test('existing behavior unchanged when no timezone', () => {
                const withTrue = (0, formatting_1.formatDate)(utcTimestamp, timeFrames_1.TimeFrames.DAY, true);
                const withFalse = (0, formatting_1.formatDate)(utcTimestamp, timeFrames_1.TimeFrames.DAY, false);
                const withDefault = (0, formatting_1.formatDate)(utcTimestamp, timeFrames_1.TimeFrames.DAY);
                expect(withTrue).toBe('2020-04-04');
                expect(withFalse).toBe(withDefault);
            });
            test('shifts UTC instant into project TZ for positive offsets (Tokyo)', () => {
                // Tokyo midnight Jan 14 = UTC Jan 13 15:00
                const tokyoMidnight = '2024-01-13T15:00:00.000Z';
                expect((0, formatting_1.formatDate)(tokyoMidnight, timeFrames_1.TimeFrames.DAY, false, 'Asia/Tokyo')).toBe('2024-01-14');
            });
            test('shifts UTC instant into project TZ for negative offsets (Pago_Pago)', () => {
                // Pago_Pago midnight Jan 14 = UTC Jan 14 11:00
                const ppMidnight = '2024-01-14T11:00:00.000Z';
                expect((0, formatting_1.formatDate)(ppMidnight, timeFrames_1.TimeFrames.DAY, false, 'Pacific/Pago_Pago')).toBe('2024-01-14');
            });
            test('UTC project TZ is a no-op', () => {
                const utcMidnight = '2024-01-14T00:00:00.000Z';
                expect((0, formatting_1.formatDate)(utcMidnight, timeFrames_1.TimeFrames.DAY, false, 'UTC')).toBe('2024-01-14');
            });
            test('timezone takes precedence over convertToUTC', () => {
                expect((0, formatting_1.formatDate)(utcTimestamp, timeFrames_1.TimeFrames.DAY, true, 'America/New_York')).toBe('2020-04-03');
            });
            test('year boundary falls on different years in Tokyo vs Pago_Pago', () => {
                // Jan 1 00:00 UTC = Tokyo Jan 1 09:00 (2024), PP Dec 31 13:00 (2023)
                const yearBoundary = '2024-01-01T00:00:00.000Z';
                expect((0, formatting_1.formatDate)(yearBoundary, timeFrames_1.TimeFrames.YEAR, false, 'Asia/Tokyo')).toBe('2024');
                expect((0, formatting_1.formatDate)(yearBoundary, timeFrames_1.TimeFrames.YEAR, false, 'Pacific/Pago_Pago')).toBe('2023');
            });
            test('month boundary shifts with positive offset (Tokyo)', () => {
                // Tokyo Feb 1 00:00 = UTC Jan 31 15:00
                const tokyoMonthBoundary = '2024-01-31T15:00:00.000Z';
                expect((0, formatting_1.formatDate)(tokyoMonthBoundary, timeFrames_1.TimeFrames.MONTH, false, 'Asia/Tokyo')).toBe('2024-02');
            });
            test('returns NaT for invalid input', () => {
                expect((0, formatting_1.formatDate)('not a date', timeFrames_1.TimeFrames.DAY, false, 'Asia/Tokyo')).toBe('NaT');
            });
        });
        describe('formatTimestamp', () => {
            test('formats timestamp in project timezone', () => {
                const result = (0, formatting_1.formatTimestamp)(utcTimestamp, timeFrames_1.TimeFrames.SECOND, false, 'America/New_York');
                expect(result).toBe('2020-04-03, 22:00:00 (-04:00)');
            });
            test('formats timestamp with correct offset in MILLISECOND interval', () => {
                const result = (0, formatting_1.formatTimestamp)(utcTimestamp, timeFrames_1.TimeFrames.MILLISECOND, false, 'America/New_York');
                expect(result).toContain('2020-04-03, 22:00:00:000');
                expect(result).toContain('-04:00');
            });
            test('existing behavior unchanged when no timezone', () => {
                const withTrue = (0, formatting_1.formatTimestamp)(utcTimestamp, timeFrames_1.TimeFrames.MILLISECOND, true);
                expect(withTrue).toBe('2020-04-04, 02:00:00:000 (+00:00)');
            });
        });
        describe('formatItemValue uniform timezone shift', () => {
            const tsBase = {
                ...formatting_mock_1.dimension,
                type: field_1.DimensionType.TIMESTAMP,
            };
            test('truncated HOUR shifts into project TZ (negative offset)', () => {
                // Pago_Pago 15:00 Jan 14 = UTC Jan 15 02:00
                const value = new Date('2024-01-15T02:00:00.000Z');
                expect((0, formatting_1.formatItemValue)({ ...tsBase, timeInterval: timeFrames_1.TimeFrames.HOUR }, value, false, undefined, 'Pacific/Pago_Pago')).toBe('2024-01-14, 15 (-11:00)');
            });
            test('truncated HOUR shifts into project TZ (positive offset)', () => {
                // Tokyo 11:00 Jan 15 = UTC Jan 15 02:00
                const value = new Date('2024-01-15T02:00:00.000Z');
                expect((0, formatting_1.formatItemValue)({ ...tsBase, timeInterval: timeFrames_1.TimeFrames.HOUR }, value, false, undefined, 'Asia/Tokyo')).toBe('2024-01-15, 11 (+09:00)');
            });
            test('RAW TIMESTAMP shifts into project TZ', () => {
                const value = new Date('2024-01-15T02:00:00.000Z');
                expect((0, formatting_1.formatItemValue)({ ...tsBase, timeInterval: timeFrames_1.TimeFrames.RAW }, value, false, undefined, 'Pacific/Pago_Pago')).toContain('(-11:00)');
            });
            test('DATE DAY shifts into project TZ (positive offset canary)', () => {
                // Tokyo midnight Jan 14 = UTC Jan 13 15:00
                const value = new Date('2024-01-13T15:00:00.000Z');
                expect((0, formatting_1.formatItemValue)({
                    ...formatting_mock_1.dimension,
                    type: field_1.DimensionType.DATE,
                    timeInterval: timeFrames_1.TimeFrames.DAY,
                }, value, false, undefined, 'Asia/Tokyo')).toBe('2024-01-14');
            });
            test('no timezone is byte-identical to default', () => {
                const value = new Date('2024-01-15T02:00:00.000Z');
                const hourItem = {
                    ...tsBase,
                    timeInterval: timeFrames_1.TimeFrames.HOUR,
                };
                expect((0, formatting_1.formatItemValue)(hourItem, value)).toBe((0, formatting_1.formatItemValue)(hourItem, value, undefined, undefined, undefined));
            });
        });
    });
    describe('toIsoWithProjectOffset', () => {
        const tz = 'Pacific/Pago_Pago'; // UTC-11, no DST
        test('returns undefined when timezone is unset', () => {
            expect((0, formatting_1.toIsoWithProjectOffset)('2024-01-15T02:00:00.000Z', undefined)).toBeUndefined();
        });
        test('returns undefined when timezone is UTC (no-op short-circuit)', () => {
            expect((0, formatting_1.toIsoWithProjectOffset)('2024-01-15T02:00:00.000Z', 'UTC')).toBeUndefined();
        });
        test('shifts a UTC ISO string into project offset', () => {
            expect((0, formatting_1.toIsoWithProjectOffset)('2024-01-15T02:00:00.000Z', tz)).toBe('2024-01-14T15:00:00.000-11:00');
        });
        test('always emits millis even when source had none', () => {
            expect((0, formatting_1.toIsoWithProjectOffset)('2024-01-15T02:00:00Z', tz)).toBe('2024-01-14T15:00:00.000-11:00');
        });
        test('accepts Date input', () => {
            expect((0, formatting_1.toIsoWithProjectOffset)(new Date('2024-01-15T02:00:00.000Z'), tz)).toBe('2024-01-14T15:00:00.000-11:00');
        });
        test('accepts numeric epoch ms', () => {
            const epoch = Date.UTC(2024, 0, 15, 2, 0, 0, 0);
            expect((0, formatting_1.toIsoWithProjectOffset)(epoch, tz)).toBe('2024-01-14T15:00:00.000-11:00');
        });
        test('returns undefined for null/undefined/non-instant inputs', () => {
            expect((0, formatting_1.toIsoWithProjectOffset)(null, tz)).toBeUndefined();
            expect((0, formatting_1.toIsoWithProjectOffset)(undefined, tz)).toBeUndefined();
            expect((0, formatting_1.toIsoWithProjectOffset)({}, tz)).toBeUndefined();
            expect((0, formatting_1.toIsoWithProjectOffset)(['a'], tz)).toBeUndefined();
        });
        test('returns undefined for unparseable strings', () => {
            expect((0, formatting_1.toIsoWithProjectOffset)('not-a-timestamp', tz)).toBeUndefined();
        });
        test('honors positive-offset zones (DST-aware via moment-timezone)', () => {
            // Asia/Karachi is UTC+5 year-round, so 02:00 UTC -> 07:00 +05:00
            expect((0, formatting_1.toIsoWithProjectOffset)('2024-01-15T02:00:00.000Z', 'Asia/Karachi')).toBe('2024-01-15T07:00:00.000+05:00');
        });
    });
    describe('formatTemporalCellForSpreadsheet', () => {
        const tz = 'Pacific/Pago_Pago'; // UTC-11
        const tsField = {
            ...formatting_mock_1.dimension,
            type: field_1.DimensionType.TIMESTAMP,
            name: 'event_timestamp',
        };
        const dateBaseTsField = {
            ...formatting_mock_1.dimension,
            type: field_1.DimensionType.DATE,
            name: 'event_timestamp_day',
            timeIntervalBaseDimensionType: field_1.DimensionType.TIMESTAMP,
        };
        const calendarDateField = {
            ...formatting_mock_1.dimension,
            type: field_1.DimensionType.DATE,
            name: 'order_date',
        };
        test('TIMESTAMP shifts into project tz with millis', () => {
            expect((0, formatting_1.formatTemporalCellForSpreadsheet)(tsField, '2024-01-15T18:00:00.000Z', tz)).toBe('2024-01-15 07:00:00.000');
        });
        test('TIMESTAMP without timezone formats wall-clock as-is', () => {
            expect((0, formatting_1.formatTemporalCellForSpreadsheet)(tsField, '2024-01-15T18:00:00.000Z', undefined)).toBe('2024-01-15 18:00:00.000');
        });
        test('DATE-base-TS shifts into project tz', () => {
            expect((0, formatting_1.formatTemporalCellForSpreadsheet)(dateBaseTsField, '2024-01-15T11:00:00.000Z', tz)).toBe('2024-01-15');
        });
        test('calendar DATE is not shifted even when tz is set', () => {
            expect((0, formatting_1.formatTemporalCellForSpreadsheet)(calendarDateField, '2024-01-15', tz)).toBe('2024-01-15');
        });
        test('returns undefined for non-temporal fields', () => {
            expect((0, formatting_1.formatTemporalCellForSpreadsheet)(formatting_mock_1.dimension, 'foo', tz)).toBeUndefined();
            expect((0, formatting_1.formatTemporalCellForSpreadsheet)(formatting_mock_1.metric, 42, tz)).toBeUndefined();
        });
        test('returns undefined for null / empty raw values', () => {
            expect((0, formatting_1.formatTemporalCellForSpreadsheet)(tsField, null, tz)).toBeUndefined();
            expect((0, formatting_1.formatTemporalCellForSpreadsheet)(tsField, undefined, tz)).toBeUndefined();
            expect((0, formatting_1.formatTemporalCellForSpreadsheet)(tsField, '', tz)).toBeUndefined();
        });
        test('accepts Date input', () => {
            expect((0, formatting_1.formatTemporalCellForSpreadsheet)(tsField, new Date('2024-01-15T18:00:00.000Z'), tz)).toBe('2024-01-15 07:00:00.000');
        });
    });
});
//# sourceMappingURL=formatting.test.js.map