/*
 * 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 NT=require("./AstNodeTypes");const JsParser=require("espree");const COMMON_DOC_METHODS={"getElementsByClassName":true,"getElementsByTagName":true,"getElementsByTagNameNS":true,"getElementById":true,"getElementsByName":true,"querySelector":true,"querySelectorAll":true,"createElement":true,"createElementNS":true};var AST=function(data,ecmaVer,comments,msgCtx){var opts={ecmaVersion:ecmaVer,comment:comments};if(ecmaVer>5){opts.sourceType="module"}this._ast=JsParser.parse(data,opts);this._data=data;this._utils=null;this._program=null;this._isComments=comments;this._msgCtx=msgCtx};AST.prototype.walk=function(callback){try{this._walkAddParent(callback)}catch(e){console.log(`JAF internal JS WALK ERROR: ${e}`)}};AST.prototype.getRawComments=function(){return this._ast.comments};AST.prototype.getNodeFromIndex=function(index){return _getNodeFromIndex2(this._ast,index)};function _getNodeFromIndex2(node,index){var body,n,i,ret=null;if(!node){return null}if(index===node.start){return node}if(index<node.start||index>node.end){return null}if(node.body){body=node.body;if(Array.isArray(body)){for(i=0;i<body.length;i++){n=body[i];if(index<n.start){ret=n}else{ret=_getNodeFromIndex2(n,index)}if(ret){if(ret===-1){ret=null}break}}}else{ret=_getNodeFromIndex2(body,index);if(ret){if(ret===-1){ret=null}}}}else{if(node.type==="IfStatement"){ret=_getNodeFromIndex2(node.consequent,index)}else{ret=node}}return ret};AST.prototype.getUtils=function(){if(!this._utils){this._utils={getBlock:this.getBlock.bind(this),getBody:this.getBody.bind(this),getProgram:this.getProgram.bind(this),getContainingFunctionName:this.getContainingFunctionName.bind(this),isFuncArg:this.isFuncArg.bind(this),getNodeTypes:_getNodeTypes,isCommonDocApi:this.isCommonDocApi,parseDefine:this.parseDefine.bind(this),parseImport:this.parseImport.bind(this)}}return this._utils};function _getNodeTypes(){return NT};AST.prototype.getBlock=function(node){var block;if(!node){if(!this._program){node=this._ast;if(node.type!==NT.PROGRAM){node=null}this._program=node}else{node=this._program}block=node}else{while(node){if(node.type){if(node.type===NT.BLOCK_STMT||node.type===NT.PROGRAM){block=node;break}}node=node.parent}}return block};AST.prototype.getBody=function(node){var block=this.getBlock(node);return block?block.body:block};AST.prototype.getFunctionBody=function(node){var block;if(!node){if(!this._program){node=this._ast;if(node.type!==NT.PROGRAM){node=null}this._program=node}else{node=this._program}block=node}else{while(node){if(node.type){if(node.type===NT.FUNC_DECLARATION||node.type===NT.PROGRAM){block=node;break}}node=node.parent}}return block};AST.prototype.getProgram=function(){return this._ast.type===NT.PROGRAM?this._ast:null};AST.prototype.getContainingFunctionName=function(node){var body=this.getFunctionBody(node);return body&&body.id?body.id.name:undefined};AST.prototype.getData=function(){return this._data};AST.prototype.isFuncArg=function(node,varName){var args,arg,n,j,ret=false;var n=node;while(n.type&&n.type!==NT.FUNC_DECLARATION){n=n.parent;if(!n||!n.type){n=null;break}}if(n){args=n.params;for(j=0;j<args.length;j++){arg=args[j];if(arg.type===NT.IDENTIFIER){if(arg.name===varName){ret=true;break}}}}return ret};AST.prototype.isCommonDocApi=function(method){return COMMON_DOC_METHODS[method]};AST.prototype.parseDefine=function(node,obj){var modules,names,len,i;if(!node.arguments||node.arguments.length<2){return null}modules=node.arguments[0].elements;names=node.arguments[1].params;obj=obj||null;if(modules&&names&&modules.length){len=Math.min(modules.length,names.length);obj=obj?obj:{};for(i=0;i<len;i++){obj[names[i].name]=modules[i].value}}return obj};AST.prototype.parseImport=function(node,obj){var source,specifiers,spec,name,i;obj=obj||{};source=node.source&&node.source.value;if(node.specifiers){specifiers=node.specifiers;for(var i=0;i<specifiers.length;i++){spec=specifiers[i];if((spec.type===NT.IMPORT_DEF_SPECIFIER||spec.type===NT.IMPORT_SPECIFIER)&&spec.local){name=spec.local.name;obj[name]=source}}}return obj};AST.prototype._walkAddParent=function(fn){var stack=[this._ast],i,j,key,len,node,child,subchild;for(i=0;i<stack.length;i+=1){node=stack[i];if(node.type==="Line"||node.type==="EmptyStatement"){continue}fn(node);for(key in node){if(key!=="parent"){child=node[key];if(child instanceof Array){for(j=0,len=child.length;j<len;j+=1){subchild=child[j];if(subchild){subchild.parent=node;stack.push(subchild)}}}else if(child!=void 0&&typeof child.type==="string"){child.parent=node;stack.push(child)}}}}};module.exports=AST;