"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.tableCalcWindowFunctionSchema = void 0;
const zod_1 = require("zod");
const field_1 = require("../../../../types/field");
const fieldId_1 = require("../fieldId");
const tableCalcBaseSchemas_1 = require("./tableCalcBaseSchemas");
const windowFunctionTypeSchema = zod_1.z.enum([
    field_1.WindowFunctionType.ROW_NUMBER,
    field_1.WindowFunctionType.PERCENT_RANK,
    field_1.WindowFunctionType.CUME_DIST,
    field_1.WindowFunctionType.RANK,
    field_1.WindowFunctionType.SUM,
    field_1.WindowFunctionType.AVG,
    field_1.WindowFunctionType.COUNT,
    field_1.WindowFunctionType.MIN,
    field_1.WindowFunctionType.MAX,
]);
exports.tableCalcWindowFunctionSchema = tableCalcBaseSchemas_1.baseTableCalcSchema.extend({
    type: zod_1.z.literal('window_function'),
    windowFunction: windowFunctionTypeSchema.describe([
        'Type of window function to apply.',
        '',
        '**Ranking functions** (fieldId optional, ignored if provided):',
        '- row_number: Sequential numbering (1, 2, 3...)',
        '- percent_rank: Relative rank as percentage (0.0-1.0)',
        '- cume_dist: Cumulative distribution as percentage (0.0-1.0)',
        '- rank: Positional rank (1, 2, 3...) with ties',
        '',
        '**Aggregate functions** (fieldId required):',
        '- sum: Sum values within frame',
        '- avg: Average values within frame',
        '- count: Count values within frame',
        '- min: Minimum value within frame',
        '- max: Maximum value within frame',
    ].join('\n')),
    fieldId: (0, fieldId_1.getFieldIdSchema)({
        additionalDescription: 'Field to aggregate (required for sum/avg/count/min/max, not used for row_number/percent_rank/cume_dist/rank)',
    }).nullable(),
    orderBy: zod_1.z
        .array(tableCalcBaseSchemas_1.orderBySchema)
        .nullable()
        .describe(tableCalcBaseSchemas_1.orderBySchemaDescription),
    partitionBy: zod_1.z
        .array(tableCalcBaseSchemas_1.partitionBySchema)
        .nullable()
        .describe(tableCalcBaseSchemas_1.partitionBySchemaDescription),
    frame: zod_1.z
        .object({
        frameType: zod_1.z
            .enum(['rows', 'range'])
            .describe([
            'Frame type:',
            '- rows: Physical row count',
            '- range: Logical value-based (includes peer rows with same ORDER BY value)',
        ].join('\n')),
        start: zod_1.z
            .object({
            type: zod_1.z.enum([
                'unbounded_preceding',
                'preceding',
                'current_row',
                'following',
                'unbounded_following',
            ]),
            offset: zod_1.z
                .number()
                .int()
                .positive()
                .nullable()
                .describe('Number of rows for PRECEDING/FOLLOWING (e.g., 6 for "6 PRECEDING")'),
        })
            .nullable()
            .describe('Start boundary. Null for single-boundary syntax.'),
        end: zod_1.z
            .object({
            type: zod_1.z.enum([
                'unbounded_preceding',
                'preceding',
                'current_row',
                'following',
                'unbounded_following',
            ]),
            offset: zod_1.z
                .number()
                .int()
                .positive()
                .nullable()
                .describe('Number of rows for PRECEDING/FOLLOWING'),
        })
            .describe('End boundary (required)'),
    })
        .nullable()
        .describe([
        'Defines which rows to include in calculation. Null uses database defaults.',
        '',
        '**Common patterns:**',
        '- Moving average (N periods): {frameType: "rows", start: {type: "preceding", offset: N-1}, end: {type: "current_row"}}',
        '- Running total: {frameType: "rows", start: {type: "unbounded_preceding"}, end: {type: "current_row"}}',
        '- Centered window: {frameType: "rows", start: {type: "preceding", offset: 1}, end: {type: "following", offset: 1}}',
        '',
        '**Default when null:**',
        '- Aggregates WITH ORDER BY: RANGE UNBOUNDED PRECEDING AND CURRENT ROW (cumulative)',
        '- Aggregates WITHOUT ORDER BY: Entire partition (all rows)',
        '- Ranking functions: Always use entire partition (frame clause has no effect)',
    ].join('\n')),
});
//# sourceMappingURL=tableCalcWindowFunction.js.map