"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.scanTeamDir = scanTeamDir;
exports.ensureAuthorInTeam = ensureAuthorInTeam;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const frontmatter_1 = require("./frontmatter");
const logger_1 = require("../lib/logger");
const log = (0, logger_1.createLogger)('team');
/**
 * Scan `.team/` directory for team member markdown files.
 * Each file: `.team/{id}.md` with frontmatter { name, email }.
 */
function scanTeamDir(teamDir) {
    if (!fs.existsSync(teamDir))
        return [];
    const members = [];
    for (const entry of fs.readdirSync(teamDir, { withFileTypes: true })) {
        if (!entry.isFile() || !entry.name.endsWith('.md'))
            continue;
        const id = entry.name.replace(/\.md$/, '');
        try {
            const raw = fs.readFileSync(path.join(teamDir, entry.name), 'utf-8');
            const { frontmatter: fm } = (0, frontmatter_1.parseMarkdown)(raw);
            members.push({
                id,
                name: fm.name ?? id,
                email: fm.email ?? '',
            });
        }
        catch (err) {
            log.error({ err, file: entry.name }, 'failed to parse team member file');
        }
    }
    return members;
}
/**
 * Ensure the author from config exists as a team member file.
 * Creates `.team/{id}.md` if it doesn't exist. The id is derived from the author name (slugified).
 */
function ensureAuthorInTeam(teamDir, author) {
    if (!author.name)
        return;
    const id = author.name.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/(^-|-$)/g, '');
    if (!id)
        return;
    const filePath = path.join(teamDir, `${id}.md`);
    if (fs.existsSync(filePath))
        return;
    try {
        fs.mkdirSync(teamDir, { recursive: true });
        const content = (0, frontmatter_1.serializeMarkdown)({ name: author.name, email: author.email }, `# ${author.name}\n`);
        fs.writeFileSync(filePath, content, 'utf-8');
    }
    catch (err) {
        log.error({ err, authorName: author.name }, 'failed to create team member file');
    }
}
