/*
 * 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 HtmlUtils=require("./HtmlUtils");const JSXELEMENT="JSXElement";const JSXATTRIBUTE="JSXAttribute";const JSXSPREADATTRIBUTE="JSXSpreadAttribute";const IDENTIFIER="Identifier";const MEMBEREXPRESSION="MemberExpression";var Jsx=function(metaLib,msgCtx){this._elemStack=[];this._parentStack=[];this._metaLib=metaLib;this._msgCtx=msgCtx};Jsx.prototype.genTsxRenderComponent=function(node,data){var tsxRC;this._data=data;try{switch(node.type){case"VariableDeclarator":tsxRC=this._onDecl(node);break;case"CallExpression":tsxRC=this._onCallExpr(node);break;case"ReturnStatement":tsxRC=this._onReturn(node);break;case"AssignmentExpression":tsxRC=this._onAssignExpr(node);break}}catch(e){this._msgCtx.error(`JAF TSXRC gen internal, file line=${node?.loc.start.line} : ${e}`)}return tsxRC};Jsx.prototype._toTsxRC=function(node,container){var first,tsxComp,curContainer,opelem,oName,props,childs;var isPreact,isJetTag,isWCTag;if(!container&&!first){container={type:"TsxRenderComponent",components:[],range:node.range,loc:node.loc};first=true;this._parentStack.length=this._elemStack.length=0}if(opelem=node.openingElement||node.openingFragment){if(oName=opelem.name){let compName;if(oName.type==="JSXIdentifier"){compName=oName.name}else if(oName.type==="JSXMemberExpression"){compName=oName.object.name+"."+oName.property.name}isPreact=!compName.includes("-")&&!HtmlUtils.isCommonElem(compName)&&compName.charAt(0).toUpperCase()===compName.charAt(0)||compName.includes(".");isJetTag=!isPreact&&this._metaLib.isJetTag(compName);isWCTag=!isPreact&&this._metaLib.isWCTag(compName);tsxComp={type:"TsxComponent",name:compName,elemStack:this._cloneElemStack(),ojNS:this._metaLib.isNamespaceTag(compName),isJetTag:isJetTag,isWCTag:isWCTag,isPreact:isPreact,loc:oName.loc,range:oName.range,node:node};this._elemStack.push(tsxComp);if(Array.isArray(container)){container.push(tsxComp)}else{container.components.push(tsxComp)}if((props=_propsToArray(opelem))&&props.length){let aProps,tsxProp;tsxComp.properties=aProps=[];props.forEach(prop=>{tsxProp={type:"TsxProperty"};if(prop.type===JSXATTRIBUTE){tsxProp.name=prop?.name?.name}else if(prop.type===JSXSPREADATTRIBUTE){if(prop.argument){if(prop.argument.type===IDENTIFIER){tsxProp.name=prop?.argument?.name}else if(prop.argument.type===MEMBEREXPRESSION){if(prop.argument?.object?.type===IDENTIFIER){tsxProp.name=prop.argument.object.name}else{tsxProp=null}}else{tsxProp=null}}}if(tsxProp){tsxProp.node=prop;tsxProp.loc=prop.loc;tsxProp.range=prop.range;aProps.push(tsxProp)}});this._genPropRawValues(aProps,tsxComp)}if(this._parentStack.length){tsxComp.parent=this._parentStack[this._parentStack.length-1]}this._parentStack.push(tsxComp);tsxComp.valueRaw=this._data.substring(node.range[0],node.range[1]);tsxComp.value=opelem}if(node.type!=="JSXFragment"){if(childs=node.children){childs.forEach(entry=>{if(entry.type===JSXELEMENT){if(!tsxComp.children){tsxComp.children=curContainer=[]}else{curContainer=tsxComp.children}this._toTsxRC(entry,curContainer)}})}this._elemStack.pop();this._parentStack.pop()}else{if(childs=node.children){childs.forEach(entry=>{if(entry.type===JSXELEMENT){this._toTsxRC(entry,container)}})}}}return first?container:tsxComp};Jsx.prototype._onDecl=function(node){var init,tsxRC;if(!((init=node.init)&&(node.init.openingElement||node.init.openingFragment))){return}tsxRC=this._toTsxRC(init);return tsxRC};Jsx.prototype._onCallExpr=function(node){var tsxRC,args,a;if(args=node.arguments){args.forEach(arg=>{if(arg.type===JSXELEMENT){if(tsxRC&&!a){a=[tsxRC]}tsxRC=this._toTsxRC(arg);if(a){a.push(tsxRC)}}})}return a?a:tsxRC;return tsxRC};Jsx.prototype._onReturn=function(node){var tsxRC,arg;if(arg=node.argument){if(arg.type===JSXELEMENT&&arg.openingElement||arg.type==="JSXFragment"){tsxRC=this._toTsxRC(arg)}}return tsxRC};Jsx.prototype._onAssignExpr=function(node){var tsxRC,right;if(right=node.right){if(right.type===JSXELEMENT&&right.openingElement){tsxRC=this._toTsxRC(right)}}return tsxRC};Jsx.prototype._cloneElemStack=function(){return[...this._elemStack]};Jsx.prototype._genPropRawValues=function(props,tsxComp){var node,val,self=this;props.forEach(prop=>{node=prop.node;if(val=node.value){prop.valueRaw=self._data.substring(val.range[0],val.range[1])}prop.parent=tsxComp})};function _propsToArray(node){var props,ret;if(props=node.attributes){ret=[];props.forEach(prop=>{ret.push(prop)})}return ret};module.exports=Jsx;