/*
 * 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 Utils=require("../jetwc-lib/jetwcutils-componentAuditUtils");const TsxUtils=require("../jetwc-lib/jetwcutils-tsxUtils");const NetUtils=require("../../lib/NetUtils");const CommonImpl=require("../jetwc-lib/jetwcutils-commonListenerImpls");const ruleDef=function(){};ruleDef.prototype.getName=function(){return"jetwco-exchange-version-check"};ruleDef.prototype.getShortDescription=function(){return"Verify version is not already in Exchange"};ruleDef.prototype.getDescription=function(){return"This audit checks that the version specified for this component does not already exist in Exchange.  This may not actually be an error, hence this informational only."};ruleDef.prototype.register=function(context){return{startaudit:this._checkForExchangeConfiguration,compjson:this._doAudit,Program:this._doVComponentAudit}};ruleDef.prototype._checkForExchangeConfiguration=function(context){this.exchangeURL=CommonImpl.checkForExchangeConfiguration(context);if(!this.exchangeURL){const issue=new context.Issue(`Invalid or Missing exchangeURL value specified for the rule ${context.ruleName}, rule execution is disabled`);issue.setMsgKey(`${context.ruleName}_3`);issue.setSeverity("critical");context.reporter.addIssue(issue,context)}};ruleDef.prototype._doAudit=function(context){if(context.suppData.obj){const componentDef=context.suppData.obj;const fullName=Utils.getComponentFullName(componentDef);this._checkForExchangeVersion(context,fullName,componentDef.version)}};ruleDef.prototype._doVComponentAudit=function(context){const componentInfo=TsxUtils.matchToVComponentImpl(context,context.filepath);if(componentInfo){this._checkForExchangeVersion(context,componentInfo.fullName,componentInfo.version)}};ruleDef.prototype._checkForExchangeVersion=function(context,fullName,newVersion){const semver=context.utils.semVerUtils;const msg=context.utils.msgLib;try{const netUtils=new NetUtils;const existingVersionsResp=netUtils.loadUrl(`${this.exchangeURL}components/${fullName}/versions`);const responseCode=existingVersionsResp.statusCode;if(responseCode===200&&existingVersionsResp.body){const existingVersions=JSON.parse(existingVersionsResp.body);if(existingVersions.items&&existingVersions.items.length>0){let latestMatchVersion,nextProjectedVersion;const versions=existingVersions.items.reverse();for(let i=0;i<versions.length;i++){if(semver.gte(newVersion,versions[i])){latestMatchVersion=versions[i];if(i>0){nextProjectedVersion=versions[i-1]}break}}if(latestMatchVersion){if(latestMatchVersion===newVersion){const sameissue=new context.Issue(`The version number defined for ${fullName} (${newVersion}) already exists in Exchange`);sameissue.setMsgKey("jetwco-exchange-version-check_1");context.reporter.addIssue(sameissue,context)}else if(nextProjectedVersion){const futureissue=new context.Issue(`The version number defined for ${fullName} (${newVersion}) is lower than existing versions in Exchange`);futureissue.setMsgKey("jetwco-exchange-version-check_2");context.reporter.addIssue(futureissue,context)}}}}else if(responseCode===404||existingVersionsResp.eCode==="ENOTFOUND"){msg.info(`The component ${fullName} does not exist in the Exchange and so cannot be API verified against older versions. It will be skipped`)}}catch(e){msg.error(`Unexpected Error in jetwco-exchange-version-check for ${fullName}: ${JSON.stringify(e)}`)};};module.exports=ruleDef;