"""
transpiler/project_context.py — Global state for multi-file projects.
"""

from __future__ import annotations
import threading
from typing import Dict, Any, Optional
from transpiler.middleend.ir import IRModule

class ModuleExport:
    def __init__(self, name: str, uri: Optional[str] = None):
        self.name = name
        self.uri = uri
        self.symbols: Dict[str, str] = {} # name -> type_name
        self.definitions: Dict[str, tuple[int, int]] = {} # name -> (line, col)
        self.functions: Dict[str, Any] = {} # name -> metadata
        self.classes: Dict[str, Any] = {} # name -> class info (props, methods)
        self.usages: Dict[str, List[tuple[str, int, int]]] = {} # name -> list of (uri, line, col)

class ProjectContext:
    def __init__(self):
        self.modules: Dict[str, ModuleExport] = {}
        self.workspace_root: Optional[str] = None
        self._lock = threading.Lock()

    def get_module(self, module_name: str, uri: Optional[str] = None) -> ModuleExport:
        with self._lock:
            if module_name not in self.modules:
                self.modules[module_name] = ModuleExport(module_name, uri)
            elif uri and not self.modules[module_name].uri:
                self.modules[module_name].uri = uri
            return self.modules[module_name]

    def define_export(self, module_name: str, symbol: str, type_name: str, lineno: int = 0, col: int = 0):
        module = self.get_module(module_name)
        with self._lock:
            module.symbols[symbol] = type_name
            module.definitions[symbol] = (lineno, col)
