/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
import{toSymbolId as e}from"./arcadeEnvironment.js";import{ArcadeCompilationError as t}from"./executionError.js";import"../core/has.js";import r from"../core/Logger.js";import{isSubsetOf as o,difference as n}from"../core/SetUtils.js";const a=()=>r.getLogger("esri.arcade.compilerUtils");function c(t,r,o){switch(t.type){case"VariableDeclaration":for(const o of t.declarations)r.add(e(o.id));return;case"BlockStatement":for(const e of t.body)c(e,r,o);return;case"ExportNamedDeclaration":{const e=new Set;c(t.declaration,e,o);for(const t of e)r.add(t),o.add(t);return}case"ForInStatement":case"ForOfStatement":return"VariableDeclaration"===t.left.type&&c(t.left,r,o),void c(t.body,r,o);case"ForStatement":return"VariableDeclaration"===t.init?.type&&c(t.init,r,o),void c(t.body,r,o);case"FunctionDeclaration":return void r.add(e(t.id));case"IfStatement":return c(t.consequent,r,o),void(t.alternate&&c(t.alternate,r,o));case"ImportDeclaration":for(const o of t.specifiers)r.add(e(o.local));return;case"WhileStatement":return void c(t.body,r,o);case"BreakStatement":case"ContinueStatement":case"EmptyStatement":case"ExpressionStatement":case"ReturnStatement":return}}function s(e){const r=new Set,s=new Set;for(const t of e.body)c(t,r,s);if(!o(s,r))throw a().error(`Internal error: exported undeclared symbols {${Array.from(n(s,r)).join(", ")}}`),new t(null,"NeverReach",null);return{globals:r,exports:s}}function i(e){const r=new Set,o=new Set;if(c(e.body,r,o),o.size>0)throw a().error(`Internal error: cannot export locals {${Array.from(o.values()).join(", ")}}`),new t(null,"NeverReach",null);return r}function l(e){const t=[];let r=[],o=e.expression;for(;;)switch(o.type){case"MemberExpression":r.push(o),o=o.object;break;case"CallExpression":r.push(o),o=o.callee;break;case"SafeMemberExpression":t.push({checked:o,unchecked:r.reverse()}),r=[],o=o.object;break;default:return{root:r.at(0)??o,sections:t.reverse()}}}export{s as collectDeclaredGlobalNames,i as collectDeclaredLocalNames,l as flattenAccessChain};
