/*
 * 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");const NULL_VERSION="0.0.0";function controlCallback(controlRuleName,controlRuleDef,semver,cacheManager,metadata,compareMetadata,componentVersionDiff,baselines,componentType,componentName,errorMsgContext,overrideTypeMappings){let errorCondition;const{add:addConstraint,remove:removeConstraint,mutate:mutateConstraint}=controlRuleDef;const addControlMetadata=CaccHelpers.deriveConstraintMetadata(baselines,addConstraint);const removeControlMetadata=CaccHelpers.deriveConstraintMetadata(baselines,removeConstraint);const mutateControlMetadata=CaccHelpers.deriveConstraintMetadata(baselines,mutateConstraint);const newJetVersionRange=metadata.jetVersion?metadata.jetVersion:NULL_VERSION;const compareJetVersionRange=compareMetadata.jetVersion?compareMetadata.jetVersion:NULL_VERSION;const mutateControlVersion=mutateControlMetadata?mutateControlMetadata.version:NULL_VERSION;if(mutateConstraint===VC.NEVER||compareMetadata.version!==mutateControlVersion){if((newJetVersionRange===NULL_VERSION||compareJetVersionRange===NULL_VERSION)&&newJetVersionRange!==compareJetVersionRange){errorCondition=`JET version range attribute (jetVersion) has been ${newJetVersionRange===NULL_VERSION?"removed from":"added to"} metadata in contradiction of ${controlRuleName} API change constraint of '${mutateConstraint}'`}}if(!errorCondition){const newMin=semver.minVersion(newJetVersionRange);const compareMin=semver.minVersion(compareJetVersionRange);if(newMin.version!==compareMin.version){if(removeConstraint===VC.NEVER||newMin>compareMin&&compareMetadata.version!==removeControlMetadata.version){errorCondition=`Lower bound of JET version range attribute (jetVersion) has been changed in contradiction of ${controlRuleName} API change constraint of '${removeConstraint}'`}}}if(!errorCondition){const newMaxMajor=CaccHelpers.incrementMajorToMaxSupported(semver,semver.minVersion(newJetVersionRange),newJetVersionRange);const compareMaxMajor=CaccHelpers.incrementMajorToMaxSupported(semver,semver.minVersion(compareJetVersionRange),compareJetVersionRange);if(newMaxMajor.major!==compareMaxMajor.major){if(addConstraint===VC.NEVER||compareMetadata.version!==addControlMetadata.version){errorCondition=`Upper bound of JET version range attribute (jetVersion) has been changed in contradiction of ${controlRuleName} API change constraint of '${addConstraint}'`}}}return errorCondition}module.exports=controlCallback;