/*
 * Copyright (c) 2018, 2025, 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-template-slots"};ruleDef.prototype.getShortDescription=function(){return"Verify template slots are named correctly"};ruleDef.prototype.getDescription=function(){return"This rule checks that any template slots used have the correct naming convention and have an associated data parameter"};ruleDef.prototype.register=function(context){return{"<oj-bind-template-slot>":this._doAudit}};ruleDef.prototype._doAudit=function(context){let issue;const DomUtils=context.utils.DomUtils;const componentName=Utils.matchComponentPath(context,path.dirname(context.filepath));if(componentName){const slotName=context.tagNode.attribs.name;const usesExpression=slotName&&DomUtils.isExpression(slotName);if(slotName&&!slotName.endsWith("Template")&&!usesExpression){issue=new context.Issue(`Template slot '${slotName}' for component ${componentName} does not follow the recommended naming pattern (should use a Template suffix)`);issue.setMsgKey(context.ruleName+"_1");context.reporter.addIssue(issue,context,"minor")}if(!usesExpression){const data=context.tagNode.attribs.data;const componentSlots=Utils.componentInfo(context,componentName).slots;if(!componentSlots||slotName&&!componentSlots.hasOwnProperty(slotName)||!slotName&&!componentSlots.hasOwnProperty("")){issue=new context.Issue(`Template slot '${slotName?slotName:"default"}' for component ${componentName} has not been declared in the component metadata`);issue.setMsgKey(context.ruleName+"_2");context.reporter.addIssue(issue,context)}else{const slotDetail=componentSlots[slotName?slotName:""];if(!slotDetail.data){issue=new context.Issue(`Template slot '${slotName?slotName:"default"}' for component ${componentName} has not been declared with a data attribute in the component metadata`);issue.setMsgKey(context.ruleName+"_3");context.reporter.addIssue(issue,context,"minor")}else{if(!data){issue=new context.Issue(`Template slot '${slotName?slotName:"default"}' for component ${componentName} has not been bound into the page with any data context`);issue.setMsgKey(context.ruleName+"_4");context.reporter.addIssue(issue,context)}else if(!DomUtils.isExpression(data)){issue=new context.Issue(`Template slot '${slotName?slotName:"default"}' for component ${componentName} has been bound into the page with an invalid data context - should be an expression`);issue.setMsgKey(context.ruleName+"_5");context.reporter.addIssue(issue,context)}}}}}};module.exports=ruleDef;