"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.addBucketUtc = addBucketUtc;
exports.fillTimeSeriesByDimension = fillTimeSeriesByDimension;
function normalizeClickhouseDate(value) {
    if (value instanceof Date) {
        return value;
    }
    const s = typeof value === 'string' ? value.trim() : String(value ?? '').trim();
    if (!s) {
        return new Date(0);
    }
    if (/^\d{4}-\d{2}-\d{2}$/.test(s)) {
        return new Date(`${s}T00:00:00Z`);
    }
    if (/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/.test(s)) {
        return new Date(`${s.replace(' ', 'T')}Z`);
    }
    const d = new Date(s);
    return Number.isFinite(d.getTime()) ? d : new Date(0);
}
function floorToBucketUtc(d, bucketUnit) {
    if (bucketUnit === 'hour') {
        return new Date(Date.UTC(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate(), d.getUTCHours(), 0, 0, 0));
    }
    if (bucketUnit === 'day') {
        return new Date(Date.UTC(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate(), 0, 0, 0, 0));
    }
    if (bucketUnit === 'week') {
        const day = d.getUTCDay();
        const daysSinceMonday = (day + 6) % 7;
        return new Date(Date.UTC(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate() - daysSinceMonday, 0, 0, 0, 0));
    }
    return new Date(Date.UTC(d.getUTCFullYear(), d.getUTCMonth(), 1, 0, 0, 0, 0));
}
function addBucketUtc(d, bucketUnit) {
    if (bucketUnit === 'hour') {
        return new Date(d.getTime() + 60 * 60 * 1000);
    }
    if (bucketUnit === 'day') {
        return new Date(d.getTime() + 24 * 60 * 60 * 1000);
    }
    if (bucketUnit === 'week') {
        return new Date(d.getTime() + 7 * 24 * 60 * 60 * 1000);
    }
    return new Date(Date.UTC(d.getUTCFullYear(), d.getUTCMonth() + 1, 1, 0, 0, 0, 0));
}
function buildTimeBucketsUtc(config) {
    const start = floorToBucketUtc(config.actualStartTs, config.bucketUnit);
    const end = floorToBucketUtc(config.actualEndTs, config.bucketUnit);
    const buckets = [];
    for (let t = start; t.getTime() <= end.getTime(); t = addBucketUtc(t, config.bucketUnit)) {
        buckets.push(t);
    }
    return buckets;
}
function fillTimeSeriesByDimension(rows, config, dimKey, valueKey) {
    const buckets = buildTimeBucketsUtc(config);
    const dimValues = Array.from(new Set((rows ?? []).map((r) => String(r?.[dimKey] ?? '')).filter(Boolean)));
    if (dimValues.length === 0) {
        return [];
    }
    const byDim = new Map();
    for (const dim of dimValues) {
        byDim.set(dim, new Map());
    }
    for (const r of rows ?? []) {
        const dim = String(r?.[dimKey] ?? '');
        if (!dim) {
            continue;
        }
        if (!byDim.has(dim)) {
            byDim.set(dim, new Map());
        }
        const rawBucket = r?.bucket ?? r?.timestamp;
        const dt = normalizeClickhouseDate(rawBucket);
        const bucketDt = floorToBucketUtc(dt, config.bucketUnit);
        const ms = bucketDt.getTime();
        const value = Number(r?.[valueKey] ?? 0);
        byDim.get(dim)?.set(ms, Number.isFinite(value) ? value : 0);
    }
    const out = [];
    for (const b of buckets) {
        const ms = b.getTime();
        for (const dim of dimValues) {
            out.push({
                timestamp: b,
                [dimKey]: dim,
                [valueKey]: byDim.get(dim)?.get(ms) ?? 0,
            });
        }
    }
    out.sort((a, b) => {
        const ta = a.timestamp.getTime();
        const tb = b.timestamp.getTime();
        if (ta !== tb) {
            return ta - tb;
        }
        return Number(b?.[valueKey] ?? 0) - Number(a?.[valueKey] ?? 0);
    });
    return out;
}
//# sourceMappingURL=timeSeries.js.map