import { DateTime } from 'luxon';
import maxmind from 'maxmind';
import app from '@adonisjs/core/services/app';
export function sanitizePayload(payload, extra) {
    if (Array.isArray(payload)) {
        return payload.map((item) => sanitizePayload(item, extra));
    }
    if (!payload || typeof payload !== 'object') {
        return payload;
    }
    const sanitized = {};
    for (const [key, value] of Object.entries(payload)) {
        if ((key.toLowerCase().endsWith('id') && key.toLowerCase() !== 'uuid') ||
            key.toLowerCase().endsWith('by') ||
            extra?.includes(key)) {
            continue;
        }
        if (Array.isArray(value)) {
            sanitized[key] = value.map((item) => item && typeof item === 'object' ? sanitizePayload(item, extra) : item);
            continue;
        }
        sanitized[key] = value && typeof value === 'object' ? sanitizePayload(value, extra) : value;
    }
    return sanitized;
}
export function toUserTimezone(utcDate, timezone) {
    if (!utcDate)
        return null;
    const dt = typeof utcDate === 'string'
        ? DateTime.fromISO(utcDate, { zone: 'utc' })
        : utcDate instanceof Date
            ? DateTime.fromJSDate(utcDate).toUTC()
            : utcDate;
    return dt.setZone(timezone).toFormat('yyyy-LL-dd HH:mm:ss');
}
export async function getTimezoneByIP(ip) {
    const dbPath = app.makePath('resources/ip-db/GeoLite2-City.mmdb');
    const lookup = await maxmind.open(dbPath);
    const geo = lookup.get(ip);
    return geo?.location?.time_zone || null;
}
//# sourceMappingURL=util.js.map