/*
 * 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 path=require("path");const{nodeModuleNameResolver}=require("typescript");const{nextTick}=require("process");let NT;const ruleDef=function(){};ruleDef.prototype.getName=function(){return"jetwc-vcomponent-loader"};ruleDef.prototype.getShortDescription=function(){return"Verify that a VComponent has the required loader"};ruleDef.prototype.getDescription=function(){return"This rule checks that a VComponent is correctly matched with a loader.ts and that the class being exported matches the VComponent implementation"};ruleDef.prototype.register=function(context){NT=context.tsNodeTypes;return{startaudit:this._checkForApplicability,[NT.EXPORT_NAMED_DECL]:this._matchLoaderToComponents,closedownRP:this._checkForMissingLoaders}};ruleDef.prototype._checkForApplicability=function(context){if(context.rulePack.getExtension()&&!context.rulePack.getExtension().precompiled){context.rulePack.disableRule()}};ruleDef.prototype._matchLoaderToComponents=function(context){if(context.filepath.endsWith("/loader.ts")){const loaderRoot=Utils.toNativePath(context.filepath.substring(0,context.filepath.lastIndexOf("/")));let issue;if(context.node.exportKind==="value"&&context.node.source&&context.node.source.type===NT.LITERAL){const relativeSource=Utils.toNativePath(context.node.source.value);const absoluteSource=Utils.toJAFPath(path.join(loaderRoot,`${relativeSource}.tsx`));const componentInfo=TsxUtils.matchToVComponentImpl(context,absoluteSource);if(componentInfo){componentInfo.viewModel.loaderMatch=true;const exportedClass=componentInfo.viewModel.className;if(exportedClass&&context.node.specifiers){context.node.specifiers.forEach(specifier=>{if(specifier.exported.type===NT.IDENTIFIER&&specifier.exported.name===exportedClass){componentInfo.viewModel.loaderCorrectExport=true}})}if(exportedClass&&!componentInfo.viewModel.loaderCorrectExport){const issue=new context.Issue(`The loader for component ${componentInfo.fullName} does not re-export the expected class name of ${exportedClass}`);issue.setMsgKey(context.ruleName+"_2");context.reporter.addIssue(issue,context,"blocker")}}}}};ruleDef.prototype._checkForMissingLoaders=function(context){if(context.rulePack.getExtension()&&context.rulePack.getExtension().allComponents){const allVComponents=[...context.rulePack.getExtension().allComponents].map(pair=>pair[1]).filter(componentInfo=>componentInfo.vcomponent&&!componentInfo.viewModel.loaderMatch);allVComponents.forEach(componentInfo=>{let reportSource=componentInfo.fullName;if(componentInfo.viewModel.resolved&&!componentInfo.viewModel.skip){reportSource=componentInfo.viewModel.path}const issue=new context.Issue(`Component ${componentInfo.fullName} does not have a matching loader.ts. All VComponents must be re-exported by a loader.`);issue.setMsgKey(context.ruleName+"_1");issue.setReportedFilePath(reportSource);context.reporter.addIssue(issue,context)})}};module.exports=ruleDef;