/*
 * 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 helperPos=require("./helpers/json-propname-pos");const RULE_NAME="oj-cca-compjson-bp-jetver";const SHORT="Best practices for component.json property 'jetVersion'";const LONG="Best Practices for component.json metadata:<br>"+"The 'jetVersion' semver range should be capped. That is, not an open-ended 'jetVersion' "+"property - e.g.  '>=7.20'<br>"+"The 'jetVersion' should generally represent some degree of range and not be "+"hard-coded to just one version of JET - e.g. '7.2.0'.<br>"+"The 'jetVersion' semver range in the component metadata should match/include the JAF configuration 'jetVer'.<br>"+"It is recommended that custom component owners re-certify their components at MAJOR version boundaries of JET, "+"and update their metadata accordingly.";function Rule(){this._jetvers=null}Rule.prototype.getName=function(){return RULE_NAME};Rule.prototype.getShortDescription=function(){return SHORT};Rule.prototype.getDescription=function(){return LONG};Rule.prototype.register=function(regCtx){if(regCtx.rulePack.isRuleEnabled("jetwc-jet-version","JETWC")){return false}else return{"compjson-composite":this._handle,"compjson-pack":this._handle}};Rule.prototype._handle=function(ruleCtx,obj){var jetver,o,x,actual,keys,keysLC=[],bad;o=obj&&obj.obj;if(!o){return}keys=Object.keys(o);keys.forEach(function(s){keysLC.push(s.toLowerCase())});x=keysLC.indexOf("jetversion");if(x>=0){jetver=o[keys[x]];actual=keys[x]}if(!jetver){if(ruleCtx.type!=="compjson-pack"){let issue=new ruleCtx.Issue("'jetVersion' property is missing");issue.setSeverity("blocker");issue.setPosition(0,0,0,0);ruleCtx.reporter.addIssue(issue,ruleCtx)}return}if(typeof jetver!=="string"){_emitIssue(ruleCtx,`'jetVersion' property '${jetver}' is not a valid range or semver`,"blocker",actual);return}bad=typeof jetver!=="string";if(bad||!ruleCtx.utils.semVerUtils.isValidRange(jetver)){_emitIssue(ruleCtx,`'jetVersion' property '${jetver}' is not a valid range or semver`,"blocker",actual);return}if(!_isPossibleRange(jetver)){_emitIssue(ruleCtx,`'jetVersion' semver property '${jetver}' should not be absolute`,"minor",actual);return}if(_isOpenEndedRange(jetver)){_emitIssue(ruleCtx,`'jetVersion' semver property '${jetver}' should not be open-ended - best practices`,null,actual);return}if(!this._isMatchVersions(ruleCtx,jetver)){_emitIssue(ruleCtx,`'jetVersion' semver property '${jetver}' does not match any supported JET versions`,null,actual);return}if(ruleCtx.utils.semVerUtils.match(jetver)){return true}{_emitIssue(ruleCtx,`'jetVersion' semver property '${jetver}' does not match audit run JET version '${ruleCtx.utils.semVerUtils.getReference()}'`,null,actual);return}};function _emitIssue(ruleCtx,msg,sev,actual){var issue=new ruleCtx.Issue(msg);if(sev){issue.setSeverity(sev)}if(actual){let pos=helperPos(ruleCtx,actual);issue.setPosition(pos.row,pos.col,pos.index,pos.index?pos.index+9:0)}ruleCtx.reporter.addIssue(issue,ruleCtx)};function _isPossibleRange(ver){const initRange=["^",">","<","=","~"];const intraRange=["-",">","<"];var s,ret=false;s=ver.charAt(0);if(initRange.includes(s)||intraRange.includes(s)||ver.indexOf("-")>3){ret=true}return ret};function _isOpenEndedRange(ver){var ret=false;const openChars=[">"];if(openChars.includes(ver.charAt(0))){if(ver.indexOf("-")<0&&ver.indexOf("<")<0){ret=true}}return ret};Rule.prototype._isMatchVersions=function(ruleCtx,jetver){var ver;if(!this._jetvers){this._jetvers=ruleCtx.utils.metaLib.getMetaVers();this._jetvers=this._jetvers.versions}for(ver in this._jetvers){if(ruleCtx.utils.semVerUtils.satisfiesRange(ver,jetver)){return true}}return false};module.exports=Rule;