// Copyright (c) 2026 Modelware. All rights reserved.
import { parse } from 'yaml';
export function parseTemplateComposeDirective(source) {
    const normalized = source.replace(/^\uFEFF/, '');
    const parsed = parse(normalized);
    if (!isRecord(parsed)) {
        return undefined;
    }
    const id = normalizeString(parsed.template);
    if (!id) {
        return undefined;
    }
    const args = parseArgs(parsed, ['template']);
    return { id, args };
}
function parseArgs(value, excludedKeys = []) {
    if (!isRecord(value)) {
        return {};
    }
    const excluded = new Set(excludedKeys);
    const args = {};
    for (const [key, raw] of Object.entries(value)) {
        const normalizedKey = normalizeString(key);
        if (!normalizedKey || excluded.has(normalizedKey)) {
            continue;
        }
        const converted = toTemplateValue(raw);
        if (converted !== undefined) {
            args[normalizedKey] = converted;
        }
    }
    return args;
}
function toTemplateValue(value) {
    if (value === null) {
        return null;
    }
    if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
        return value;
    }
    if (Array.isArray(value)) {
        return value.every((entry) => typeof entry === 'string') ? value : undefined;
    }
    if (isRecord(value)) {
        return value;
    }
    return undefined;
}
function normalizeString(value) {
    if (typeof value !== 'string') {
        return undefined;
    }
    const normalized = value.trim();
    return normalized.length > 0 ? normalized : undefined;
}
function isRecord(value) {
    return typeof value === 'object' && value !== null && !Array.isArray(value);
}
//# sourceMappingURL=compose.js.map