"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.applyDefaultDateRange = applyDefaultDateRange;
exports.applyDefaultLimit = applyDefaultLimit;
exports.applyDefaultSorting = applyDefaultSorting;
exports.applyDefaultFilters = applyDefaultFilters;
/**
 * Normalize date range values to ISO strings
 * Handles relative dates like "30 days ago", "now", etc.
 */
function normalizeDateRange(dateRange) {
    if (!dateRange || typeof dateRange !== 'object') {
        return null;
    }
    const now = new Date();
    let start;
    let end;
    // Normalize start date
    if (!dateRange.start) {
        return null;
    }
    else if (typeof dateRange.start === 'string') {
        const startStr = dateRange.start.toLowerCase().trim();
        if (startStr === 'now' || startStr === 'today') {
            start = new Date(now);
        }
        else if (startStr.includes('days ago') || startStr.includes('day ago')) {
            const daysMatch = startStr.match(/(\d+)\s*days?\s*ago/i);
            const days = daysMatch ? Number.parseInt(daysMatch[1], 10) : 30;
            start = new Date(now);
            start.setDate(start.getDate() - days);
        }
        else if (startStr.includes('months ago') || startStr.includes('month ago')) {
            const monthsMatch = startStr.match(/(\d+)\s*months?\s*ago/i);
            const months = monthsMatch ? Number.parseInt(monthsMatch[1], 10) : 1;
            start = new Date(now);
            start.setMonth(start.getMonth() - months);
        }
        else {
            // Try to parse as ISO string or date
            start = new Date(dateRange.start);
            if (Number.isNaN(start.getTime())) {
                return null; // Invalid date
            }
        }
    }
    else {
        start = new Date(dateRange.start);
        if (Number.isNaN(start.getTime())) {
            return null; // Invalid date
        }
    }
    // Normalize end date
    if (!dateRange.end) {
        end = new Date(now);
    }
    else if (typeof dateRange.end === 'string') {
        const endStr = dateRange.end.toLowerCase().trim();
        if (endStr === 'now' || endStr === 'today') {
            end = new Date(now);
        }
        else {
            // Try to parse as ISO string or date
            end = new Date(dateRange.end);
            if (Number.isNaN(end.getTime())) {
                end = new Date(now); // Default to now if invalid
            }
        }
    }
    else {
        end = new Date(dateRange.end);
        if (Number.isNaN(end.getTime())) {
            end = new Date(now); // Default to now if invalid
        }
    }
    // Ensure start is before end
    if (start > end) {
        [start, end] = [end, start];
    }
    // Validate that dates are not too old (more than 1 year in the past)
    // This prevents using hardcoded dates from old examples
    const oneYearAgo = new Date(now);
    oneYearAgo.setFullYear(oneYearAgo.getFullYear() - 1);
    if (start < oneYearAgo) {
        // Date is too old, likely a hardcoded date from an example
        // Return null to trigger default date range
        return null;
    }
    return {
        start: start.toISOString(),
        end: end.toISOString(),
    };
}
/**
 * Apply default date range (last 30 days) if not provided
 * Also normalizes relative date strings to ISO date strings
 */
function applyDefaultDateRange(filters) {
    if (!filters) {
        filters = {};
    }
    let dateRangeApplied = false;
    if (filters.dateRange) {
        // DateRange provided, but normalize it (convert relative dates to ISO strings)
        const normalized = normalizeDateRange(filters.dateRange);
        if (normalized) {
            // Check if normalization changed the values (indicates relative dates were used)
            const originalStart = filters.dateRange.start;
            const originalEnd = filters.dateRange.end;
            filters.dateRange = normalized;
            // If the original values were strings that needed normalization, mark as applied
            if ((typeof originalStart === 'string' && (originalStart.includes('ago') || originalStart === 'now')) ||
                (typeof originalEnd === 'string' && (originalEnd === 'now'))) {
                dateRangeApplied = true;
            }
        }
        else {
            // Invalid dateRange, apply default
            const end = new Date();
            const start = new Date();
            start.setDate(start.getDate() - 30);
            filters.dateRange = {
                start: start.toISOString(),
                end: end.toISOString(),
            };
            dateRangeApplied = true;
        }
    }
    else {
        // No dateRange provided, apply default (last 30 days)
        const end = new Date();
        const start = new Date();
        start.setDate(start.getDate() - 30); // 30 days ago
        filters.dateRange = {
            start: start.toISOString(),
            end: end.toISOString(),
        };
        dateRangeApplied = true;
    }
    return { filters, dateRangeApplied };
}
/**
 * Apply default limit (100) if not provided
 */
function applyDefaultLimit(limit) {
    return limit || 100;
}
/**
 * Apply default sorting (timestamp desc) if not provided
 */
function applyDefaultSorting(sorting, defaultField = 'timestamp') {
    return (sorting || {
        field: defaultField,
        order: 'desc',
    });
}
/**
 * Apply all default filters
 */
function applyDefaultFilters(filters, limit, sorting, defaultSortField = 'timestamp') {
    const { filters: finalFilters, dateRangeApplied } = applyDefaultDateRange(filters);
    const finalLimit = applyDefaultLimit(limit);
    const finalSorting = applyDefaultSorting(sorting, defaultSortField);
    return {
        filters: finalFilters,
        limit: finalLimit,
        sorting: finalSorting,
        dateRangeApplied,
    };
}
//# sourceMappingURL=defaultFilters.js.map