"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.filtersSchemaTransformed = exports.filtersSchemaV2 = exports.stringFilterSchema = exports.numberFilterSchema = exports.dateFilterSchema = exports.booleanFilterSchema = void 0;
const tslib_1 = require("tslib");
const uuid_1 = require("uuid");
const zod_1 = require("zod");
const assertUnreachable_1 = tslib_1.__importDefault(require("../../../../utils/assertUnreachable"));
const booleanFilters_1 = tslib_1.__importDefault(require("./booleanFilters"));
exports.booleanFilterSchema = booleanFilters_1.default;
const dateFilters_1 = tslib_1.__importDefault(require("./dateFilters"));
exports.dateFilterSchema = dateFilters_1.default;
const numberFilters_1 = tslib_1.__importDefault(require("./numberFilters"));
exports.numberFilterSchema = numberFilters_1.default;
const stringFilters_1 = tslib_1.__importDefault(require("./stringFilters"));
exports.stringFilterSchema = stringFilters_1.default;
const filterAndOrSchema = zod_1.z
    .union([zod_1.z.literal('and'), zod_1.z.literal('or')])
    .describe('Type of filter group operation');
const filterRuleSchema = zod_1.z.union([
    booleanFilters_1.default,
    stringFilters_1.default,
    numberFilters_1.default,
    dateFilters_1.default,
]);
const filterRuleSchemaTransformed = filterRuleSchema.transform((data) => ({
    id: (0, uuid_1.v4)(),
    target: {
        fieldId: data.fieldId,
        fieldFilterType: data.fieldFilterType,
    },
    operator: data.operator,
    values: 'values' in data ? data.values : [],
    ...('settings' in data ? { settings: data.settings } : {}),
}));
// TODO: deprecate this in 2 weeks
const filtersSchemaV1 = zod_1.z.object({
    type: filterAndOrSchema,
    dimensions: zod_1.z.array(filterRuleSchema).nullable(),
    metrics: zod_1.z.array(filterRuleSchema).nullable(),
});
exports.filtersSchemaV2 = filtersSchemaV1.extend({
    tableCalculations: zod_1.z.array(numberFilters_1.default).nullable(),
});
const filtersSchemaAndFilterRulesTransformedV1 = zod_1.z.object({
    type: filterAndOrSchema,
    dimensions: zod_1.z.array(filterRuleSchemaTransformed).nullable(),
    metrics: zod_1.z.array(filterRuleSchemaTransformed).nullable(),
});
const filtersSchemaAndFilterRulesTransformedV2 = filtersSchemaAndFilterRulesTransformedV1.extend({
    tableCalculations: zod_1.z.array(filterRuleSchemaTransformed).nullable(),
});
const filtersSchemaAndFilterRulesTransformed = zod_1.z
    .union([
    filtersSchemaAndFilterRulesTransformedV2,
    filtersSchemaAndFilterRulesTransformedV1,
])
    .nullable();
exports.filtersSchemaTransformed = filtersSchemaAndFilterRulesTransformed.transform((data) => {
    if (!data) {
        return {
            dimensions: { id: (0, uuid_1.v4)(), and: [] },
            metrics: { id: (0, uuid_1.v4)(), and: [] },
            tableCalculations: { id: (0, uuid_1.v4)(), and: [] },
        };
    }
    switch (data.type) {
        case 'and':
            return {
                dimensions: {
                    id: (0, uuid_1.v4)(),
                    and: data.dimensions ?? [],
                },
                metrics: {
                    id: (0, uuid_1.v4)(),
                    and: data.metrics ?? [],
                },
                tableCalculations: {
                    id: (0, uuid_1.v4)(),
                    and: 'tableCalculations' in data
                        ? (data.tableCalculations ?? [])
                        : [],
                },
            };
        case 'or':
            return {
                dimensions: {
                    id: (0, uuid_1.v4)(),
                    or: data.dimensions ?? [],
                },
                metrics: {
                    id: (0, uuid_1.v4)(),
                    or: data.metrics ?? [],
                },
                tableCalculations: {
                    id: (0, uuid_1.v4)(),
                    or: 'tableCalculations' in data
                        ? (data.tableCalculations ?? [])
                        : [],
                },
            };
        default:
            return (0, assertUnreachable_1.default)(data.type, 'Invalid filter type');
    }
});
//# sourceMappingURL=index.js.map