'use strict';

const path = require('path');
const abiVersion = process.versions.modules;

/**
 * Try several known locations for the prebuilt .node binary:
 *  1. prebuilds/  directory shipped inside the npm package
 *  2. ../build/bindings/  directory (local CMake build output)
 */
const candidates = [
    path.join(__dirname, 'prebuilds', `authdb-nds.abi-${abiVersion}.node`),
    path.join(__dirname, '..', '..', 'build', 'bindings', `authdb-nds.abi-${abiVersion}.node`)
];

let binding = null;

for (const candidate of candidates) {
    try {
        binding = require(candidate);
        break;
    } catch (_) {
        // try next candidate
    }
}

if (!binding) {
    const searched = candidates.map(c => '  - ' + c).join('\n');
    throw new Error(
        `authdb-client: No prebuilt binary found for Node.js ABI ${abiVersion} ` +
        `(${process.platform} ${process.arch}).\n` +
        `Searched:\n${searched}\n\n` +
        `You may need to rebuild from source – see README.md for instructions.`
    );
}

module.exports = binding;
