/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import{toSymbolId as e}from"./arcadeEnvironment.js";import{ArcadeCompilationError as t}from"./executionError.js";import r from"../core/Logger.js";const n=()=>r.getLogger("esri.arcade.compilerUtils");function a(t,r,n){switch(t.type){case"VariableDeclaration":for(const n of t.declarations)r.set(e(n.id),n.id.name);return;case"BlockStatement":for(const e of t.body)a(e,r,n);return;case"ExportNamedDeclaration":{const e=new Map;a(t.declaration,e,n);for(const[t,a]of e)r.set(t,a),n.add(t);return}case"ForInStatement":case"ForOfStatement":return"VariableDeclaration"===t.left.type&&a(t.left,r,n),void a(t.body,r,n);case"ForStatement":return"VariableDeclaration"===t.init?.type&&a(t.init,r,n),void a(t.body,r,n);case"FunctionDeclaration":return void r.set(e(t.id),t.id.name);case"IfStatement":return a(t.consequent,r,n),void(t.alternate&&a(t.alternate,r,n));case"ImportDeclaration":for(const n of t.specifiers)r.set(e(n.local),n.local.name);return;case"WhileStatement":return void a(t.body,r,n);case"BreakStatement":case"ContinueStatement":case"EmptyStatement":case"ExpressionStatement":case"ReturnStatement":return}}function o(e){const r=new Map,o=new Set;for(const t of e.body)a(t,r,o);for(const a of o)if(!r.has(a)){const e=new Set(o);for(const t of r.keys())e.delete(t);throw n().error(`Internal error: exported undeclared symbols {${Array.from(e).join(", ")}}`),new t("NeverReach",null)}return{globals:r,exports:o}}function s(e){const r=new Map,o=new Set;if(a(e.body,r,o),o.size>0)throw n().error(`Internal error: cannot export locals {${Array.from(o.values()).join(", ")}}`),new t("NeverReach",null);return r}function c(e){const t=[];let r=[],n=e.expression;for(;;)switch(n.type){case"MemberExpression":r.push(n),n=n.object;break;case"CallExpression":r.push(n),n=n.callee;break;case"SafeMemberExpression":t.push({checked:n,unchecked:r.reverse()}),r=[],n=n.object;break;default:return{root:r.at(0)??n,sections:t.reverse()}}}function i(e,t){switch(e.type){case"VariableDeclaration":for(const r of e.declarations)if(null!=r.init&&i(r.init,t)||t(r))return!0;return!1;case"BlockStatement":for(const r of e.body)if(i(r,t))return!0;return!1;case"ExportNamedDeclaration":return i(e.declaration,t);case"ForInStatement":case"ForOfStatement":if(i(e.right,t))return!0;if("VariableDeclaration"===e.left.type){if(i(e.left,t))return!0}else e.left.type;return i(e.body,t);case"ForStatement":return!(null==e.init||!i(e.init,t))||(!(null==e.test||!i(e.test,t))||(!!i(e.body,t)||!(null==e.update||!i(e.update,t))));case"FunctionDeclaration":return!!t(e)||i(e.body,t);case"IfStatement":return!!i(e.test,t)||(!!i(e.consequent,t)||!(null==e.alternate||!i(e.alternate,t)));case"WhileStatement":return i(e.test,t)||i(e.body,t);case"ImportDeclaration":case"BreakStatement":case"ContinueStatement":case"Identifier":case"Literal":return t(e);case"EmptyStatement":default:return!1;case"ReturnStatement":return null!=e.argument&&i(e.argument,t)||t(e);case"ExpressionStatement":case"MemberAccessChainExpression":return i(e.expression,t);case"UpdateExpression":return i(e.argument,t)||t(e);case"AssignmentExpression":if("MemberExpression"===e.left.type){if(i(e.left.object,t))return!0;if(e.left.computed&&i(e.left.property,t))return!0}else e.left.type;return t(e.right)||i(e.right,t);case"TemplateLiteral":for(const r of e.expressions)if(i(r,t))return!0;return!1;case"MemberExpression":case"SafeMemberExpression":return i(e.object,t)||e.computed&&i(e.property,t);case"CallExpression":return i(e.callee,t)||e.arguments.some(e=>i(e,t))||t(e);case"UnaryExpression":return i(e.argument,t);case"BinaryExpression":case"LogicalExpression":return i(e.left,t)||i(e.right,t);case"ArrayExpression":return t(e)||e.elements.some(e=>i(e,t));case"ObjectExpression":return t(e)||e.properties.some(e=>i(e.value,t))}}function l(e,t){const r=new Map(e.map(e=>[e.line,e])),n=new Map;for(const a of t.body)i(a,e=>{const t=e.loc.start.line;if(r.has(t)){const a=r.get(t);return r.delete(t),n.set(e,a),r.size<=0}return!1});return n}export{o as collectDeclaredGlobalNames,s as collectDeclaredLocalNames,c as flattenAccessChain,l as resolveBreakpoints};
