"use strict";
// SPDX-License-Identifier: AGPL-3.0-or-later
// Copyright (C) 2024-2026 Gracker (Chris)
// This file is part of SmartPerfetto. See LICENSE for details.
Object.defineProperty(exports, "__esModule", { value: true });
exports.rowObject = rowObject;
exports.rowsToObjects = rowsToObjects;
exports.queryRows = queryRows;
exports.toNumber = toNumber;
exports.toNullableNumber = toNullableNumber;
exports.toOptionalString = toOptionalString;
exports.toBool = toBool;
exports.nsToMs = nsToMs;
function rowObject(columns, row) {
    const out = {};
    columns.forEach((column, index) => {
        out[column] = row[index];
    });
    return out;
}
function rowsToObjects(result) {
    return result.rows.map((row) => rowObject(result.columns, row));
}
async function queryRows(tp, traceId, sql) {
    const result = await tp.query(traceId, sql);
    return rowsToObjects(result);
}
function toNumber(value, fallback = 0) {
    if (typeof value === 'number')
        return Number.isFinite(value) ? value : fallback;
    if (typeof value === 'bigint') {
        const converted = Number(value);
        return Number.isFinite(converted) ? converted : fallback;
    }
    if (typeof value === 'string' && value.trim()) {
        const converted = Number(value);
        return Number.isFinite(converted) ? converted : fallback;
    }
    return fallback;
}
function toNullableNumber(value) {
    if (value === undefined || value === null || value === '')
        return null;
    const converted = toNumber(value, Number.NaN);
    return Number.isFinite(converted) ? converted : null;
}
function toOptionalString(value) {
    if (value === undefined || value === null)
        return null;
    const text = String(value).trim();
    return text.length > 0 ? text : null;
}
function toBool(value) {
    if (value === undefined || value === null)
        return null;
    if (typeof value === 'boolean')
        return value;
    const numeric = toNullableNumber(value);
    return numeric === null ? null : numeric > 0;
}
function nsToMs(value) {
    return Math.round((value / 1e6) * 100) / 100;
}
//# sourceMappingURL=traceProcessorRowUtils.js.map