/*
 * 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"jetwc-component-dependencies"};ruleDef.prototype.getShortDescription=function(){return"Validate the stated dependencies for a component"};ruleDef.prototype.getDescription=function(){return"This rule checks that any dependencies defined for a component actually exist in the context of the project and are correct"};ruleDef.prototype.register=function(context){return{compjson:this._doAudit,Program:this._doVComponentAudit}};ruleDef.prototype._doAudit=function(context){if(context.rulePack.getExtension()&&context.rulePack.getExtension().hasOwnProperty("allComponents")){const componentMetadata=context.suppData.obj;const componentType=Utils.getComponentType(componentMetadata);if(componentMetadata){let issue;if(componentMetadata.dependencies){Object.keys(componentMetadata.dependencies).forEach(dependency=>{const dependentVersion=componentMetadata.dependencies[dependency];if(componentType==="pack"){if(!context.utils.semVerUtils.isValid(dependentVersion)){issue=new context.Issue(`Pack dependency '${dependency}':'${componentMetadata.dependencies[dependency]}' in ${componentMetadata.name} must be an absolute semver version not a range`);issue.setMsgKey(context.ruleName+"_1");context.reporter.addIssue(issue,context)}}else{if(!context.utils.semVerUtils.isValidRange(dependentVersion)){issue=new context.Issue(`Component dependency '${dependency}':'${componentMetadata.dependencies[dependency]}' in ${componentMetadata.name} is not a valid semver or semver range`);issue.setMsgKey(context.ruleName+"_2");context.reporter.addIssue(issue,context)}else{if(componentMetadata.type&&componentMetadata.type==="demo"&&context.utils.semVerUtils.isValid(dependentVersion)){issue=new context.Issue(`Demo component ${componentMetadata.name} depends on an absolute version of '${dependency}':'${componentMetadata.dependencies[dependency]}', range support is preferred for demos`);issue.setMsgKey(context.ruleName+"_3");context.reporter.addIssue(issue,context,"info")}}}if(!issue){this._validateDependency(context,componentMetadata.name,dependency,componentMetadata.dependencies[dependency])}})}}}else{Utils.reportMissingBootstrapInfo(context,"allComponents")}};ruleDef.prototype._doVComponentAudit=function(context){const componentInfo=TsxUtils.matchToVComponentImpl(context,context.filepath);if(componentInfo){if(componentInfo.dependencies){componentInfo.dependencies.forEach(dependency=>{this._validateDependency(context,componentInfo.fullName,dependency.name,dependency.range)})}}};ruleDef.prototype._validateDependency=function(context,fullName,dependencyName,dependencyVersion){const allVisibleComponents=context.rulePack.getExtension().allComponents;if(!allVisibleComponents.has(dependencyName)){issue=new context.Issue(`Component dependency '${dependencyName}':'${dependencyVersion}' in '${fullName}' has no match in the project`);issue.setMsgKey(context.ruleName+"_4");context.reporter.addIssue(issue,context)}else{const availableVersion=allVisibleComponents.get(dependencyName).version;if(!context.utils.semVerUtils.satisfiesRange(availableVersion,dependencyVersion)){issue=new context.Issue(`Component dependency '${dependencyName}' in '${fullName}' exists in the project but the versions are mismatched. Requested:'${dependencyVersion}', version available in project: '${availableVersion}'`);issue.setMsgKey(context.ruleName+"_5");context.reporter.addIssue(issue,context)}}};module.exports=ruleDef;