/**
 * @license
 * Copyright 2025 ReverseCraft
 * SPDX-License-Identifier: Apache-2.0
 */
import*as e from"acorn";import{buildLineOffsets as t}from"./context-code-utils.js";function n(e,t){let n=1,o=-1;for(let r=0;r<t&&r<e.length;r++)"\n"===e[r]&&(n++,o=r);return{line:n,column:t-o-1}}function o(e){return e?e.map((e,t)=>e.name?e.name:"RestElement"===e.type?"...rest":"ObjectPattern"===e.type?"{...}":"ArrayPattern"===e.type?"[...]":(e.type,`param${t}`)):[]}export function parseScript(r,s,a){const i=[],l=[],c=[],p=["<global>"];let u;try{u=e.parse(a,{ecmaVersion:"latest",sourceType:"module",allowHashBang:!0,allowAwaitOutsideFunction:!0,allowImportExportEverywhere:!0,allowReserved:!0})}catch(e){const n=e;c.push({scriptId:r,scriptUrl:s,message:n.message||String(e),line:n.loc?.line,column:n.loc?.column});const o=t(a);return{ast:null,functions:i,calls:l,errors:c,lineOffsets:o}}function m(e,t,l,c){const p=n(a,l.start);i.push({name:e,scriptId:r,scriptUrl:s,lineNumber:p.line,columnNumber:p.column,params:o(c),type:t})}function f(e,t){var o;if(e)switch(e.type){case"FunctionDeclaration":{const n=e.id?.name||t||`<anonymous@${e.start}>`;m(n,"declaration",e,e.params),p.push(n),y(e.body),p.pop();break}case"FunctionExpression":{const n=e.id?.name||t||`<anonymous@${e.start}>`;m(n,e.id?.name||t?"expression":"anonymous",e,e.params),p.push(n),y(e.body),p.pop();break}case"ArrowFunctionExpression":{const n=t||`<anonymous@${e.start}>`;m(n,"arrow",e,e.params),p.push(n),y(e.body),p.pop();break}case"VariableDeclaration":for(const t of e.declarations||[]){const e="Identifier"===t.id?.type?t.id.name:void 0;t.init&&f(t.init,e)}break;case"AssignmentExpression":{let t;"Identifier"===e.left?.type&&e.left.name?t=e.left.name:"MemberExpression"===e.left?.type&&e.left.property?.name&&(t=e.left.property.name),f(e.right,t);break}case"Property":{const t=e.key?.name||e.key?.value;e.value&&("FunctionExpression"===e.value.type||"ArrowFunctionExpression"===e.value.type?f(e.value,t):f(e.value));break}case"MethodDefinition":{const t=e.key?.name||e.key?.value||`<method@${e.start}>`;if(e.value){const n=e.value;m(t,"method",n,n.params),p.push(t),y(n.body),p.pop()}break}case"CallExpression":{const t="Identifier"===(o=e.callee).type&&o.name?o.name:"MemberExpression"===o.type&&o.property?.name?o.property.name:null;if(t){const o=n(a,e.start);l.push({caller:p[p.length-1],callee:t,scriptId:r,lineNumber:o.line,columnNumber:o.column})}f(e.callee);for(const t of e.arguments||[])f(t);break}case"ObjectExpression":for(const t of e.properties||[])f(t);break;case"ClassDeclaration":case"ClassExpression":{const t=e.body;if(t?.body)for(const e of t.body)f(e);break}default:if(y(e.body),e.expression&&f(e.expression),e.init&&f(e.init),e.test&&f(e.test),e.update&&f(e.update),e.consequent&&y(e.consequent),e.alternate&&f(e.alternate),e.argument&&f(e.argument),e.left&&f(e.left),e.right&&f(e.right),e.object&&f(e.object),e.elements)for(const t of e.elements)t&&f(t);if(e.declarations)for(const t of e.declarations)t.init&&f(t.init)}}function y(e){if(e)if(Array.isArray(e))for(const t of e)f(t);else f(e)}y(u.body);const d=t(a);return{ast:u,functions:i,calls:l,errors:c,lineOffsets:d}}