/*
 * 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"jetwc-component-location"};ruleDef.prototype.getShortDescription=function(){return"Ensure components are defined in the correct folder structure"};ruleDef.prototype.getDescription=function(){return"This rule applies to both singleton and JET Pack components and verifies that they are correctly placed in the folder structure based on their naming"};ruleDef.prototype.register=function(context){return{compjson:this._doAudit}};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 componentParentPack=meta.pack;const componentName=meta.name;const fullName=Utils.getComponentFullName(meta);const pathSegments=context.filepath.split("/");const componentRoot=context.rulePack.getExtension().componentHome;const sepStrip=/\//g;const parentFolder=pathSegments[pathSegments.length-3];const grandParentFolder=pathSegments[pathSegments.length-4];let expectedParent;let expectedGrandParent;const processingBuilt=context.rulePack.getExtension().auditBuildOutput;if(processingBuilt){if(componentParentPack){expectedParent=Utils.componentInfo(context,componentParentPack).version;expectedGrandParent=componentParentPack}else{expectedParent=componentName;expectedGrandParent=componentRoot.replace(sepStrip,"")}}else{if(componentParentPack){expectedParent=componentParentPack;expectedGrandParent=componentRoot.replace(sepStrip,"")}else{expectedParent=componentRoot.replace(sepStrip,"")}}if(parentFolder!==expectedParent){if(!processingBuilt){issue=new context.Issue(`Component '${fullName}' should be defined in a folder which is a child of '${expectedParent}'`);issue.setMsgKey(context.ruleName+"_1")}else{issue=new context.Issue(`Built component '${fullName}' should be present in a folder which is a child of '${expectedGrandParent}${path.sep}${expectedParent}'`);issue.setMsgKey(context.ruleName+"_3")}}if(!issue){if(expectedGrandParent&&grandParentFolder!==expectedGrandParent){issue=new context.Issue(`Parent folder for ${processingBuilt?"built":""} component '${fullName}' should be present in a folder called '${expectedGrandParent}'`);issue.setMsgKey(context.ruleName+"_2")}}if(issue){context.reporter.addIssue(issue,context)}}}else{Utils.reportMissingBootstrapInfo(context,"auditBuildOutput")}};module.exports=ruleDef;