"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseTag = parseTag;
exports.parseTags = parseTags;
exports.getTagValue = getTagValue;
const LEGACY_TAG_ALIASES = {
    audio: 'sfx',
    audioloop: 'music',
    separator: 'section',
};
function normalizeKey(key) {
    const normalized = key.trim().toLowerCase().replace(/[^a-z0-9_-]+/g, '-');
    return LEGACY_TAG_ALIASES[normalized] || normalized;
}
function parseTag(raw) {
    const text = String(raw || '').trim().replace(/^#\s*/, '');
    if (!text)
        return null;
    const bracketMatch = text.match(/^([A-Za-z][\w-]*)(?:\[([^\]]*)\])?(?:\(([^)]*)\))?$/);
    if (bracketMatch) {
        const tag = { key: normalizeKey(bracketMatch[1]) };
        if (typeof bracketMatch[2] !== 'undefined')
            tag.value = bracketMatch[2].trim();
        if (typeof bracketMatch[3] !== 'undefined')
            tag.param = bracketMatch[3].trim();
        return tag;
    }
    const colonMatch = text.match(/^([A-Za-z][\w-]*)\s*:\s*(.*?)\s*(?:\(([^)]*)\))?$/);
    if (colonMatch) {
        const tag = { key: normalizeKey(colonMatch[1]) };
        tag.value = colonMatch[2].trim();
        if (typeof colonMatch[3] !== 'undefined')
            tag.param = colonMatch[3].trim();
        return tag;
    }
    const bareMatch = text.match(/^[A-Za-z][\w-]*$/);
    if (bareMatch) {
        return { key: normalizeKey(text) };
    }
    return null;
}
function parseTags(rawTags) {
    if (!Array.isArray(rawTags))
        return [];
    return rawTags
        .map((raw) => parseTag(String(raw ?? '')))
        .filter((tag) => Boolean(tag));
}
function getTagValue(tags, key) {
    const normalizedKey = normalizeKey(key);
    return tags.find((tag) => tag.key === normalizedKey)?.value;
}
//# sourceMappingURL=tag-parser.js.map