/**
 * Plugin loader: resolves, imports, and validates OpenWriter plugins.
 */
function resolvePluginConfig(plugin, globalConfig) {
    const resolved = {};
    if (!plugin.configSchema)
        return resolved;
    for (const [key, field] of Object.entries(plugin.configSchema)) {
        // Priority: globalConfig (CLI flags) → env var → empty
        const envVal = field.env ? process.env[field.env] : undefined;
        const value = globalConfig[key] || envVal || '';
        if (value)
            resolved[key] = value;
    }
    return resolved;
}
export async function loadPlugins(names, globalConfig) {
    const plugins = [];
    const errors = [];
    for (const name of names) {
        try {
            const mod = await import(name);
            const plugin = mod.default || mod.plugin || mod;
            if (!plugin.name || !plugin.version) {
                errors.push(`Plugin "${name}" missing name or version`);
                continue;
            }
            const config = resolvePluginConfig(plugin, globalConfig);
            plugins.push({ plugin, config });
        }
        catch (err) {
            errors.push(`Failed to load plugin "${name}": ${err.message}`);
        }
    }
    return { plugins, errors };
}
