/*
 * 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/tsx/has-slot-name");const RULE_NAME="oj-tsx-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";const SLOT="slot";const LITERAL="Literal";const TSXCOMPONENT="TsxComponent";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{"Tsx<slot=>":this._onSlot,"file":this._onFile}};Rule.prototype._onSlot=function(ruleCtx,tsxProp){var issue,tagNode,pnode,smd,min,max,items,slotName;slotName=tsxProp.node.value;if(slotName.type===LITERAL){slotName=slotName.value}else{return}tagNode=tsxProp.parent;if(!(pnode=getParentNodeForSlot(ruleCtx,tagNode,slotName))){return}if(this._isNodeHandled(pnode,slotName)){return}if(pnode.name===OJ_SWITCHER){return}if(smd=ruleCtx.utils.metaLib.getTagSlotMetadata(pnode.name,slotName)){min=typeof smd.minItems==="number"?smd.minItems:0;max=typeof smd.maxItems==="number"?smd.maxItems:0;if(min>0&&max<0){return}}items=tagNode.name===TEMPLATE?_getTemplateChildTagCount(tagNode.children):_getChildTagCount(pnode.children,slotName);let sItems;if(items>max&&max>0){sItems=max===1?"one element can":`${max} elements may`;issue=new ruleCtx.Issue(`<${pnode.name}> specifies that no more than ${sItems} be assigned to slot '${slotName}'`);ruleCtx.utils.tsxUtils.setIssuePosition(issue,pnode);ruleCtx.reporter.addIssue(issue,ruleCtx);this._handleNode(pnode,slotName)}if(min>0&&items<min){sItems=min===1?"one element":`${min} elements`;issue=new ruleCtx.Issue(`<${pnode.name}> specifies that at least ${sItems} must be assigned to slot '${slotName}'`);issue.setMsgKey(RULE_NAME+"_1");issue.setSeverity("major");ruleCtx.utils.tsxUtils.setIssuePosition(issue,pnode);ruleCtx.reporter.addIssue(issue,ruleCtx);this._handleNode(pnode,slotName)}};Rule.prototype._onFile=function(){this._handled.length=0};Rule.prototype._isNodeHandled=function(node,slot){var i,startIndex;startIndex=node.node.range[0];for(i=0;i<this._handled.length;i+=2){if(this._handled[i]===startIndex&&this._handled[i+1]===slot){return true}}return false};Rule.prototype._handleNode=function(node,slot){this._handled.push(node.node.range[0]);this._handled.push(slot)};function _getTemplateChildTagCount(childs){var n=0,child;if(childs){for(let i=0;i<childs.length;i++){child=childs[i];if(child.type===TSXCOMPONENT){n++}}}return n};function _getChildTagCount(childs,slot){var child,props,prop,n=0;if(childs){for(let i=0;i<childs.length;i++){child=childs[i];if(child.type==="TsxComponent"){if(props=child.properties){for(let j=0;j<props.length;j++){prop=props[j];if(prop.name===SLOT&&prop.node.value.type===LITERAL&&prop.node.value.value===slot){n++}}}}}}return n};module.exports=Rule;