/**
 * Platform-specific native addon loader.
 *
 * Resolves the correct .node binary for the current OS/arch.
 * Platform packages are installed as optionalDependencies.
 */

'use strict';

const { platform, arch } = process;
const path = require('path');

const platformMap = {
  'darwin-arm64': 'lastid-sdk-darwin-arm64',
  'darwin-x64': 'lastid-sdk-darwin-x64',
  'linux-x64': 'lastid-sdk-linux-x64-gnu',
  'linux-arm64': 'lastid-sdk-linux-arm64-gnu',
  'win32-x64': 'lastid-sdk-win32-x64-msvc',
};

const key = `${platform}-${arch}`;
const pkgName = platformMap[key];

let nativeBinding = null;

if (pkgName) {
  // Try loading from npm optionalDependency first
  try {
    nativeBinding = require(`@lastid/${pkgName}`);
  } catch {
    // Not installed as dependency — try local platforms dir (dev mode)
    try {
      nativeBinding = require(path.join(__dirname, 'platforms', pkgName, 'lastid_sdk.node'));
    } catch {
      nativeBinding = null;
    }
  }
}

module.exports = nativeBinding;
