import * as path from 'path';
import { resolveNativePlatformTarget } from './platform';
function getRequire() {
    return typeof require === 'function' ? require : undefined;
}
export function loadEvmNativeBindings() {
    const target = resolveNativePlatformTarget();
    if (!target)
        return undefined;
    const req = getRequire();
    if (!req)
        return undefined;
    const currentDir = typeof __dirname === 'string' ? __dirname : undefined;
    if (!currentDir)
        return undefined;
    const candidates = [
        path.resolve(currentDir, '../../native', target.filename),
        path.resolve(currentDir, '../../../native', target.filename),
        path.resolve(currentDir, '../../../dist/native', target.filename),
    ];
    for (const candidate of candidates) {
        try {
            return req(candidate);
        }
        catch (error) {
            const code = error?.code;
            if (code !== 'MODULE_NOT_FOUND' && code !== 'ERR_DLOPEN_FAILED') {
                throw error;
            }
        }
    }
    return undefined;
}
