/*
 * Copyright (c) 2018, 2025, Oracle and/or its affiliates.
 * Licensed under The Universal Permissive License (UPL), Version 1.0
 * as shown at https://oss.oracle.com/licenses/upl/
 */
const VC=require("./jetwco-cacc-auditConstraints");const CaccHelpers=require("./jetwco-cacc-helpers");function controlCallback(controlRuleName,controlRuleDef,semver,cacheManager,metadata,compareMetadata,componentVersionDiff,baselines,componentType,componentName,errorMsgContext,overrideTypeMappings){if(componentType==="pack"){let errorConditions=[];const{add:addConstraint,remove:removeConstraint,mutate:mutateConstraint}=controlRuleDef;const mdSemver=semver.parse(metadata.version);const compareSemver=semver.parse(compareMetadata.version);const removeControlMetadata=CaccHelpers.deriveConstraintMetadata(baselines,removeConstraint);const diffs=CaccHelpers.dependenciesDiff(metadata.dependencies,compareMetadata.dependencies);if(CaccHelpers.checkConstraintPatchMajor(diffs.add,addConstraint,componentVersionDiff)){errorConditions.push(`Component(s) ${diffs.add.map(dep=>dep.dependency).join(", ")} added to${errorMsgContext}'${addConstraint}'`)}if(diffs.mutate.length>0){const mutationErrors=[];diffs.mutate.forEach(diff=>{switch(mutateConstraint){case VC.NEVER:mutationErrors.push(`${diff.dependency}@${diff.historicalVersion} -> ${diff.version}`);break;case VC.SEMVER:case VC.PATCH:case VC.MINOR:const dependencyVersionDiff=CaccHelpers.semanticDiff(semver,diff.version,diff.historicalVersion);if(dependencyVersionDiff>componentVersionDiff){mutationErrors.push(`${diff.dependency}@${diff.historicalVersion} -> ${diff.version}`)}break;default:if(mdSemver.major>=compareSemver.major){mutationErrors.push(`${diff.dependency}@${diff.historicalVersion} -> ${diff.version}`)}break}});if(mutationErrors.length>0){errorConditions.push(`Pack children changes for ${metadata.name} in contradiction of ${controlRuleName} API change constraint '${mutateConstraint}':[${mutationErrors.join(", ")}]`)}}if(diffs.remove.length>0){const removeErrors=[];diffs.remove.forEach(diff=>{if(removeConstraint===VC.NEVER||mdSemver.major<=compareSemver.major){removeErrors.push(`${diff.dependency}@${diff.version}`)}else{const historicalDependencyVersion=removeControlMetadata.dependencies[diff.dependency];switch(removeConstraint){case VC.MAJOR_DEPRECATED:case VC.MAJOR_2_DEPRECATED:case VC.MAJOR_3_DEPRECATED:if(!CaccHelpers.isMarkedAsDeprecated(cacheManager,diff.dependency,historicalDependencyVersion)){removeErrors.push(`${diff.dependency}@${diff.version} not marked as deprecated in historical version: ${historicalDependencyVersion}`)}break}}});if(removeErrors.length>0){errorConditions.push(`Pack children removal for ${metadata.name} in contradiction of ${controlRuleName} API change constraint '${removeConstraint}':[${removeErrors.join(", ")}]`)}}if(errorConditions.length>0){return errorConditions.join("\n")}}return}module.exports=controlCallback;