"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const zod_1 = require("zod");
const field_1 = require("../../../../types/field");
const filter_1 = require("../../../../types/filter");
const fieldId_1 = require("../fieldId");
const filterDescriptionUtils_1 = require("./filterDescriptionUtils");
const dateOrDateTimeSchema = zod_1.z
    .union([zod_1.z.string().date(), zod_1.z.string().datetime()])
    .describe('ISO date (YYYY-MM-DD) or ISO datetime. Do not use relative phrases like "last 2 weeks" here.');
const commonDateFilterRuleSchema = zod_1.z.object({
    fieldId: (0, fieldId_1.getFieldIdSchema)({ additionalDescription: null }),
    fieldType: zod_1.z.union([
        zod_1.z.literal(field_1.DimensionType.DATE),
        zod_1.z.literal(field_1.DimensionType.TIMESTAMP),
        zod_1.z.literal(field_1.MetricType.DATE),
        zod_1.z.literal(field_1.MetricType.TIMESTAMP),
    ]),
    fieldFilterType: zod_1.z.literal(filter_1.FilterType.DATE),
});
const dateFilterSchema = zod_1.z.union([
    commonDateFilterRuleSchema
        .extend({
        operator: zod_1.z
            .union([
            zod_1.z.literal(filter_1.FilterOperator.NULL),
            zod_1.z.literal(filter_1.FilterOperator.NOT_NULL),
        ])
            .describe(filterDescriptionUtils_1.datePresenceOperatorDescription),
    })
        .describe(`Use for date/timestamp fields when checking if a value is missing or present. Do not include values. ${(0, filterDescriptionUtils_1.filterJsonExamples)({
        fieldId: 'orders_order_date',
        fieldType: field_1.DimensionType.DATE,
        fieldFilterType: filter_1.FilterType.DATE,
        operator: filter_1.FilterOperator.NULL,
    }, {
        fieldId: 'orders_created_at',
        fieldType: field_1.DimensionType.TIMESTAMP,
        fieldFilterType: filter_1.FilterType.DATE,
        operator: filter_1.FilterOperator.NOT_NULL,
    })}`),
    commonDateFilterRuleSchema
        .extend({
        operator: zod_1.z
            .union([
            zod_1.z.literal(filter_1.FilterOperator.EQUALS),
            zod_1.z.literal(filter_1.FilterOperator.NOT_EQUALS),
        ])
            .describe(`Use ${(0, filterDescriptionUtils_1.filterOperatorList)(filter_1.FilterOperator.EQUALS, filter_1.FilterOperator.NOT_EQUALS)} for explicit dates or datetimes.`),
        values: zod_1.z
            .array(dateOrDateTimeSchema)
            .describe('One or more explicit ISO dates/datetimes.'),
    })
        .describe(`Use for specific dates like 2024-01-01. For relative periods like "last 2 weeks", use ${filter_1.FilterOperator.IN_THE_PAST}. ${(0, filterDescriptionUtils_1.filterJsonExamples)({
        fieldId: 'orders_order_date',
        fieldType: field_1.DimensionType.DATE,
        fieldFilterType: filter_1.FilterType.DATE,
        operator: filter_1.FilterOperator.EQUALS,
        values: ['2024-01-01'],
    }, {
        fieldId: 'orders_created_at',
        fieldType: field_1.DimensionType.TIMESTAMP,
        fieldFilterType: filter_1.FilterType.DATE,
        operator: filter_1.FilterOperator.NOT_EQUALS,
        values: ['2024-01-01T00:00:00Z'],
    })}`),
    commonDateFilterRuleSchema
        .extend({
        operator: zod_1.z
            .union([
            zod_1.z.literal(filter_1.FilterOperator.IN_THE_PAST),
            zod_1.z.literal(filter_1.FilterOperator.NOT_IN_THE_PAST),
            zod_1.z.literal(filter_1.FilterOperator.IN_THE_NEXT),
            // NOTE: NOT_IN_THE_NEXT is not supported...
        ])
            .describe(`Use ${filter_1.FilterOperator.IN_THE_PAST} for last/past N periods, ${filter_1.FilterOperator.IN_THE_NEXT} for next N periods, ${filter_1.FilterOperator.NOT_IN_THE_PAST} to exclude recent periods.`),
        values: zod_1.z
            .array(zod_1.z.number())
            .length(1)
            .describe('Exactly one positive number of periods, e.g. [2].'),
        settings: zod_1.z
            .object({
            completed: zod_1.z
                .boolean()
                .describe('false for rolling periods including the current partial period; true for completed periods only.'),
            unitOfTime: zod_1.z
                .union([
                zod_1.z.literal(filter_1.UnitOfTime.days),
                zod_1.z.literal(filter_1.UnitOfTime.weeks),
                zod_1.z.literal(filter_1.UnitOfTime.months),
                zod_1.z.literal(filter_1.UnitOfTime.quarters),
                zod_1.z.literal(filter_1.UnitOfTime.years),
            ])
                .describe('Period unit for the relative date filter.'),
        })
            .describe('Relative period settings.'),
    })
        .describe(`Use for relative date requests such as "last 2 weeks" or "next 3 months". ${(0, filterDescriptionUtils_1.filterJsonExamples)({
        fieldId: 'orders_order_date',
        fieldType: field_1.DimensionType.DATE,
        fieldFilterType: filter_1.FilterType.DATE,
        operator: filter_1.FilterOperator.IN_THE_PAST,
        values: [2],
        settings: {
            completed: false,
            unitOfTime: filter_1.UnitOfTime.weeks,
        },
    }, {
        fieldId: 'orders_order_date',
        fieldType: field_1.DimensionType.DATE,
        fieldFilterType: filter_1.FilterType.DATE,
        operator: filter_1.FilterOperator.IN_THE_PAST,
        values: [3],
        settings: {
            completed: true,
            unitOfTime: filter_1.UnitOfTime.months,
        },
    }, {
        fieldId: 'orders_ship_date',
        fieldType: field_1.DimensionType.DATE,
        fieldFilterType: filter_1.FilterType.DATE,
        operator: filter_1.FilterOperator.IN_THE_NEXT,
        values: [14],
        settings: {
            completed: false,
            unitOfTime: filter_1.UnitOfTime.days,
        },
    }, {
        fieldId: 'orders_order_date',
        fieldType: field_1.DimensionType.DATE,
        fieldFilterType: filter_1.FilterType.DATE,
        operator: filter_1.FilterOperator.NOT_IN_THE_PAST,
        values: [7],
        settings: {
            completed: false,
            unitOfTime: filter_1.UnitOfTime.days,
        },
    })}`),
    commonDateFilterRuleSchema
        .extend({
        operator: zod_1.z
            .union([
            zod_1.z.literal(filter_1.FilterOperator.IN_THE_CURRENT),
            zod_1.z.literal(filter_1.FilterOperator.NOT_IN_THE_CURRENT),
        ])
            .describe('Use for this/current period or to exclude this/current period.'),
        values: zod_1.z
            .array(zod_1.z.literal(1))
            .length(1)
            .describe('Always [1] for current-period filters.'),
        settings: zod_1.z
            .object({
            completed: zod_1.z
                .literal(false)
                .describe('Always false for current-period filters.'),
            unitOfTime: zod_1.z
                .union([
                zod_1.z.literal(filter_1.UnitOfTime.days),
                zod_1.z.literal(filter_1.UnitOfTime.weeks),
                zod_1.z.literal(filter_1.UnitOfTime.months),
                zod_1.z.literal(filter_1.UnitOfTime.quarters),
                zod_1.z.literal(filter_1.UnitOfTime.years),
            ])
                .describe('Current period unit, e.g. weeks for this week.'),
        })
            .describe('Current-period settings.'),
    })
        .describe(`Use for current date requests such as "today", "this week", "this month", or "this year". ${(0, filterDescriptionUtils_1.filterJsonExamples)({
        fieldId: 'orders_order_date',
        fieldType: field_1.DimensionType.DATE,
        fieldFilterType: filter_1.FilterType.DATE,
        operator: filter_1.FilterOperator.IN_THE_CURRENT,
        values: [1],
        settings: {
            completed: false,
            unitOfTime: filter_1.UnitOfTime.days,
        },
    }, {
        fieldId: 'orders_order_date',
        fieldType: field_1.DimensionType.DATE,
        fieldFilterType: filter_1.FilterType.DATE,
        operator: filter_1.FilterOperator.NOT_IN_THE_CURRENT,
        values: [1],
        settings: {
            completed: false,
            unitOfTime: filter_1.UnitOfTime.months,
        },
    })}`),
    commonDateFilterRuleSchema
        .extend({
        operator: zod_1.z
            .union([
            zod_1.z.literal(filter_1.FilterOperator.LESS_THAN),
            zod_1.z.literal(filter_1.FilterOperator.LESS_THAN_OR_EQUAL),
            zod_1.z.literal(filter_1.FilterOperator.GREATER_THAN),
            zod_1.z.literal(filter_1.FilterOperator.GREATER_THAN_OR_EQUAL),
        ])
            .describe('Use for before/after comparisons against one explicit date or datetime.'),
        values: zod_1.z
            .array(dateOrDateTimeSchema)
            .length(1)
            .describe('Exactly one explicit ISO date/datetime threshold.'),
    })
        .describe(`Use for date/timestamp fields before, on-or-before, after, or on-or-after a specific date. ${(0, filterDescriptionUtils_1.filterJsonExamples)({
        fieldId: 'orders_order_date',
        fieldType: field_1.DimensionType.DATE,
        fieldFilterType: filter_1.FilterType.DATE,
        operator: filter_1.FilterOperator.GREATER_THAN_OR_EQUAL,
        values: ['2024-01-01'],
    }, {
        fieldId: 'orders_created_at',
        fieldType: field_1.DimensionType.TIMESTAMP,
        fieldFilterType: filter_1.FilterType.DATE,
        operator: filter_1.FilterOperator.LESS_THAN,
        values: ['2024-02-01T00:00:00Z'],
    })}`),
    commonDateFilterRuleSchema
        .extend({
        operator: zod_1.z
            .literal(filter_1.FilterOperator.IN_BETWEEN)
            .describe('Use for explicit date ranges between two dates/datetimes.'),
        values: zod_1.z
            .array(dateOrDateTimeSchema)
            .length(2)
            .describe('Exactly two explicit ISO dates/datetimes: [start, end].'),
    })
        .describe(`Use for explicit date ranges such as from 2024-01-01 to 2024-01-31. ${(0, filterDescriptionUtils_1.filterJsonExamples)({
        fieldId: 'orders_order_date',
        fieldType: field_1.DimensionType.DATE,
        fieldFilterType: filter_1.FilterType.DATE,
        operator: filter_1.FilterOperator.IN_BETWEEN,
        values: ['2024-01-01', '2024-01-31'],
    }, {
        fieldId: 'orders_created_at',
        fieldType: field_1.DimensionType.TIMESTAMP,
        fieldFilterType: filter_1.FilterType.DATE,
        operator: filter_1.FilterOperator.IN_BETWEEN,
        values: ['2024-01-01T00:00:00Z', '2024-01-31T23:59:59Z'],
    })}`),
]);
exports.default = dateFilterSchema;
//# sourceMappingURL=dateFilters.js.map