/*
 * Copyright (c) 2018, 2026, 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 CaccHelpers=require("./jetwco-cacc-helpers");function controlCallback(controlRuleName,controlRuleDef,semver,cacheManager,metadata,compareMetadata,componentVersionDiff,baselines,componentType,componentName,errorMsgContext,overrideTypeMappings){if(componentType==="composite"){let errorConditions=[];const{add:addConstraint,remove:removeConstraint,mutate:mutateConstraint}=controlRuleDef;const removeControlMetadata=CaccHelpers.deriveConstraintMetadata(baselines,removeConstraint);if(metadata.hasOwnProperty("methods")&&compareMetadata.hasOwnProperty("methods")){for(const[methodName,methodDef]of Object.entries(metadata.methods)){if(compareMetadata.methods.hasOwnProperty(methodName)){const diffs=methodParamsDiff(methodDef.params,compareMetadata.methods[methodName].params);if(CaccHelpers.checkConstraintPatchMajor(diffs.add,addConstraint,componentVersionDiff)){errorConditions.push(`Method parameters ${diffs.add.join(", ")} added to ${methodName} in${errorMsgContext}'${addConstraint}'`)}if(CaccHelpers.checkConstraintMajor(diffs.mutate,mutateConstraint,componentVersionDiff)){errorConditions.push(`Method parameters mutated in a breaking fashion for ${methodName} in${errorMsgContext}'${mutateConstraint}'`)}const removeErrors=CaccHelpers.checkConstraintMajorWithDeprecation(diffs.remove,removeConstraint,componentVersionDiff,cacheManager,componentName,removeControlMetadata,diff=>{return["methods",methodName,"params",`[name:${diff}]`]});if(removeErrors.length>0){errorConditions.push(`Method parameters ${removeErrors.join(", ")} removed from ${methodName} in ${errorMsgContext}'${removeConstraint}'`)}}}}if(errorConditions.length>0){return errorConditions.join("\n")}}return};function methodParamsDiff(currentParams,historicalParams){const compare=currentParams?currentParams.map(p=>p.name):[];const historical=historicalParams?historicalParams.map(p=>p.name):[];const diffs={add:[],remove:[],mutate:[]};for(let ci=0;ci<compare.length;ci++){const checkParam=compare[ci];const historicalIndex=historical.indexOf(checkParam);if(ci!==historicalIndex){if(historicalIndex>=0){diffs.mutate.push(checkParam)}else{if(ci>historical.length-1){diffs.add.push(checkParam)}else{diffs.mutate.push(checkParam)}}}}historical.forEach(histParam=>{if(!compare.includes(histParam)){diffs.remove.push(histParam)}});return diffs}function recurseThroughProps(controlRuleName,componentName,metadata,compareMetadata,removeControlMetadata,compareScope,addConstraint,removeConstraint,componentVersionDiff,cacheManager){return errorConditions}module.exports=controlCallback;