"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.formatSettingsPathList = exports.findCodexTranslatorUrlPaths = exports.CODEX_TRANSLATOR_URL_MARKER = void 0;
exports.CODEX_TRANSLATOR_URL_MARKER = '/api/provider/codex';
const MAX_CODEX_TRANSLATOR_SCAN_DEPTH = 100;
const MAX_CODEX_TRANSLATOR_SCAN_NODES = 10000;
function formatSettingsPathSegment(basePath, segment) {
    if (typeof segment === 'number') {
        return `${basePath}[${segment}]`;
    }
    if (/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(segment)) {
        return basePath ? `${basePath}.${segment}` : segment;
    }
    return `${basePath}[${JSON.stringify(segment)}]`;
}
function findCodexTranslatorUrlPaths(value, path = '') {
    const matches = [];
    const stack = [{ value, path, depth: 0 }];
    const seen = new WeakSet();
    let visitedNodes = 0;
    while (stack.length > 0 && visitedNodes < MAX_CODEX_TRANSLATOR_SCAN_NODES) {
        const item = stack.pop();
        if (!item) {
            break;
        }
        visitedNodes += 1;
        if (typeof item.value === 'string') {
            if (item.value.includes(exports.CODEX_TRANSLATOR_URL_MARKER)) {
                matches.push(item.path || '(root)');
            }
            continue;
        }
        if (typeof item.value !== 'object' || item.value === null) {
            continue;
        }
        if (seen.has(item.value)) {
            continue;
        }
        seen.add(item.value);
        if (item.depth >= MAX_CODEX_TRANSLATOR_SCAN_DEPTH) {
            continue;
        }
        if (Array.isArray(item.value)) {
            for (let index = item.value.length - 1; index >= 0; index -= 1) {
                stack.push({
                    value: item.value[index],
                    path: formatSettingsPathSegment(item.path, index),
                    depth: item.depth + 1,
                });
            }
            continue;
        }
        const entries = Object.entries(item.value);
        for (let index = entries.length - 1; index >= 0; index -= 1) {
            const [key, child] = entries[index];
            stack.push({
                value: child,
                path: formatSettingsPathSegment(item.path, key),
                depth: item.depth + 1,
            });
        }
    }
    return matches;
}
exports.findCodexTranslatorUrlPaths = findCodexTranslatorUrlPaths;
function formatSettingsPathList(paths) {
    const visiblePaths = paths.slice(0, 5);
    const remainingCount = paths.length - visiblePaths.length;
    return remainingCount > 0
        ? `${visiblePaths.join(', ')} (+${remainingCount} more)`
        : visiblePaths.join(', ');
}
exports.formatSettingsPathList = formatSettingsPathList;
//# sourceMappingURL=stale-codex-translator-settings.js.map