"use strict";
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 () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolveAdrDirectoryRelativePath = resolveAdrDirectoryRelativePath;
exports.getAdrDirectoryPath = getAdrDirectoryPath;
exports.loadArchitectureDecisionRecords = loadArchitectureDecisionRecords;
const path = __importStar(require("node:path"));
const node_fs_1 = require("node:fs");
const adrParser_1 = require("./adrParser");
const architectureInit_1 = require("../architectureInit");
const moduleDiscovery_1 = require("../moduleDiscovery");
const adrFilePattern = /^adr-.+\.md$/i;
function normalizeRelativePath(value) {
    return value.replace(/\\/g, "/").replace(/^\/+/, "");
}
async function resolveAdrDirectoryRelativePath(workspaceRoot) {
    const contract = await (0, architectureInit_1.loadArchitectureContract)(workspaceRoot);
    if (typeof contract?.artifacts?.adrDirectory === "string" &&
        contract.artifacts.adrDirectory.trim().length > 0) {
        return normalizeRelativePath(contract.artifacts.adrDirectory);
    }
    const docsRoot = (0, moduleDiscovery_1.resolveDocsRootFromContract)(contract);
    return `${docsRoot}/adrs`;
}
function getAdrDirectoryPath(workspaceRoot, adrDirectoryRelativePath = "docs/adrs") {
    return path.join(workspaceRoot, ...normalizeRelativePath(adrDirectoryRelativePath).split("/"));
}
async function loadArchitectureDecisionRecords(workspaceRoot, options) {
    const adrDirectoryRelativePath = options?.adrDirectoryRelativePath ??
        (await resolveAdrDirectoryRelativePath(workspaceRoot));
    const adrDirectory = getAdrDirectoryPath(workspaceRoot, adrDirectoryRelativePath);
    let entries;
    try {
        entries = await node_fs_1.promises.readdir(adrDirectory, { withFileTypes: true });
    }
    catch (error) {
        if (error.code === "ENOENT") {
            return [];
        }
        throw error;
    }
    const records = [];
    for (const entry of entries.sort((left, right) => left.name.localeCompare(right.name))) {
        if (!entry.isFile() || !adrFilePattern.test(entry.name)) {
            continue;
        }
        const absolutePath = path.join(adrDirectory, entry.name);
        let rawContent = "";
        try {
            rawContent = await node_fs_1.promises.readFile(absolutePath, { encoding: "utf8" });
        }
        catch {
            rawContent = "";
        }
        records.push((0, adrParser_1.parseArchitectureDecisionRecord)({
            fileName: entry.name,
            filePath: path
                .relative(workspaceRoot, absolutePath)
                .replace(/\\/g, "/"),
            rawContent,
        }));
    }
    return records.sort((left, right) => left.fileName.localeCompare(right.fileName));
}
//# sourceMappingURL=adrLoader.js.map