"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.sleep = void 0;
exports.getCurrentTimestamp = getCurrentTimestamp;
exports.getDateRangeByTimespan = getDateRangeByTimespan;
exports.createTimespanWhereConditions = createTimespanWhereConditions;
exports.formatDate = formatDate;
exports.parseISODate = parseISODate;
exports.isValidDate = isValidDate;
exports.getStartOfDay = getStartOfDay;
exports.getStartOfWeek = getStartOfWeek;
const drizzle_orm_1 = require("drizzle-orm");
const date_fns_1 = require("date-fns");
function getCurrentTimestamp() {
    return Math.floor(new Date().getTime() / 1000);
}
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
exports.sleep = sleep;
/**
 * Get date range based on timespan type
 * @param timespan - The timespan type
 * @param customStartDate - Optional custom start date for "custom" timespan
 * @param customEndDate - Optional custom end date for "custom" timespan
 * @returns Object with startDate and endDate
 */
function getDateRangeByTimespan(timespan, customStartDate, customEndDate) {
    const now = new Date();
    switch (timespan) {
        case "hourly":
            return {
                startDate: (0, date_fns_1.subHours)(now, 1),
                endDate: now
            };
        case "daily":
            return {
                startDate: (0, date_fns_1.startOfDay)(now),
                endDate: now
            };
        case "weekly":
            return {
                startDate: (0, date_fns_1.startOfWeek)(now, { weekStartsOn: 0 }), // Sunday as start of week
                endDate: now
            };
        case "monthly":
            return {
                startDate: (0, date_fns_1.startOfMonth)(now),
                endDate: now
            };
        case "quarterly":
            return {
                startDate: (0, date_fns_1.startOfQuarter)(now),
                endDate: now
            };
        case "yearly":
            return {
                startDate: (0, date_fns_1.startOfYear)(now),
                endDate: now
            };
        case "custom":
            if (!customStartDate || !customEndDate) {
                throw new Error("Custom start and end dates are required for 'custom' timespan");
            }
            if (!(0, date_fns_1.isValid)(customStartDate) || !(0, date_fns_1.isValid)(customEndDate)) {
                throw new Error("Invalid custom start or end date provided");
            }
            return {
                startDate: customStartDate,
                endDate: customEndDate
            };
        default:
            return {
                startDate: now,
                endDate: now
            };
    }
}
/**
 * Create database where conditions for timespan-based filtering
 * @param table - The database table object
 * @param timeColumn - The time column name (e.g., 'createdAt', 'updatedAt')
 * @param orgId - The organization ID
 * @param timespan - The timespan type
 * @param customStartDate - Optional custom start date for "custom" timespan
 * @param customEndDate - Optional custom end date for "custom" timespan
 * @returns Database where conditions for filtering
 */
function createTimespanWhereConditions({ table, timeColumn, orgId, timespan = 'lifetime', customStartDate, customEndDate, extraConditions }) {
    // Base condition for organization
    let whereConditions = (0, drizzle_orm_1.eq)(table.workspaceId, orgId);
    // Only apply date range filtering when timespan is not 'lifetime'
    if (timespan !== 'lifetime') {
        // Get date range based on timespan
        const { startDate, endDate } = getDateRangeByTimespan(timespan, customStartDate, customEndDate);
        // Add time-based filtering conditions
        whereConditions = (0, drizzle_orm_1.and)(whereConditions, (0, drizzle_orm_1.gte)(table[timeColumn], startDate), (0, drizzle_orm_1.lte)(table[timeColumn], endDate));
    }
    if (extraConditions) {
        whereConditions = (0, drizzle_orm_1.and)(whereConditions, extraConditions);
    }
    return whereConditions;
}
/**
 * Format date using date-fns
 * @param date - The date to format
 * @param formatString - The format string (default: 'yyyy-MM-dd HH:mm:ss')
 * @returns Formatted date string
 */
function formatDate(date, formatString = 'yyyy-MM-dd HH:mm:ss') {
    return (0, date_fns_1.format)(date, formatString);
}
/**
 * Parse ISO date string using date-fns
 * @param dateString - ISO date string
 * @returns Parsed Date object
 */
function parseISODate(dateString) {
    return (0, date_fns_1.parseISO)(dateString);
}
/**
 * Check if a date is valid using date-fns
 * @param date - The date to validate
 * @returns True if date is valid
 */
function isValidDate(date) {
    return (0, date_fns_1.isValid)(date);
}
/**
 * Get start of day for a given date
 * @param date - The date
 * @returns Start of day Date object
 */
function getStartOfDay(date) {
    return (0, date_fns_1.startOfDay)(date);
}
/**
 * Get start of week for a given date
 * @param date - The date
 * @param weekStartsOn - Day of week to start on (0 = Sunday, 1 = Monday, etc.)
 * @returns Start of week Date object
 */
function getStartOfWeek(date, weekStartsOn = 0) {
    return (0, date_fns_1.startOfWeek)(date, { weekStartsOn });
}
//# sourceMappingURL=time.js.map