/*
 * 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 path=require("path");const NetUtils=require("../../lib/NetUtils");const AuditComponentCacheManager=function(context,ruleName,exchangeURL){this.msg=context.utils.msgLib;this.cacheFolder=`jaftmp@-${context.rulePack.getPrefix()}`;this.fsUtils=context.utils.fsUtils;this.exchangeURL=exchangeURL;this.ruleName=ruleName;this._ensureLocalCache();this.baselineOpts=context.rulePack.getRuleCustomOption("baseline",ruleName);if(this.baselineOpts&&Object.entries(this.baselineOpts).length>0){this.baselineIndexFileName=path.join(this.cacheFolder,`${this._getObjectHashSignature(this.baselineOpts)}.json`)}else{this.baselineIndexFileName=null}};AuditComponentCacheManager.prototype.isBaselineIndexAvailable=function(){return this.baselineIndexFileName!==null};AuditComponentCacheManager.prototype.getBaselineOpts=function(){return this.baselineOpts};AuditComponentCacheManager.prototype.readComponentCacheIndexFile=function(){if(this.baselineIndexFileName){if(this.fsUtils.fileExistsSync(this.baselineIndexFileName)){return this.fsUtils.readJsonSync(this.baselineIndexFileName)}else{return{}}}else{return}};AuditComponentCacheManager.prototype.writeComponentCacheIndexFile=function(index){if(this.baselineIndexFileName){this.fsUtils.writeJsonSync(index,this.baselineIndexFileName)}};AuditComponentCacheManager.prototype.removeLocalCache=function(){if(this.fsUtils.pathExistsSync(this.cacheFolder)){this.fsUtils.deleteFolderSync(this.cacheFolder,true)}};AuditComponentCacheManager.prototype.generateComponentCachePath=function(component,componentVersion){const mungedVersion=componentVersion.replace(/\./g,"-");return path.join(this.cacheFolder,`${component}@${mungedVersion}-component.json`)};AuditComponentCacheManager.prototype.existsInComponentCache=function(component,componentVersion){const cacheFile=this.generateComponentCachePath(component,componentVersion);return this.fsUtils.fileExistsSync(cacheFile)};AuditComponentCacheManager.prototype.writeToComponentCache=function(component,componentVersion,metadata){if(metadata){const cacheFile=this.generateComponentCachePath(component,componentVersion);this.fsUtils.writeJsonSync(metadata,cacheFile)}};AuditComponentCacheManager.prototype.readFromComponentCache=function(component,componentVersion){const cacheFile=this.generateComponentCachePath(component,componentVersion);return this.fsUtils.readJsonSync(cacheFile)};AuditComponentCacheManager.prototype.getComponentMetadata=function(component,componentVersion){if(this.existsInComponentCache(component,componentVersion)){return this.readFromComponentCache(component,componentVersion)}else{try{const netUtils=new NetUtils;const getResp=netUtils.loadUrl(`${this.exchangeURL}components/${component}/versions/${componentVersion}`);if(getResp.statusCode=200&&getResp.body){const cachedDef=JSON.parse(getResp.body).component;this.writeToComponentCache(component,componentVersion,cachedDef);return cachedDef}else{this.msg.error(`The exchange component ${component}@${componentVersion} needed by the rule ${this.ruleName} cannot be retrieved, ensure it is public`)}}catch(e){this.msg.error(`Unable to read component ${component}@${componentVersion} for rule ${this.ruleName}:\n${JSON.stringify(e)}`)};}return};AuditComponentCacheManager.prototype._ensureLocalCache=function(){if(!this.fsUtils.pathExistsSync(this.cacheFolder)){this.fsUtils.createFolderSync(this.cacheFolder)}};AuditComponentCacheManager.prototype._getObjectHashSignature=function(sourceObject){const sourceAsStringArray=Array.from(JSON.stringify(sourceObject));return sourceAsStringArray.reduce((hash,c)=>Math.imul(31,hash)+c.charCodeAt(0)|0,0)};module.exports=AuditComponentCacheManager;