"use strict";
/**
 * Shared Routes — Markdown directory walker
 *
 * Iterative BFS/DFS over a shared directory collecting all .md files
 * within allowed roots and the configured traversal depth limit.
 */
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.collectMarkdownFiles = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const shared_routes_path_guards_1 = require("./shared-routes-path-guards");
const MAX_DIRECTORY_TRAVERSAL_DEPTH = 10;
function collectMarkdownFiles(sharedDir, allowedRoots) {
    const directoriesToVisit = [
        { path: sharedDir, depth: 0 },
    ];
    const visitedDirectories = new Set();
    const markdownFiles = [];
    while (directoriesToVisit.length > 0) {
        const current = directoriesToVisit.pop();
        if (!current) {
            continue;
        }
        const currentDir = current.path;
        const resolvedCurrentDir = (0, shared_routes_path_guards_1.safeRealPath)(currentDir);
        if (!resolvedCurrentDir || !(0, shared_routes_path_guards_1.isPathWithinAny)(resolvedCurrentDir, allowedRoots)) {
            continue;
        }
        const normalizedDirPath = process.platform === 'win32'
            ? path.resolve(resolvedCurrentDir).toLowerCase()
            : path.resolve(resolvedCurrentDir);
        if (visitedDirectories.has(normalizedDirPath)) {
            continue;
        }
        visitedDirectories.add(normalizedDirPath);
        let entries = [];
        try {
            entries = fs.readdirSync(currentDir, { withFileTypes: true });
        }
        catch {
            continue;
        }
        for (const entry of entries) {
            const entryPath = path.join(currentDir, entry.name);
            const resolvedEntryPath = (0, shared_routes_path_guards_1.safeRealPath)(entryPath);
            if (!resolvedEntryPath || !(0, shared_routes_path_guards_1.isPathWithinAny)(resolvedEntryPath, allowedRoots)) {
                continue;
            }
            let stats;
            try {
                stats = fs.statSync(resolvedEntryPath);
            }
            catch {
                continue;
            }
            if (stats.isDirectory()) {
                if (current.depth < MAX_DIRECTORY_TRAVERSAL_DEPTH) {
                    directoriesToVisit.push({ path: entryPath, depth: current.depth + 1 });
                }
                continue;
            }
            if (stats.isFile() && entry.name.toLowerCase().endsWith('.md')) {
                markdownFiles.push({
                    displayPath: entryPath,
                    resolvedPath: resolvedEntryPath,
                });
            }
        }
    }
    return markdownFiles;
}
exports.collectMarkdownFiles = collectMarkdownFiles;
//# sourceMappingURL=shared-routes-markdown-walker.js.map