/*
 * 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){let errorConditions=[];if(["composite","resource","vbcs-pattern"].includes(componentType)){const{add:addConstraint,remove:removeConstraint,mutate:mutateConstraint}=controlRuleDef;const diffs=CaccHelpers.dependenciesDiff(metadata.dependencies,compareMetadata.dependencies);if(CaccHelpers.checkConstraintMajor(diffs.add,addConstraint,componentVersionDiff)){errorConditions.push(`Dependencies ${diffs.add.map(dep=>dep.dependency).join(", ")} added to${errorMsgContext}'${addConstraint}'`)}if(CaccHelpers.checkConstraintMinorMajor(diffs.remove,removeConstraint,componentVersionDiff)){errorConditions.push(`Dependencies ${diffs.remove.map(dep=>dep.dependency).join(", ")} removed from${errorMsgContext}'${removeConstraint}'`)}if(diffs.mutate.length>0){diffs.mutate.forEach(mutation=>{const mutationVersionDiff=CaccHelpers.semanticDiff(semver,mutation.version,mutation.historicalVersion);switch(mutateConstraint){case VC.NEVER:errorConditions.push(`Dependency ${mutation.dependency} version change in${errorMsgContext}'${mutateConstraint}'`);break;case VC.SEMVER:case VC.PATCH:if(mutationVersionDiff>componentVersionDiff){errorConditions.push(`Dependency ${mutation.dependency}@${mutation.historicalVersion}->${mutation.version} change in${errorMsgContext}'${mutateConstraint}'`)}break;case VC.MINOR:if(componentVersionDiff<3||mutationVersionDiff>componentVersionDiff){errorConditions.push(`Dependency ${mutation.dependency}@${mutation.historicalVersion}->${mutation.version} change in${errorMsgContext}'${mutateConstraint}'`)}break;default:if(componentVersionDiff<4){errorConditions.push(`Dependency ${mutation.dependency}@${mutation.historicalVersion}->${mutation.version} change in${errorMsgContext}'${mutateConstraint}'`)}break}})}if(errorConditions.length>0){return errorConditions.join("/n")}}return};module.exports=controlCallback;