'use strict';

var z = require('zod');
var dayjs = require('dayjs');
var isSameOrBefore = require('../../_virtual/isSameOrBefore.js');
var dateHelper = require('../utils/dateHelper.js');

dayjs.extend(isSameOrBefore);
const buildFilterValidator = (field) => {
    if (!field.filter) {
        return z.z.any();
    }
    switch (field.filter.type) {
        case 'multiselect':
            return buildMultiSelectField(field);
        case 'date':
            return buildDateField(field);
        case 'daterange':
        case 'granulardaterange':
            return buildDateRangeField(field);
        case 'Radio':
        case 'Select':
        case 'autocomplete':
        case 'text':
        default:
            return buildTextField(field);
    }
};
/**
 * TEXT / STRING FIELDS
 */
const buildTextField = (field) => {
    const { filter, display } = field;
    if (!filter)
        throw new Error('Missing filter');
    let stringSchema = z.z.string().trim();
    if (filter.mandatory) {
        stringSchema = stringSchema.min(1, `${display} is required`);
    }
    if (filter.pattern) {
        const regex = new RegExp(filter.pattern);
        stringSchema = stringSchema.refine((value) => value === '' || regex.test(value), {
            message: `${display} has an invalid format`,
        });
    }
    return z.z.preprocess((value) => value ?? '', stringSchema);
};
/**
 * MULTI‑SELECT FIELDS
 */
const buildMultiSelectField = (field) => {
    const { filter, display } = field;
    if (!filter)
        throw new Error('Missing filter');
    let arraySchema = z.z.array(z.z.string());
    if (filter.mandatory) {
        arraySchema = arraySchema.min(1, `${display} Please select at least one`);
    }
    return z.z.preprocess((value) => {
        if (value == null)
            return [];
        if (Array.isArray(value))
            return value;
        return [value];
    }, arraySchema);
};
/**
 * DATE FIELDS
 */
const buildDateField = (field) => {
    const { filter, display } = field;
    if (!filter)
        throw new Error('Missing filter');
    const schema = z.z.preprocess((v) => (v === '' ? undefined : v), z.z.string().refine(dateHelper.isValidUiDate, `${display} must be a valid date`).optional());
    return filter.mandatory ? schema.refine((v) => v !== undefined, `${display} is required`) : schema;
};
/**
 * DATE RANGE FIELDS
 */
const buildDateRangeField = (field) => {
    const { filter, display } = field;
    if (!filter)
        throw new Error('Missing filter');
    if (filter.type === 'daterange' || filter.type === 'granulardaterange') {
        const isMandatory = filter.mandatory === true;
        let base = z.z.object({
            start: isMandatory
                ? z.z
                    .string()
                    .min(1, `${display} start date is required`)
                    .refine(dateHelper.isValidUiDate, `${display} start date must be a valid date`)
                : z.z
                    .string()
                    .optional()
                    .refine((value) => !value || dateHelper.isValidUiDate(value), `${display} start date must be a valid date`),
            end: isMandatory
                ? z.z
                    .string()
                    .min(1, `${display} end date is required`)
                    .refine(dateHelper.isValidUiDate, `${display} end date must be a valid date`)
                : z.z
                    .string()
                    .optional()
                    .refine((value) => !value || dateHelper.isValidUiDate(value), `${display} end date must be a valid date`),
        });
        // Cross-field validation:
        // Only enforce ordering when both dates are present
        base = base.refine(({ start, end }) => {
            if (!start || !end)
                return true;
            const startDate = dayjs(start, [...dateHelper.UI_INPUT_FORMATS], true);
            const endDate = dayjs(end, [...dateHelper.UI_INPUT_FORMATS], true);
            return startDate.isSameOrBefore(endDate);
        }, {
            message: `${display} end date must be the same as or after the start date`,
            path: ['end'],
        });
        return base;
    }
    throw new Error(`Unsupported filter type: ${filter.type}`);
};

exports.buildDateField = buildDateField;
exports.buildDateRangeField = buildDateRangeField;
exports.buildFilterValidator = buildFilterValidator;
//# sourceMappingURL=filterValidators.js.map
