"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.minicoreStatus = void 0;
exports.getBinaryName = getBinaryName;
exports.getMinicoreStatus = getMinicoreStatus;
const logger_1 = __importDefault(require("../logger"));
const libc_details_1 = require("../telemetry/libc_details");
exports.minicoreStatus = {
    version: null,
    binaryName: null,
    errorType: null,
    errorDetails: null,
};
if (process.env.SNOWFLAKE_DISABLE_MINICORE) {
    exports.minicoreStatus.errorType = 'Minicore is disabled with SNOWFLAKE_DISABLE_MINICORE env variable';
}
else {
    try {
        exports.minicoreStatus.binaryName = getBinaryName();
        // eval('require') prevents bundlers (esbuild, webpack, etc.) from statically analyzing
        // and attempting to bundle .node native addon files
        // oxlint-disable-next-line no-eval
        const minicoreModule = eval('require')(`./binaries/${exports.minicoreStatus.binaryName}`);
        exports.minicoreStatus.version = minicoreModule.sfCoreFullVersion();
    }
    catch (error) {
        exports.minicoreStatus.errorType = isBinaryIgnoredByBundlers(error)
            ? 'Binary is missing from the bundle'
            : 'Failed to load binary';
        if (error instanceof Error) {
            exports.minicoreStatus.errorDetails = error;
        }
    }
}
// NOTE:
// Custom loader instead of napi-rs autogenerated binding file because:
// - napi-rs tries to require(process.env.NAPI_RS_NATIVE_LIBRARY_PATH) which might be a security risk
// - napi-rs tries to require('packageName-platform-arch') which we don't publish yet
function getBinaryName() {
    const { platform, arch } = process;
    const suffix = [platform, arch];
    if (platform === 'linux') {
        suffix.push((0, libc_details_1.getLibcDetails)().family === 'musl' ? 'musl' : 'gnu');
    }
    else if (platform === 'win32') {
        suffix.push('msvc');
    }
    return `sf_mini_core_0.0.1.${suffix.join('-')}.node`;
}
let isMinicoreStatusLogged = false;
function getMinicoreStatus() {
    // NOTE:
    // At the time of minicore loading, the logger isn't initialized, so we hold the error until
    // the first getMinicoreStatus() call
    if (!isMinicoreStatusLogged) {
        (0, logger_1.default)().debug('Minicore status: binaryName - %s, error - %s', exports.minicoreStatus.binaryName, exports.minicoreStatus.errorDetails ?? exports.minicoreStatus.errorType);
        isMinicoreStatusLogged = true;
    }
    return exports.minicoreStatus;
}
function isBinaryIgnoredByBundlers(error) {
    if (error instanceof Error) {
        const message = error.message;
        return (
        // eval('require') was ignored by bundlers
        message.startsWith('Cannot find module') ||
            // bundled code running in ESM environment
            message === 'require is not defined' ||
            // Windows: DLL mapped to a network path that the bundled app can't reach
            message === 'LoadLibrary failed: The network name cannot be found.' ||
            // Windows: DLL not found
            message === 'LoadLibrary failed: The specified module could not be found.');
    }
    else {
        return false;
    }
}
//# sourceMappingURL=minicore.js.map