"use strict";
/**
 * PKCS#11 middleware detector.
 *
 * Scans the filesystem for known PKCS#11 middleware libraries
 * used by Brazilian certificate providers.
 *
 * Paths ported from sign-docs/lib/services/token_server/middleware_detector.dart
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.detectMiddleware = detectMiddleware;
exports.getInstalledMiddleware = getInstalledMiddleware;
const fs_1 = require("fs");
const os_1 = require("os");
const WINDOWS_MIDDLEWARE = {
    'SafeSign': String.raw `C:\Windows\System32\aetpkss1.dll`,
    'SafeSign (64-bit)': String.raw `C:\Windows\SysWOW64\aetpkss1.dll`,
    'Watchdata': String.raw `C:\Windows\System32\watchdata.dll`,
    'Watchdata (WD)': String.raw `C:\Windows\System32\wdpkcs.dll`,
    'Gemalto': String.raw `C:\Windows\System32\gclib.dll`,
    'Thales': String.raw `C:\Windows\System32\eTPKCS11.dll`,
    'Oberthur': String.raw `C:\Windows\System32\OcsCryptoki.dll`,
    'IDEMIA': String.raw `C:\Windows\System32\idprimepkcs11.dll`,
    'ePass2003': String.raw `C:\Windows\System32\eps2003csp11.dll`,
    'ePass3003': String.raw `C:\Windows\System32\ShuttleCsp11_3003.dll`,
    'Certisign': String.raw `C:\Windows\System32\csp11.dll`,
    'Serasa': String.raw `C:\Windows\System32\acospkcs11.dll`,
    'Valid': String.raw `C:\Windows\System32\cvP11.dll`,
    'OpenSC': String.raw `C:\Windows\System32\opensc-pkcs11.dll`,
};
const LINUX_MIDDLEWARE = {
    'SafeSign': '/usr/lib/libaetpkss.so',
    'SafeSign (x86_64)': '/usr/lib/x86_64-linux-gnu/libaetpkss.so',
    'SafeSign (i386)': '/usr/lib/i386-linux-gnu/libaetpkss.so',
    'SafeSign (lib64)': '/usr/lib64/libaetpkss.so',
    'Watchdata': '/usr/lib/libwdpkcs.so',
    'Watchdata (WatchKey)': '/usr/lib/watchkey/libwdpkcs.so',
    'Watchdata (lib64)': '/usr/lib64/libwdpkcs.so',
    'Gemalto': '/usr/lib/libgclib.so',
    'Thales': '/usr/lib/libeTPkcs11.so',
    'Gemalto (lib64)': '/usr/lib64/libgclib.so',
    'Thales (lib64)': '/usr/lib64/libeTPkcs11.so',
    'Oberthur': '/usr/lib/libOcsCryptoki.so',
    'IDEMIA': '/usr/lib/libidprimepkcs11.so',
    'Oberthur (lib64)': '/usr/lib64/libOcsCryptoki.so',
    'IDEMIA (lib64)': '/usr/lib64/libidprimepkcs11.so',
    'ePass2003': '/usr/lib/libeps2003csp11.so',
    'ePass2003 (lib64)': '/usr/lib64/libeps2003csp11.so',
    'OpenSC': '/usr/lib/opensc-pkcs11.so',
    'OpenSC (x86_64)': '/usr/lib/x86_64-linux-gnu/opensc-pkcs11.so',
    'OpenSC (lib64)': '/usr/lib64/opensc-pkcs11.so',
    'OpenSC (lib64 pkcs11)': '/usr/lib64/pkcs11/opensc-pkcs11.so',
    'CoolKey': '/usr/lib/pkcs11/libcoolkeypk11.so',
    'CoolKey (lib64)': '/usr/lib64/pkcs11/libcoolkeypk11.so',
};
const MACOS_MIDDLEWARE = {
    'SafeSign': '/usr/local/lib/libaetpkss.dylib',
    'SafeSign (Framework)': '/Library/Frameworks/AETSafeSign.framework/Versions/Current/AETSafeSign',
    'Gemalto': '/usr/local/lib/libgclib.dylib',
    'Thales': '/usr/local/lib/libeTPkcs11.dylib',
    'ePass2003': '/usr/local/lib/libeps2003csp11.dylib',
    'OpenSC': '/usr/local/lib/opensc-pkcs11.so',
    'OpenSC (Homebrew)': '/opt/homebrew/lib/opensc-pkcs11.so',
};
function getKnownMiddleware() {
    const p = (0, os_1.platform)();
    if (p === 'win32')
        return WINDOWS_MIDDLEWARE;
    if (p === 'darwin')
        return MACOS_MIDDLEWARE;
    return LINUX_MIDDLEWARE;
}
/** Detect all known PKCS#11 middleware on the system. */
function detectMiddleware() {
    const known = getKnownMiddleware();
    return Object.entries(known).map(([name, libraryPath]) => ({
        name,
        libraryPath,
        isInstalled: (0, fs_1.existsSync)(libraryPath),
    }));
}
/** Get only installed middleware. */
function getInstalledMiddleware() {
    return detectMiddleware().filter(m => m.isInstalled);
}
