"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.readSkillMetadataFromMarkdown = readSkillMetadataFromMarkdown;
const yaml_1 = require("yaml");
const frontmatter_1 = require("./frontmatter");
function readSkillMetadataFromMarkdown(text) {
    const frontmatter = (0, frontmatter_1.parseFrontmatterBlock)(text, {
        closingDelimiter: "trimmed",
    });
    if (frontmatter.kind === "not-frontmatter" ||
        frontmatter.kind === "missing-closing-delimiter") {
        return null;
    }
    let parsed;
    try {
        const document = (0, yaml_1.parseDocument)(frontmatter.lines.join("\n"), {
            strict: false,
        });
        parsed = document.toJSON();
    }
    catch {
        return null;
    }
    if (!isParsedSkillFrontmatter(parsed)) {
        return null;
    }
    return {
        name: normalizeSkillMetadataName(parsed.name),
        description: normalizeSkillMetadataDescription(parsed.description),
    };
}
function isParsedSkillFrontmatter(value) {
    return typeof value === "object" && value !== null && !Array.isArray(value);
}
function normalizeSkillMetadataName(value) {
    return normalizeSkillMetadataString(value);
}
function normalizeSkillMetadataDescription(value) {
    const normalized = normalizeSkillMetadataString(value);
    return normalized === null ? null : normalized.replace(/\s+/gu, " ");
}
function normalizeSkillMetadataString(value) {
    if (typeof value !== "string") {
        return null;
    }
    const trimmed = value.trim();
    return trimmed.length === 0 ? null : trimmed;
}
