//! ts-native 库入口
//! 提供 DLL 导出接口供 loader 调用

use std::sync::Once;

static PANIC_HOOK_INIT: Once = Once::new();

/// 初始化 panic hook（线程安全，只执行一次）
pub fn init_panic_hook() {
    PANIC_HOOK_INIT.call_once(|| {
        std::panic::set_hook(Box::new(|info| {
            eprintln!("\n=========== ts-native Library Panic ===========");
            if let Some(s) = info.payload().downcast_ref::<&str>() {
                eprintln!("Message: {}", s);
            } else if let Some(s) = info.payload().downcast_ref::<String>() {
                eprintln!("Message: {}", s);
            } else {
                eprintln!("Message: <unknown>");
            }
            if let Some(loc) = info.location() {
                eprintln!("Location: {}:{}", loc.file(), loc.line());
            }
            eprintln!("--- Backtrace ---");
            let bt = backtrace::Backtrace::new();
            for frame in bt.frames().iter().take(20) {
                for symbol in frame.symbols() {
                    let name = symbol.name().map(|n| n.to_string()).unwrap_or_else(|| "<unknown>".to_string());
                    let file = symbol.filename().and_then(|f| f.file_name()).and_then(|f| f.to_str()).unwrap_or("");
                    let line = symbol.lineno().map(|l| l.to_string()).unwrap_or_else(|| "?".to_string());
                    eprintln!("  {} ({}:{})", name, file, line);
                }
            }
            eprintln!("=============================================\n");
        }));
    });
}

pub mod diag;
pub mod codegen;
pub mod coff;
pub mod dep_graph;
#[cfg(feature = "native-linker")]
pub mod native_linker;
pub mod project;
pub mod project_compiler;
pub mod standalone_gen;
pub mod swc_transform;
pub mod tsn_ffi;

// 重新导出常用类型
pub use tsn_ffi::{TsnCompileResult, TsnSymbol, TsnRelocation};
