/*
 * 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 ruleDef=function(){};ruleDef.prototype.getName=function(){return"jetwco-compjson-ns-reg"};ruleDef.prototype.getShortDescription=function(){return"Web component namespaces starting with 'oj-' or 'ns-' must be registered."};ruleDef.prototype.getDescription=function(){return"Web components namespaces starting with prefixes 'oj-' or 'ns-' must be registered. A component must not use an un-registered namespace."};ruleDef.prototype.register=function(context){return{startup:this._enableIfRequired,compjson:this._doAudit,Program:this._doVComponentAudit}};ruleDef.prototype._enableIfRequired=function(context){if(context.rulePack.isRuleEnabled("oj-cca-compjson-ns-reg","JET")){context.rulePack.disableRule()}};ruleDef.prototype._doAudit=function(context){if(context.suppData.obj){const meta=context.suppData.obj;const componentName=Utils.getComponentFullName(meta);if(componentName&&!context.utils.metaLib.isJetTag(componentName)&&(componentName.startsWith("oj-")||componentName.startsWith("ns-"))){const nameParts=componentName.split("-");if(nameParts.length>1){const checkNS=nameParts[0]+"-"+nameParts[1];if(!context.utils.metaLib.isNSRegistered(checkNS)){const issue=new context.Issue(`${componentName}: unregistered use of the '${nameParts[0]}' namespace`);issue.setPosition(this._getNamePos(context.suppData.ast));context.reporter.addIssue(issue,context)}}}}};ruleDef.prototype._doVComponentAudit=function(context){const componentInfo=TsxUtils.matchToVComponentImpl(context,context.filepath);if(componentInfo){const componentName=componentInfo.fullName;if(componentName&&!context.utils.metaLib.isJetTag(componentName)&&(componentName.startsWith("oj-")||componentName.startsWith("ns-"))){const nameParts=componentName.split("-");if(nameParts.length>1){const checkNS=nameParts[0]+"-"+nameParts[1];if(!context.utils.metaLib.isNSRegistered(checkNS)){const issue=new context.Issue(`${componentName}: unregistered use of the '${nameParts[0]}' namespace`);context.reporter.addIssue(issue,context)}}}}};ruleDef.prototype._getNamePos=function(ast){var props,name,pos,ret=null,i;if(ast&&ast.members){props=ast.members;for(i=0;i<props.length;i++){name=props[i];if(name.name==="name"){pos=name.pos;ret={line:pos.row,col:pos.col,startIndex:pos.index,endIndex:pos.index+5};break}}}return ret};module.exports=ruleDef;