/*
 * 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 ruleDef=function(){};ruleDef.prototype.getName=function(){return"jetwc-nls-root"};ruleDef.prototype.getShortDescription=function(){return"Checks for the existence of translations in the expected place"};ruleDef.prototype.getDescription=function(){return"This rule ensures that standards are being followed regarding the use of translation bundles"};ruleDef.prototype.register=function(context){return{CallExpression:this._doAudit,TSImportEqualsDeclaration:this._doAuditImportRequire}};ruleDef.prototype._doAudit=function(context,node){const NT=context.utils.AstUtils.getNodeTypes();const withinComponent=Utils.matchComponentPath(context,path.dirname(context.filepath));if(withinComponent&&(node.callee.name==="define"||node.callee.name==="require")){if(node.arguments[0].type===NT.ARRAY_EXPR&&node.arguments[0].elements.length>0){node.arguments[0].elements.forEach(arg=>{if(arg.value){this._checkRequireTarget(context,withinComponent,arg.value)}})}else if(node.arguments[0].type===NT.LITERAL&&node.arguments[0].value){this._checkRequireTarget(context,withinComponent,node.arguments[0].value)}}};ruleDef.prototype._doAuditImportRequire=function(context,node){const NT=context.utils.AstUtils.getNodeTypes();const withinComponent=Utils.matchComponentPath(context,path.dirname(context.filepath));if(withinComponent){if(node.moduleReference&&node.moduleReference.type===NT.EXTERN_MOD_REF&&node.moduleReference.expression){const sourcePath=node.moduleReference.expression.value;this._checkRequireTarget(context,withinComponent,sourcePath)}}};ruleDef.prototype._checkRequireTarget=function(context,withinComponent,sourcePath){if(sourcePath&&sourcePath.startsWith("ojL10n!")){let bundleStdLocation=false;const componentInfo=context.rulePack.getExtension().allComponents.get(withinComponent);const expectedFolderStart=sourcePath.indexOf("/resources/nls/");if(expectedFolderStart>0){const depathed=sourcePath.substring(7).split("/");const resourcesOffset=depathed.indexOf("resources");const nlsOffset=depathed.indexOf("nls");if(nlsOffset===resourcesOffset+1){const requireRoot=depathed.slice(0,nlsOffset-1).join("/");if(componentInfo.requirePaths.has(requireRoot)){bundleStdLocation=true}}}if(!bundleStdLocation){issue=new context.Issue(`String bundle for component ${componentInfo.fullName} is either not in the recommended location of /resources/nls/ relative to the source component or is defined with an unsupported path. Found: ${sourcePath}`);context.reporter.addIssue(issue,context)}}};module.exports=ruleDef;