/**
 * Native binding loader — lazy initialization.
 *
 * The binding is loaded on first access via `getNative()`, NOT at import time.
 * This allows pure-JS modules (schema, columns, table) to be imported without
 * requiring the native `.node` binary to be present.
 *
 * Uses `new URL("./file.node", import.meta.url)` — the standard bundler pattern.
 * Rspack/Webpack emits matching .node files as assets.
 */
import { dlopen } from "node:process";
import { fileURLToPath } from "node:url";
function loadBundled() {
    const platform = process.platform;
    const arch = process.arch;
    let addonUrl;
    if (platform === "darwin" && arch === "arm64") {
        addonUrl = new URL("./sybase_native.darwin-arm64.node", import.meta.url);
    }
    else if (platform === "darwin" && arch === "x64") {
        addonUrl = new URL("./sybase_native.darwin-x64.node", import.meta.url);
    }
    else if (platform === "linux" && arch === "arm64") {
        addonUrl = new URL("./sybase_native.linux-arm64.node", import.meta.url);
    }
    else if (platform === "linux" && arch === "x64") {
        addonUrl = new URL("./sybase_native.linux-x64.node", import.meta.url);
    }
    else if (platform === "win32" && arch === "x64") {
        addonUrl = new URL("./sybase_native.win32-x64.node", import.meta.url);
    }
    else {
        return null;
    }
    try {
        const mod = { exports: {} };
        dlopen(mod, fileURLToPath(addonUrl));
        return mod.exports;
    }
    catch (err) {
        const message = err instanceof Error ? err.message : String(err);
        throw new Error(`drizzle-sybase: failed to load native binding for ${platform}-${arch}.\n` +
            `Path: ${fileURLToPath(addonUrl)}\n` +
            `Reason: ${message}\n` +
            `This usually means the .node binary has an unresolved DLL dependency (e.g. sybdb.dll on Windows).`);
    }
}
let _binding = null;
let _loaded = false;
/**
 * Get the native binding, loading it lazily on first call.
 * Throws if no binding is available for the current platform.
 */
export function getNative() {
    if (!_loaded) {
        _binding = loadBundled(); // throws on dlopen failure; returns null on unsupported platform
        _loaded = true;
    }
    if (!_binding) {
        throw new Error(`drizzle-sybase: unsupported platform ${process.platform}-${process.arch}.\n` +
            `Supported: darwin-arm64, darwin-x64, linux-arm64, linux-x64, win32-x64.`);
    }
    return _binding;
}
/**
 * Lazy-loaded native binding.
 * The binding is only loaded on first property access, allowing pure-JS modules
 * (schema, columns, table) to be imported without requiring the native binary.
 */
export const native = new Proxy({}, {
    get(_target, prop) {
        if (typeof prop === "symbol") {
            return undefined;
        }
        const binding = getNative();
        const value = binding[prop];
        // Bind functions so they work correctly when called detached
        if (typeof value === "function") {
            return value.bind(binding);
        }
        return value;
    }
});
//# sourceMappingURL=index.js.map