/*
 * 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 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);const componentName=CaccHelpers.getComponentFullName(metadata);return recurseThroughProps(controlRuleName,componentName,metadata,compareMetadata,removeControlMetadata,["properties"],addConstraint,removeConstraint,componentVersionDiff,cacheManager,errorMsgContext,overrideTypeMappings).join("\n")}return};function recurseThroughProps(controlRuleName,componentName,metadata,compareMetadata,removeControlMetadata,compareScope,addConstraint,removeConstraint,componentVersionDiff,cacheManager,errorMsgContext,overrideTypeMappings){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 currentTypeDef=propertyDef.type;const compareTypeDef=compareProps[propertyName].type;const printScope=CaccHelpers.getPrintScope(compareScope)+propertyName;const diffs=CaccHelpers.typeDiff(currentTypeDef,compareTypeDef,overrideTypeMappings);if(CaccHelpers.checkConstraintPatchMajor(diffs.add,addConstraint,componentVersionDiff)){errorConditions.push(`Type expanded to add ${diffs.add.map(addition=>addition.type).join("|")} to ${printScope} in${errorMsgContext}'${addConstraint}'`)}const removeErrors=CaccHelpers.checkConstraintMajorWithDeprecation(diffs.remove,removeConstraint,componentVersionDiff,cacheManager,componentName,removeControlMetadata,diff=>{return compareScope.concat([propertyName])},"propertyType",diff=>diff.historicalType);if(removeErrors.length>0){errorConditions.push(`Type contracted to remove ${removeErrors.map(removal=>removal.historicalType).join("|")} from ${printScope} in${errorMsgContext}'${removeConstraint}'`)}if(diffs.mutate.length>0){errorConditions.push(`Forbidden type change of ${diffs.mutate[0].historicalType} to ${diffs.mutate[0].type} made to ${printScope} in ${componentName}`)}if(propertyDef.hasOwnProperty("properties")){const subPropertyScope=compareScope.concat([propertyName,"properties"]);errorConditions=errorConditions.concat(recurseThroughProps(controlRuleName,componentName,metadata,compareMetadata,removeControlMetadata,subPropertyScope,addConstraint,removeConstraint,componentVersionDiff,cacheManager,errorMsgContext,overrideTypeMappings))}}}}return errorConditions}module.exports=controlCallback;