/*
 * 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==="composite"){const{add:addConstraint,remove:removeConstraint,mutate:mutateConstraint}=controlRuleDef;const removeControlMetadata=CaccHelpers.deriveConstraintMetadata(baselines,removeConstraint);return recurseThroughProps(controlRuleName,componentName,metadata,compareMetadata,removeControlMetadata,["properties"],addConstraint,removeConstraint,mutateConstraint,componentVersionDiff,cacheManager,errorMsgContext).join("\n")}return};function recurseThroughProps(controlRuleName,componentName,metadata,compareMetadata,removeControlMetadata,compareScope,addConstraint,removeConstraint,mutateConstraint,componentVersionDiff,cacheManager,errorMsgContext){let errorConditions=[];const checkProps=CaccHelpers.castToScope(metadata,compareScope);const compareProps=CaccHelpers.castToScope(compareMetadata,compareScope);if(checkProps&&compareProps){for(const[propertyName,propertyDef]of Object.entries(checkProps)){if(compareProps.hasOwnProperty(propertyName)){const currentEnums=propertyDef.enumValues;const compareEnums=compareProps[propertyName].enumValues;const printScope=CaccHelpers.getPrintScope(compareScope)+propertyName;if(currentEnums&&compareEnums){const diffs=CaccHelpers.stringArrayDiff(currentEnums,compareEnums);if(CaccHelpers.checkConstraintPatchMajor(diffs.add,addConstraint,componentVersionDiff)){errorConditions.push(`Values ${diffs.add.join(", ")} added to enumValues of ${printScope} in${errorMsgContext}'${addConstraint}'`)}const removeErrors=CaccHelpers.checkConstraintMajorWithDeprecation(diffs.remove,removeConstraint,componentVersionDiff,cacheManager,componentName,removeControlMetadata,diff=>{return compareScope.concat([propertyName])},"propertyValue",diff=>diff);if(removeErrors.length>0){errorConditions.push(`Values ${removeErrors.join(", ")} removed from enumValues of ${printScope} in${errorMsgContext}'${removeConstraint}'`)}}else if(currentEnums||compareEnums){let errorFlag=false;switch(mutateConstraint){case VC.NEVER:errorFlag=true;break;default:if(componentVersionDiff<4){errorFlag=true}break}if(errorFlag){const action=currentEnums?"added to":"removed from";errorConditions.push(`Enumerations ${action} ${printScope} in${errorMsgContext}'${mutateConstraint}'`)}}if(propertyDef.hasOwnProperty("properties")){const subPropertyScope=compareScope.concat([propertyName,"properties"]);errorConditions=errorConditions.concat(recurseThroughProps(controlRuleName,componentName,metadata,compareMetadata,removeControlMetadata,subPropertyScope,addConstraint,removeConstraint,mutateConstraint,componentVersionDiff,cacheManager,errorMsgContext))}}}}return errorConditions}module.exports=controlCallback;