"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolveEffectivePolicy = resolveEffectivePolicy;
function isValidationSeverity(value) {
    return value === 'error' || value === 'warning' || value === 'info';
}
function deepMergeRecord(base, override) {
    const merged = {};
    const keys = [...new Set([...Object.keys(base), ...Object.keys(override)])].sort((left, right) => left.localeCompare(right));
    for (const key of keys) {
        const baseValue = base[key];
        const overrideValue = override[key];
        if (baseValue &&
            overrideValue &&
            typeof baseValue === 'object' &&
            typeof overrideValue === 'object' &&
            !Array.isArray(baseValue) &&
            !Array.isArray(overrideValue)) {
            merged[key] = deepMergeRecord(baseValue, overrideValue);
            continue;
        }
        if (overrideValue !== undefined) {
            merged[key] = overrideValue;
            continue;
        }
        merged[key] = baseValue;
    }
    return merged;
}
function normalizeDependencyRulesConfig(value) {
    const modulesRaw = value.modules;
    if (!modulesRaw || typeof modulesRaw !== 'object' || Array.isArray(modulesRaw)) {
        return undefined;
    }
    const modules = Object.entries(modulesRaw)
        .sort(([left], [right]) => left.localeCompare(right))
        .reduce((accumulator, [moduleName, moduleRules]) => {
        if (!moduleRules || typeof moduleRules !== 'object' || Array.isArray(moduleRules)) {
            return accumulator;
        }
        const typed = moduleRules;
        const allowedDependencies = Array.isArray(typed.allowedDependencies)
            ? typed.allowedDependencies
                .filter((item) => typeof item === 'string')
                .sort((left, right) => left.localeCompare(right))
            : undefined;
        const publicEntrypointsOnly = typeof typed.publicEntrypointsOnly === 'boolean'
            ? typed.publicEntrypointsOnly
            : undefined;
        accumulator[moduleName] = {
            ...(allowedDependencies ? { allowedDependencies } : {}),
            ...(publicEntrypointsOnly !== undefined
                ? { publicEntrypointsOnly }
                : {}),
        };
        return accumulator;
    }, {});
    return { modules };
}
function resolveEffectivePolicy(input) {
    const knownRuleIdSet = new Set(input.knownRuleIds);
    const localSeverities = {
        ...(input.localValidationConfig.severityOverrides ?? {}),
    };
    const remoteRulesApplied = Object.entries(input.remotePolicy?.rules ?? {})
        .filter(([ruleId, severity]) => knownRuleIdSet.has(ruleId) && isValidationSeverity(severity))
        .sort(([left], [right]) => left.localeCompare(right))
        .reduce((accumulator, [ruleId, severity]) => {
        accumulator[ruleId] = severity;
        return accumulator;
    }, {});
    const mergedSeverities = {
        ...localSeverities,
        ...remoteRulesApplied,
    };
    const mergedDependencyRulesRaw = deepMergeRecord(input.localDependencyRulesRaw ?? {}, input.remotePolicy?.dependencyRules ?? {});
    return {
        effectiveValidationConfig: {
            ...(input.localValidationConfig.disabledRules
                ? { disabledRules: input.localValidationConfig.disabledRules }
                : {}),
            ...(Object.keys(mergedSeverities).length > 0
                ? { severityOverrides: mergedSeverities }
                : {}),
        },
        effectiveDependencyRulesRaw: mergedDependencyRulesRaw,
        effectiveDependencyRulesConfig: normalizeDependencyRulesConfig(mergedDependencyRulesRaw),
        remoteRulesApplied,
    };
}
//# sourceMappingURL=effectivePolicyResolver.js.map