/*
 * 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 ruleDef=function(){};ruleDef.prototype.getName=function(){return"jetwco-thirdparty-ba"};ruleDef.prototype.getShortDescription=function(){return"Validate Business Approvals"};ruleDef.prototype.getDescription=function(){return"This rule is applied to reference component and any components that depend on reference components to ensure that the PLS Business Approval number is recorded"};ruleDef.prototype.register=function(context){return{compjson:this._doAudit,Program:this._doVComponentAudit}};ruleDef.prototype._doAudit=function(context){if(context.suppData.obj){const meta=context.suppData.obj;const componentType=Utils.getComponentType(meta);this._checkBA(context,componentType,meta.dependencies,meta.extension)}};ruleDef.prototype._doVComponentAudit=function(context){const componentInfo=TsxUtils.matchToVComponentImpl(context,context.filepath);if(componentInfo){let remappedDependencies;if(componentInfo.dependencies){remappedDependencies=componentInfo.dependencies.reduce((asMap,entry)=>{asMap[entry.name]=entry.range;return asMap},{})}this._checkBA(context,componentInfo.type,remappedDependencies,componentInfo.extension)}};ruleDef.prototype._checkBA=function(context,componentType,dependenciesMetadata,extensionMetadata){const referenceComponents=context.rulePack.getExtension().allReferenceComponents;if(componentType==="reference"){if(!(extensionMetadata&&extensionMetadata.oracle&&extensionMetadata.oracle.businessApprovals)){issue=new context.Issue(`Reference component is missing PLS Business Approval Information`);issue.setMsgKey("jetwco-thirdparty-ba_4");context.reporter.addIssue(issue,context)}}if(referenceComponents){let issue;if(dependenciesMetadata){Object.keys(dependenciesMetadata).forEach(dependency=>{if(referenceComponents.has(dependency)){const referencedLibs=referenceComponents.get(dependency);if(referencedLibs&&Object.keys(referencedLibs).length>0){if(!(extensionMetadata&&extensionMetadata.oracle&&extensionMetadata.oracle.businessApprovals)){issue=new context.Issue(`Component is missing PLS Business Approval Information relating to the reference '${dependency}'`);issue.setMsgKey("jetwco-thirdparty-ba_1");context.reporter.addIssue(issue,context)}else{Object.keys(referencedLibs).forEach(lib=>{if(!extensionMetadata.oracle.businessApprovals.hasOwnProperty(lib)){issue=new context.Issue(`Component is missing PLS Business Approval Information relating to '${lib}' from '${dependency}'`);issue.setMsgKey("jetwco-thirdparty-ba_2");context.reporter.addIssue(issue,context)}else{if(extensionMetadata.oracle.businessApprovals[lib]!==referencedLibs[lib]){issue=new context.Issue(`Component PLS Business Approval Information relating to '${lib}' from '${dependency}' does not match`);issue.setMsgKey("jetwco-thirdparty-ba_2");context.reporter.addIssue(issue,context)}}})}}else{issue=new context.Issue(`Reference component dependency '${dependency}' does not have supplied PLS Business Approval Information`);issue.setMsgKey("jetwco-thirdparty-ba_3");context.reporter.addIssue(issue,context)}}})}}};module.exports=ruleDef;