/**
 * Manifest Adapter
 *
 * Bridges between ManifestConfig (realize format) and SpecVerseManifest (validation format).
 * Converts the unified manifest format to the structure expected by validators.
 *
 * @module registry/utils/manifest-adapter
 * @version 1.0.0
 */
import { getEffectiveMappings } from '../../realize/index.js';
/**
 * Convert ManifestConfig to SpecVerseManifest
 *
 * Extracts instance factories and capability mappings from the unified format.
 * Supports both v3.6 (defaults/overrides) and v3.3 (capabilityMappings/instanceMappings).
 *
 * @param manifestConfig - Full manifest configuration
 * @returns SpecVerseManifest suitable for validation
 */
export function manifestConfigToSpecVerseManifest(manifestConfig) {
    // Extract instance factories if present
    const instanceFactories = [];
    if (manifestConfig.instanceFactories) {
        for (const factory of manifestConfig.instanceFactories) {
            // Convert source metadata to string format for validation
            const sourceString = getSourceString(factory);
            instanceFactories.push({
                name: factory.name,
                source: sourceString
            });
        }
    }
    // Get effective mappings (auto-migrates v3.3 to v3.6 if needed)
    const { defaults, overrides, system } = getEffectiveMappings(manifestConfig);
    // Build capability mappings from effective mappings
    const capabilityMappings = [];
    // If using v3.6 format, convert overrides with capability targets
    if (system === 'v3.6' && overrides) {
        for (const override of overrides) {
            // Only include capability targets (not instance names)
            if (isCapabilityTarget(override.target)) {
                capabilityMappings.push({
                    capability: override.target,
                    instanceFactory: override.factory
                });
            }
        }
    }
    // If using v3.3 format, use original legacy mappings (not migrated)
    if (system === 'v3.3' && manifestConfig.capabilityMappings) {
        capabilityMappings.push(...manifestConfig.capabilityMappings.map(mapping => ({
            capability: mapping.capability,
            instanceFactory: mapping.instanceFactory
        })));
    }
    return {
        specVersion: manifestConfig.specVersion,
        instanceFactories,
        capabilityMappings
    };
}
/**
 * Determine if a target is a capability pattern
 *
 * Capability patterns contain dots (e.g., "storage.database")
 * Instance names typically don't (e.g., "primary-db")
 *
 * @param target - Target string to check
 * @returns True if target looks like a capability
 */
function isCapabilityTarget(target) {
    return /^[a-z]+\.[a-z]+(\.[a-z]+)*$/.test(target);
}
/**
 * Get source string from factory declaration
 *
 * Converts structured source metadata to a string representation.
 *
 * @param factory - Instance factory declaration
 * @returns Source string (e.g., "@specverse/factories-prisma")
 */
function getSourceString(factory) {
    const { source } = factory;
    switch (source.type) {
        case 'npm':
            return source.package || '';
        case 'git':
            return source.url || '';
        case 'url':
            return source.url || '';
        default:
            return '';
    }
}
/**
 * Validate that capability mappings reference declared factories
 *
 * Supports both v3.6 (defaults/overrides) and v3.3 (capabilityMappings/instanceMappings).
 *
 * @param manifest - Manifest configuration to validate
 * @returns Validation errors (empty if valid)
 */
export function validateFactoryReferences(manifest) {
    const errors = [];
    if (!manifest.instanceFactories || manifest.instanceFactories.length === 0) {
        // If no instance factories declared, skip validation
        // (backward compatibility with old manifests)
        return errors;
    }
    // Build map of declared factory names
    const declaredFactories = new Set(manifest.instanceFactories.map(f => f.name));
    // Check v3.6 overrides
    if (manifest.overrides) {
        for (const override of manifest.overrides) {
            if (!declaredFactories.has(override.factory)) {
                errors.push(`Override references undeclared factory: "${override.factory}" for target "${override.target}". ` +
                    `Add it to instanceFactories section.`);
            }
        }
    }
    // Check v3.3 capability mappings
    if (manifest.capabilityMappings) {
        for (const mapping of manifest.capabilityMappings) {
            if (!declaredFactories.has(mapping.instanceFactory)) {
                errors.push(`Capability mapping references undeclared factory: "${mapping.instanceFactory}". ` +
                    `Add it to instanceFactories section.`);
            }
        }
    }
    // Check v3.3 instance mappings
    if (manifest.instanceMappings) {
        for (const mapping of manifest.instanceMappings) {
            const factoryName = mapping.instanceFactory;
            if (factoryName && !declaredFactories.has(factoryName)) {
                errors.push(`Instance mapping references undeclared factory: "${factoryName}". ` +
                    `Add it to instanceFactories section.`);
            }
        }
    }
    return errors;
}
/**
 * Get all factory names from manifest
 *
 * Extracts factory names from instanceFactories, overrides, capabilityMappings, and instanceMappings.
 * Supports both v3.6 and v3.3 formats.
 * Useful for determining what needs to be declared.
 *
 * @param manifest - Manifest configuration
 * @returns Set of factory names
 */
export function getReferencedFactoryNames(manifest) {
    const names = new Set();
    // From instance factories
    if (manifest.instanceFactories) {
        for (const factory of manifest.instanceFactories) {
            names.add(factory.name);
        }
    }
    // From v3.6 overrides
    if (manifest.overrides) {
        for (const override of manifest.overrides) {
            names.add(override.factory);
        }
    }
    // From v3.3 capability mappings
    if (manifest.capabilityMappings) {
        for (const mapping of manifest.capabilityMappings) {
            names.add(mapping.instanceFactory);
        }
    }
    // From v3.3 instance mappings
    if (manifest.instanceMappings) {
        for (const mapping of manifest.instanceMappings) {
            if (mapping.instanceFactory) {
                names.add(mapping.instanceFactory);
            }
        }
    }
    return names;
}
//# sourceMappingURL=manifest-adapter.js.map