/*
 * 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-css-pack-load-path"};ruleDef.prototype.getShortDescription=function(){return"Checks that the canonical pack path is used when loading a css file from a component in a pack"};ruleDef.prototype.getDescription=function(){return"Components in a JET Pack should always use the canonical path for that pack when using the css! or ojcss! plugins rather than relative (./) addressing"};ruleDef.prototype.register=function(context){return{CallExpression:this._doAudit,ImportDeclaration:this._doAuditImport}};ruleDef.prototype._doAuditImport=function(context,node){const NT=context.utils.AstUtils.getNodeTypes();const withinComponent=Utils.matchComponentPath(context,path.dirname(context.filepath));if(withinComponent){if(node.importKind==="value"&&node.source&&node.source.type===NT.LITERAL&&this._matchRelativeReference(node.source.value)){this._packMemberCheck(context,withinComponent)}}};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=>{this._checkRequireTarget(context,withinComponent,arg)})}else if(node.arguments[0].type===NT.LITERAL){this._checkRequireTarget(context,withinComponent,node.arguments[0])}}};ruleDef.prototype._checkRequireTarget=function(context,withinComponent,node){const NT=context.utils.AstUtils.getNodeTypes();if(node.type===NT.LITERAL&&this._matchRelativeReference(node.value)){this._packMemberCheck(context,withinComponent)}};ruleDef.prototype._matchRelativeReference=function(importString){const cssImportMatcher=/^(?:css!|ojcss!)\.\//;return cssImportMatcher.test(importString)};ruleDef.prototype._packMemberCheck=function(context,withinComponent){if(withinComponent){const componentInfo=context.rulePack.getExtension().allComponents.get(withinComponent);if(componentInfo?.pack){const issue=new context.Issue(`Component ${componentInfo.fullName} should import css files using the canonical pack path e.g "${componentInfo.pack}/${componentInfo.name}/" rather than a relative path "./" to enable correct minification`);context.reporter.addIssue(issue,context)}}};module.exports=ruleDef;