/*
 * 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){let errorConditions=[];if(componentType==="composite"){const{add:addConstraint,remove:removeConstraint,mutate:mutateConstraint}=controlRuleDef;const removeControlMetadata=CaccHelpers.deriveConstraintMetadata(baselines,removeConstraint);const mutateControlMetadata=CaccHelpers.deriveConstraintMetadata(baselines,mutateConstraint);const cleanedProperties=removeReadonlyProperties(metadata.properties);const cleanedCompareProperties=removeReadonlyProperties(compareMetadata.properties);const diffs=CaccHelpers.mapAttributesDiff(cleanedProperties,cleanedCompareProperties,"value");if(CaccHelpers.checkConstraintPatchMajor(diffs.add,addConstraint,componentVersionDiff)){errorConditions.push(`Property default value set for ${diffs.add.map(addition=>addition.name).join(", ")} in${errorMsgContext}'${addConstraint}'`)}const removeErrors=CaccHelpers.checkConstraintMinorMajorWithDeprecation(diffs.remove,removeConstraint,componentVersionDiff,cacheManager,componentName,removeControlMetadata,diff=>{return["properties",diff.name]},"propertyValue",diff=>diff.historicalValue);if(removeErrors.length>0){errorConditions.push(`Property default value removed from ${removeErrors.map(err=>err.name).join(", ")} in${errorMsgContext}'${removeConstraint}'`)}const mutateErrors=CaccHelpers.checkConstraintMinorMajorWithDeprecation(diffs.mutate,mutateConstraint,componentVersionDiff,cacheManager,componentName,removeControlMetadata,diff=>{return["properties",diff.name]},"propertyValue",diff=>diff.historicalValue);if(mutateErrors.length>0){errorConditions.push(`Property default value changed in ${mutateErrors.map(err=>err.name).join(", ")} in${errorMsgContext}'${mutateConstraint}'`)}}if(errorConditions.length>0){return errorConditions.join("\n")}return};function removeReadonlyProperties(propertyMap){const cleaned={};if(propertyMap){for(const[propName,prop]of Object.entries(propertyMap)){if(!prop.readOnly){cleaned[propName]=prop}}}return cleaned}module.exports=controlCallback;