/*
 * 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 RULENAME="oj-tsx-ojoption-text";const DESCRIPTION="<oj-option> components cannot contain other custom elements that might render "+"something other than the expected text node that is used to define the label of the option ";const SHORT_DESCRIPTION="<oj-options> components cannot contain elements that render something other than a text node.";const DIV="div";const SPAN="span";const OJ_IF="oj-if";const OJ_BIND_IF="oj-bind-if";const OJ_BIND_TEXT="oj-bind-text";const OJ_CHECKBOXSET="oj-checkboxset";const OJ_MENU="oj-menu";const OJ_OPTION="oj-option";const TEMPLATE="template";const OJ_BIND_DASH="oj-bind-";const JSXTEXT="JSXText";const JSXELEMENT="JSXElement";const TSXCOMPONENT="TsxComponent";const JSXATTRIBUTE="JSXAttribute";const JSXIDENTIFIER="JSXIdentifier";const CHILD_TAGS=[DIV,SPAN,OJ_BIND_IF,OJ_IF];const FORMATS=["b","strong","i","em","mark","small","del","ins","sub","sup"];class Rule{getName(){return RULENAME}getDescription(){return DESCRIPTION}getShortDescription(){return SHORT_DESCRIPTION}register(regCtx){this._tsxUtils=regCtx.utils.tsxUtils;return{"Tsx<oj-option>":this._onOption}}_onOption(ruleCtx,tsxComp){if(this._pos&&tsxComp.loc.start.line===this._pos){return}this._haveText=this._done=false;this._pos=0;this._tsxComp=tsxComp;this._evalNodes(ruleCtx,tsxComp.node.children)}_evalNodes(ruleCtx,childs){if(this._haveText||this._done){return}if(!childs||childs.length===0){let rc;if(rc=this._checkNoDataExceptions(ruleCtx,childs)){this._emitIssue(ruleCtx,rc)}return}for(const child of childs){if(this._haveText){return}if(child.type===JSXTEXT){this._haveText=true;break}if(child.type===JSXELEMENT){let name=child.openingElement.name.name;if(CHILD_TAGS.includes(name)||FORMATS.includes(name)){this._evalNodes(ruleCtx,child.children)}else if(name===OJ_BIND_TEXT){this._evalOjBindText(ruleCtx,child)}else{this._emitIssue(ruleCtx,1);this._done=true;if(name===OJ_OPTION){this._pos=child.loc.start.line}break}}}}_checkNoDataExceptions(ruleCtx,childs){var node,parent,childs,name;node=this._tsxComp;if(name=this._getParent(node)){if(name===OJ_MENU){return 0}if(name===OJ_CHECKBOXSET){let n=0;childs=node.parent.children;childs.forEach(child=>{if(child.type===TSXCOMPONENT&&child.name===OJ_OPTION){n++}});return n===1?0:2}}else{return 0}if(this._tsxUtils.getProperty(node,"data-oj-command")){return 0}return 1}_evalOjBindText(ruleCtx,child){var attribs,attrib;if(attribs=child?.openingElement.attributes){for(let i=0;i<attribs.length;i++){attrib=attribs[i];if(attrib.type===JSXATTRIBUTE&&attrib.name.type===JSXIDENTIFIER){break}}}if(!attribs||!attrib||attrib.name.name!=="value"){this._emitIssue(ruleCtx,1)}}_emitIssue(ruleCtx,msgType){var msg;if(msgType===1||msgType===2){msg="The default slot of <oj-option> must evaluate to a text label.";ruleCtx.reporter.addIssue(new ruleCtx.Issue(msg),ruleCtx)}}_getParent(node){var parent,name;if(parent=node.parent){name=parent.name;if(name==TEMPLATE||name.startsWith(OJ_BIND_DASH)){return this._getParent(parent)}}else{name=null}return name}}module.exports=Rule;