/**
 * Unified Two-Level Mapping System (v3.6+)
 *
 * Simplifies factory mappings with a clear two-level system:
 * - Level 1: defaults (sugar syntax for common cases)
 * - Level 2: overrides (explicit with optional configuration)
 *
 * @module realize/types/unified-mappings
 * @version 3.6.0
 */
/**
 * Helper functions
 */
/**
 * 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
 */
export function isCapabilityTarget(target) {
    // Capability patterns have dots and match capability naming convention
    // storage.database, api.rest.crud, security.authentication
    return /^[a-z]+\.[a-z]+(\.[a-z]+)*$/.test(target);
}
/**
 * Determine if a target is an instance name
 *
 * @param target - Target string to check
 * @returns True if target looks like an instance name
 */
export function isInstanceTarget(target) {
    return !isCapabilityTarget(target);
}
/**
 * Resolve target type for an override
 *
 * @param override - Override mapping to analyze
 * @returns Resolved override with type information
 */
export function resolveOverrideType(override) {
    const targetType = isCapabilityTarget(override.target)
        ? 'capability'
        : 'instance';
    return {
        ...override,
        targetType,
        originalTarget: override.target
    };
}
/**
 * Expand defaults to capability mappings
 *
 * Converts sugar syntax to explicit capability patterns.
 *
 * @param defaults - Default mappings
 * @returns Map of capability patterns to factory aliases
 */
export function expandDefaults(defaults) {
    const expanded = new Map();
    // Predefined category → capability mappings
    const categoryMappings = {
        orm: ['storage.database'],
        api: ['api.rest', 'api.rest.crud'],
        cache: ['storage.cache'],
        queue: ['messaging.queue'],
        auth: ['security.authentication', 'security.authorization'],
        email: ['communication.email'],
        storage: ['storage.files', 'storage.objects'],
        search: ['storage.search'],
        logging: ['observability.logging'],
        monitoring: ['observability.monitoring', 'observability.metrics']
    };
    for (const [category, factoryAlias] of Object.entries(defaults)) {
        if (!factoryAlias)
            continue;
        const capabilities = categoryMappings[category] || [category];
        for (const capability of capabilities) {
            expanded.set(capability, factoryAlias);
        }
    }
    return expanded;
}
/**
 * Validate override references
 *
 * Checks that overrides reference declared factories.
 *
 * @param overrides - Override mappings to validate
 * @param declaredFactories - Set of declared factory names
 * @returns Validation errors (empty if valid)
 */
export function validateOverrides(overrides, declaredFactories) {
    const errors = [];
    for (const override of overrides) {
        if (!declaredFactories.has(override.factory)) {
            errors.push(`Override references undeclared factory: "${override.factory}" for target "${override.target}". ` +
                `Add it to instanceFactories section.`);
        }
        // Warn if configuration is used with capability target
        if (override.configuration && isCapabilityTarget(override.target)) {
            errors.push(`Override for capability "${override.target}" has configuration, but capabilities don't support instance config. ` +
                `Use an instance name as target instead.`);
        }
    }
    return errors;
}
//# sourceMappingURL=unified-mappings.js.map