/*
 * 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-unused-events"};ruleDef.prototype.getShortDescription=function(){return"Verify that all declared events are actually used"};ruleDef.prototype.getDescription=function(){return"This rule checks that any events declared in the metadata are then actually raised in the associated viewModel"};ruleDef.prototype.register=function(context){return{CallExpression:this._flagEventUse,closedownRP:this._doAudit}};ruleDef.prototype._flagEventUse=function(context,node){Utils.deriveViewModels(context,node);const NT=context.utils.AstUtils.getNodeTypes();if(node.callee.type===NT.MEMBER_EXPR&&node.callee.property.type===NT.IDENTIFIER&&node.callee.property.name==="dispatchEvent"){if(node.arguments[0].type===NT.CALL_EXPR||node.arguments[0].type===NT.NEW_EXPR){if(node.arguments[0].callee.name==="CustomEvent"){const eventNameNode=node.arguments[0].arguments[0];if(eventNameNode.type===NT.LITERAL){const eventName=eventNameNode.value;const componentName=Utils.matchComponentPath(context,path.dirname(context.filepath));this._markEventUsed(context,componentName,eventName)}}}}};ruleDef.prototype._doAudit=function(context){if(context.rulePack.getExtension()){const eventUsage=context.rulePack.getExtension().eventUsage;if(eventUsage){eventUsage.forEach(function(value,key){if(!value){const idParts=key.split(":");const componentName=idParts[0];const eventName=idParts[1];if(!Utils.shouldIgnoreCheck(context,"jetwc-unused-events",componentName,"events",eventName)){const compInfo=Utils.componentInfo(context,componentName);let reportSource=compInfo.fullName;if(compInfo.viewModel.resolved&&!compInfo.viewModel.skip){reportSource=compInfo.viewModel.path}const issue=new context.Issue(`Event '${eventName}' for component ${componentName} does not appear to be raised`);issue.setReportedFilePath(reportSource);context.reporter.addIssue(issue,context)}}})}}};ruleDef.prototype._markEventUsed=function(context,componentName,eventName){const msg=context.utils.msgLib;if(componentName){let eventUsage=context.rulePack.getExtension().eventUsage;const eventKey=`${componentName}:${eventName}`;if(!eventUsage){eventUsage=new Map;context.rulePack.getExtension().eventUsage=eventUsage}if(!eventUsage.has(eventKey)){const eventComponentDef=context.rulePack.getExtension().allComponents.get(componentName);if(eventComponentDef){if(eventComponentDef.hasOwnProperty("events")){Object.keys(eventComponentDef.events).forEach(event=>{eventUsage.set(`${componentName}:${event}`,false)})}}else{msg.error(`JETWC Error (jetwc-unused-events): component ${componentName} missing from allComponents map`)}}if(eventUsage.has(eventKey)){eventUsage.set(eventKey,true)}}else{msg.msg(`JETWC Warning (jetwc-unused-events): component event ${eventName} cannot be mapped to a component due to missing viewModel mapping`)}};module.exports=ruleDef;