import { wrapMinimal } from '../utils.js';
export function parseComJson(codigo) {
    try {
        const parsed = JSON.parse(codigo);
        const info = {
            type: Array.isArray(parsed) ? 'array' : typeof parsed,
        };
        if (typeof parsed === 'object' && parsed !== null && !Array.isArray(parsed)) {
            const keys = Object.keys(parsed);
            info.keys = keys;
            info.depth = estimateDepth(parsed, 0);
            info.totalKeys = countKeys(parsed);
        }
        if (Array.isArray(parsed)) {
            info.length = parsed.length;
            info.elementTypes = [...new Set(parsed.map(e => Array.isArray(e) ? 'array' : typeof e))];
        }
        return wrapMinimal('json', info);
    }
    catch {
        return null;
    }
}
function estimateDepth(obj, depth) {
    if (typeof obj !== 'object' || obj === null)
        return depth;
    let maxDepth = depth;
    for (const val of Object.values(obj)) {
        maxDepth = Math.max(maxDepth, estimateDepth(val, depth + 1));
    }
    return maxDepth;
}
function countKeys(obj) {
    if (typeof obj !== 'object' || obj === null)
        return 0;
    let count = 0;
    for (const [, val] of Object.entries(obj)) {
        count++;
        if (typeof val === 'object' && val !== null) {
            count += countKeys(val);
        }
    }
    return count;
}
//# sourceMappingURL=json.js.map