/*
 * 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 getParentNodeForSlot=require("./helpers/has-slot-name");const RULE_NAME="oj-html-slot-item-minmax";const DESCRIPT="Components can include a 'minItems' or 'maxItems' property in a slot defintion in the component metadata, to describe "+"how many child elements can be assigned to that slot.  This rule applies to components that are directly allocated to a "+"slot, or which are direct children of a template allocated to that slot.";const SHORT=DESCRIPT;const TEMPLATE="template";const OJ_SWITCHER="oj-switcher";function Rule(){};Rule.prototype.getName=function(){return RULE_NAME};Rule.prototype.getShortDescription=function(){return SHORT};Rule.prototype.getDescription=function(){return DESCRIPT};Rule.prototype.register=function(){this._handled=[];return{"elem":this._onElem,"file":this._onFile}};Rule.prototype._onElem=function(ruleCtx){var slotVal;if(ruleCtx.tagNode.attribs&&(slotVal=ruleCtx.tagNode.attribs.slot)){this._onSlot(ruleCtx,slotVal)}};Rule.prototype._onSlot=function(ruleCtx,slotVal){var issue,node,smd,min,max,items;if(!(node=getParentNodeForSlot(ruleCtx,slotVal))){return}if(this._isNodeHandled(node,slotVal)){return}if(node.name===OJ_SWITCHER){return}if(smd=ruleCtx.utils.metaLib.getTagSlotMetadata(node.name,slotVal)){min=typeof smd.minItems==="number"?smd.minItems:0;max=typeof smd.maxItems==="number"?smd.maxItems:0;if(min>0&&max<0){return}}if(ruleCtx.tag===TEMPLATE){items=_getTemplateChildTagCount(ruleCtx.tagNode.children)}else{items=_getChildTagCount(node.children,slotVal)}items=ruleCtx.tag===TEMPLATE?_getTemplateChildTagCount(ruleCtx.tagNode.children):_getChildTagCount(node.children,slotVal);let sItems;if(items>max&&max>0){sItems=max===1?"one element can":`${max} elements may`;issue=new ruleCtx.Issue(`<${node.name}> specifies that no more than ${sItems} be assigned to slot '${slotVal}'`);issue.setPosition(null,null,node.startIndex+1,node.startIndex+node.name.length);ruleCtx.reporter.addIssue(issue,ruleCtx);this._handleNode(node,slotVal)}if(min>0&&items<min){sItems=min===1?"one element":`${min} elements`;issue=new ruleCtx.Issue(`<${node.name}> specifies that at least ${sItems} must be assigned to slot '${slotVal}'`);issue.setMsgKey(RULE_NAME+"_1");issue.setSeverity("major");issue.setPosition(null,null,node.startIndex+1,node.startIndex+node.name.length);ruleCtx.reporter.addIssue(issue,ruleCtx);this._handleNode(node,slotVal)}};Rule.prototype._onFile=function(){this._handled.length=0};Rule.prototype._isNodeHandled=function(node,slot){var i,si;si=node.startIndex;for(i=0;i<this._handled.length;i+=2){if(this._handled[i]===si&&this._handled[i+1]===slot){return true}}return false};Rule.prototype._handleNode=function(node,slot){this._handled.push(node.startIndex);this._handled.push(slot)};function _getTemplateChildTagCount(childs){var n=0;if(childs){for(let i=0;i<childs.length;i++){if(childs[i].type==="tag"){n++}}}return n};function _getChildTagCount(childs,slot){var child,n=0;if(childs){for(let i=0;i<childs.length;i++){child=childs[i];if(child.type==="tag"&&child.attribs&&child.attribs.slot===slot){n++}}}return n};module.exports=Rule;