/*
 * 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 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"jetwc-component-foldername"};ruleDef.prototype.getShortDescription=function(){return"Ensure components are defined in the correctly named folder"};ruleDef.prototype.getDescription=function(){return"This rule applies to both singleton and JET Pack components and verifies that they are correctly named in the folder structure based on their type"};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("auditBuildOutput")){if(context.suppData.obj){let issue;const meta=context.suppData.obj;const fullName=Utils.getComponentFullName(meta);const componentName=meta.name;const componentVersion=meta.version;const pathSegments=context.filepath.split("/");const componentFolder=pathSegments[pathSegments.length-2];const componentParentFolder=pathSegments[pathSegments.length-3];let expectedFolder;const processingBuilt=context.rulePack.getExtension().auditBuildOutput;if(!processingBuilt||meta.pack){expectedFolder=componentName}else{expectedFolder=componentVersion}if(componentFolder!==expectedFolder){issue=new context.Issue(`${processingBuilt?"For built output ":""}Component '${fullName}' should be defined in a folder called '${processingBuilt?componentName+path.sep:""}${expectedFolder}' not '${processingBuilt?componentParentFolder+path.sep:""}${componentFolder}' for referencing to work correctly`);context.reporter.addIssue(issue,context)}}}else{Utils.reportMissingBootstrapInfo(context,"auditBuildOutput")}};ruleDef.prototype._doVComponentAudit=function(context){const componentInfo=TsxUtils.matchToVComponentImpl(context,context.filepath);if(componentInfo){const fullName=componentInfo.fullName;let expectedPath=componentInfo.pack?componentInfo.name:componentInfo.fullName;const pathSegments=context.filepath.split("/");const componentFolder=pathSegments[pathSegments.length-2];const parentFolder=pathSegments[pathSegments.length-3];if(componentFolder!==expectedPath){issue=new context.Issue(`Component '${fullName}' should be defined in a folder called '${expectedPath}' not '${componentFolder}' for referencing to work correctly`);context.reporter.addIssue(issue,context)}}};module.exports=ruleDef;