"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getAccountUserTimezone = getAccountUserTimezone;
exports.resolveQueryTimezone = resolveQueryTimezone;
const errors_1 = require("../types/errors");
const scheduler_1 = require("./scheduler");
/**
 * Returns the account's user-level timezone preference, or null if the
 * account belongs to an anonymous viewer (embeds / JWT) where no profile
 * preference exists.
 */
function getAccountUserTimezone(account) {
    if (account.user.type !== 'registered') {
        return null;
    }
    return account.user.timezone ?? null;
}
/**
 * Resolves the effective timezone for a query using the hierarchy:
 *   metricQuery.timezone → user timezone → project timezone → 'UTC'
 *
 * The project timezone (from getQueryTimezoneForProject) already handles
 * the project → config → 'UTC' fallback. The chart-level override sits on
 * top, and the user's profile timezone slots between the two so a viewer
 * with a personal preference sees their zone whenever the chart doesn't
 * pin one.
 *
 * Validates the resolved timezone to prevent SQL injection — the result
 * is interpolated into warehouse SQL strings (e.g., AT TIME ZONE '...').
 */
function resolveQueryTimezone(metricQuery, projectTimezone, userTimezone) {
    const timezone = metricQuery.timezone ?? userTimezone ?? projectTimezone;
    if (!(0, scheduler_1.isValidTimezone)(timezone)) {
        throw new errors_1.ParameterError(`Invalid timezone: ${timezone}`);
    }
    return timezone;
}
//# sourceMappingURL=resolveQueryTimezone.js.map