/*
 * 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-slots"};ruleDef.prototype.getShortDescription=function(){return"Verify that all declared slots are actually used"};ruleDef.prototype.getDescription=function(){return"This rule checks that any slots declared in the metadata are then actually used via an oj-bind-* in the associated html"};ruleDef.prototype.register=function(context){return{"<oj-bind-template-slot>":this._flagSlotUse,"<oj-bind-slot>":this._flagSlotUse,closedownRP:this._doAudit}};ruleDef.prototype._flagSlotUse=function(context){const slotName=context.tagNode.attribs.name?context.tagNode.attribs.name:"__default__";if(slotName&&!context.utils.DomUtils.isExpression(slotName)){const componentName=Utils.matchComponentPath(context,path.dirname(context.filepath));if(componentName){this._markSlotUsed(context,componentName,slotName)}}};ruleDef.prototype._doAudit=function(context){if(context.rulePack.getExtension()){const slotUsage=context.rulePack.getExtension().slotUsage;if(slotUsage){slotUsage.forEach(function(value,key){if(!value){const idParts=key.split(":");const componentName=idParts[0];const slotName=idParts[1];if(!Utils.shouldIgnoreCheck(context,"jetwc-unused-slots",componentName,"slots",slotName)){const compInfo=Utils.componentInfo(context,componentName);const reportSource=compInfo.rootFolder+"/component.json";const issue=new context.Issue(`Slot '${slotName?slotName:"default"}' for component ${componentName} does not appear to be bound to the UI`);issue.setReportedFilePath(reportSource);context.reporter.addIssue(issue,context)}}})}}};ruleDef.prototype._markSlotUsed=function(context,componentName,slotName){let slotUsage=context.rulePack.getExtension().slotUsage;const slotKey=`${componentName}:${slotName}`;if(!slotUsage){slotUsage=new Map;context.rulePack.getExtension().slotUsage=slotUsage}if(!slotUsage.has(slotKey)){const slotComponentDef=context.rulePack.getExtension().allComponents.get(componentName);if(slotComponentDef.hasOwnProperty("slots")){Object.keys(slotComponentDef.slots).forEach(slot=>{slotUsage.set(`${componentName}:${slot?slot:"__default__"}`,false)})}}if(slotUsage.has(slotKey)){slotUsage.set(slotKey,true)}};module.exports=ruleDef;