/*
 * 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 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==="composite"){let errorConditions=[];const{add:addConstraint,remove:removeConstraint,mutate:mutateConstraint}=controlRuleDef;const removeControlMetadata=CaccHelpers.deriveConstraintMetadata(baselines,removeConstraint);if(metadata.hasOwnProperty("slots")&&compareMetadata.hasOwnProperty("slots")){for(const[slotName,slotDef]of Object.entries(metadata.slots)){if(compareMetadata.slots.hasOwnProperty(slotName)){const compareSlotDef=compareMetadata.slots[slotName];if(slotDef.hasOwnProperty("data")===compareSlotDef.hasOwnProperty("data")){if(slotDef.hasOwnProperty("data")){const diffs=CaccHelpers.apiDiff(slotDef.data,compareSlotDef.data);if(CaccHelpers.checkConstraintPatchMajor(diffs.add,addConstraint,componentVersionDiff)){errorConditions.push(`Slot data attribute amended to include ${diffs.add.map(addition=>addition.name).join(", ")} on ${slotName} in${errorMsgContext}'${addConstraint}'`)}const removeErrors=CaccHelpers.checkConstraintMajorWithDeprecation(diffs.remove,removeConstraint,componentVersionDiff,cacheManager,componentName,removeControlMetadata,diff=>{return["slots",slotName,"data",diff.name]});if(removeErrors.length>0){errorConditions.push(`Attributes ${removeErrors.map(removal=>removal.name).join(", ")} removed from data attribute of slot ${slotName} in${errorMsgContext}'${removeConstraint}'`)}}}else{let errorFlag=false;switch(mutateConstraint){case VC.NEVER:errorFlag=true;break;default:if(componentVersionDiff<4){errorFlag=true}break}if(errorFlag){const errorAction=slotDef.hasOwnProperty("data")?"added":"removed";errorConditions.push(`Slot data attribute ${errorAction} for ${slotName} in${errorMsgContext}'${mutateConstraint}'`)}}}}}if(errorConditions.length>0){return errorConditions.join("\n")}}return};module.exports=controlCallback;