/*
 * 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 Utils=require("../jetwc-lib/jetwcutils-componentAuditUtils");const TsxUtils=require("../jetwc-lib/jetwcutils-tsxUtils");const ruleDef=function(){};ruleDef.prototype.getName=function(){return"jetwco-license-info"};ruleDef.prototype.getShortDescription=function(){return"Validate License Information"};ruleDef.prototype.getDescription=function(){return"This rule is checks that a license file (LICENSE.txt) or URL is supplied, and third party license (THIRD_PARTY_LICENSE.txt) file is present if required"};ruleDef.prototype.register=function(context){return{compjson:this._doAudit,Program:this._doVComponentAudit}};ruleDef.prototype._doAudit=function(context){const componentMetadata=context.suppData.obj;this._checkLicense(context,componentMetadata)};ruleDef.prototype._doVComponentAudit=function(context){const componentInfo=TsxUtils.matchToVComponentImpl(context,context.filepath);this._checkLicense(context,componentInfo)};ruleDef.prototype._checkLicense=function(context,componentMetadata){const msg=context.utils.msgLib;const rulePackState=context.rulePack.getExtension();if(componentMetadata){let issue;let licenseFileFound=false;let isInMonoPack=Utils.isMonoPackMember(context,componentMetadata);;let licenseURL;let licenseCandidate="LICENSE.txt";const compRootFiles=context.utils.fsUtils.readDir(path.dirname(context.filepath));for(let i=0;i<compRootFiles.length;i++){const rootEntry=compRootFiles[i];if(rootEntry.isFile&&rootEntry.name===licenseCandidate){licenseFileFound=true;break}}if(componentMetadata.hasOwnProperty("license")&&typeof componentMetadata.license==="string"){licenseURL=componentMetadata.license}if(!isInMonoPack&&!licenseFileFound&&!licenseURL){issue=new context.Issue(`Component is missing license information. There should either be a 'LICENSE.txt' file (case sensitive) in the root, or a license attribute in the component metadata.`);issue.setMsgKey(context.ruleName+"_1");context.reporter.addIssue(issue,context)}if(licenseURL){const customOpts=context.rulePack.getRuleCustomOptions();const allowedLicenses=customOpts.allowedLicenseURLs;if(Array.isArray(allowedLicenses)&&allowedLicenses.length>0){if(!allowedLicenses.includes(licenseURL)){issue=new context.Issue(`Component license metadata value does not specified one of the allowed values: ${allowedLicenses.join(", ")}`);issue.setMsgKey(context.ruleName+"_3");context.reporter.addIssue(issue,context)}}}let thirdPartyScan=false;if(componentMetadata.type&&componentMetadata.type==="reference"){thirdPartyScan=true}else{if(componentMetadata.dependencies){if(rulePackState.allReferenceComponents){Object.keys(componentMetadata.dependencies).forEach(dependency=>{if(rulePackState.allReferenceComponents.has(dependency)){thirdPartyScan=true}})}else{msg.error("JETWC: Unable to scan for reference component dependencies - JETWC-BOOTSTRAP was not run")}}}if(thirdPartyScan){licenseFileFound=false;licenseCandidate="THIRD_PARTY_LICENSE.txt";for(let i=0;i<compRootFiles.length;i++){const rootEntry=compRootFiles[i];if(rootEntry.isFile&&rootEntry.name===licenseCandidate){licenseFileFound=true;break}}if(!licenseFileFound){issue=new context.Issue(`Component with third party dependencies is missing 3rd party license information file in root, 'THIRD_PARTY_LICENSE.txt' is expected (case sensitive)`);issue.setMsgKey(context.ruleName+"_2");context.reporter.addIssue(issue,context)}}}};module.exports=ruleDef;