/**
 * Native bindings loader
 *
 * This module loads the native C++ bindings compiled by node-gyp.
 * All native classes are accessed through this single entry point.
 */
import { existsSync } from 'node:fs';
import { createRequire } from 'node:module';
import { resolve } from 'node:path';
import { getDirname } from '../utils/electron.js';
const __dirname = getDirname(import.meta.url);
/**
 * Load the native binding
 *
 * @returns The loaded native binding interface
 *
 * @internal
 */
function loadBinding() {
    const require = createRequire(import.meta.url);
    const errors = [];
    // Detect platform
    const platform = process.platform;
    const arch = process.arch;
    const platformArch = `${platform}-${arch}`;
    const loadLocal = process.env.AV_FROM_SOURCE === '1';
    // Local build directory (--build-from-source)
    try {
        const releasePath = resolve(__dirname, '..', '..', 'build', 'Release', 'node-av.node');
        const binaryPath = resolve(__dirname, '..', '..', 'binary', 'node-av.node');
        const rootPath = resolve(__dirname, '..', '..', 'node-av.node');
        const localPath = [releasePath, binaryPath, rootPath];
        for (const path of localPath) {
            if (existsSync(path)) {
                return require(path);
            }
        }
    }
    catch (err) {
        errors.push(new Error(`Local build loading failed: ${err}`));
    }
    if (!loadLocal) {
        try {
            const packageName = `@revizly/node-av-${platformArch}`;
            return require(`${packageName}/node-av.node`);
        }
        catch (err) {
            errors.push(new Error(`Platform package not found or loading failed: ${err}`));
        }
    }
    // All attempts failed
    const errorMessages = errors.map((e) => e.message).join('\n  ');
    // prettier-ignore
    throw new Error(`Could not load the node-av native binding for ${platformArch}.\n` +
        `Errors:\n  ${errorMessages}\n\n`);
}
// Load the native binding with fallback logic
const bindings = loadBinding();
export { bindings };
//# sourceMappingURL=binding.js.map