// prettier-ignore
/* eslint-disable */
// @ts-nocheck
/* auto-generated by NAPI-RS */

const { createRequire } = require('node:module')
require = createRequire(__filename)

const { readFileSync } = require('node:fs')
let nativeBinding = null
const loadErrors = []

const isMusl = () => {
  let musl = false
  if (process.platform === 'linux') {
    musl = isMuslFromFilesystem()
    if (musl === null) {
      musl = isMuslFromReport()
    }
    if (musl === null) {
      musl = isMuslFromChildProcess()
    }
  }
  return musl
}

const isFileMusl = (f) => f.includes('libc.musl-') || f.includes('ld-musl-')

const isMuslFromFilesystem = () => {
  try {
    return readFileSync('/usr/bin/ldd', 'utf-8').includes('musl')
  } catch {
    return null
  }
}

const isMuslFromReport = () => {
  let report = null
  if (typeof process.report?.getReport === 'function') {
    process.report.excludeNetwork = true
    report = process.report.getReport()
  }
  if (!report) {
    return null
  }
  if (report.header && report.header.glibcVersionRuntime) {
    return false
  }
  if (Array.isArray(report.sharedObjects)) {
    if (report.sharedObjects.some(isFileMusl)) {
      return true
    }
  }
  return false
}

const isMuslFromChildProcess = () => {
  try {
    return require('child_process').execSync('ldd --version', { encoding: 'utf8' }).includes('musl')
  } catch (e) {
    // If we reach this case, we don't know if the system is musl or not, so is better to just fallback to false
    return false
  }
}

function requireNative() {
  if (process.env.NAPI_RS_NATIVE_LIBRARY_PATH) {
    try {
      nativeBinding = require(process.env.NAPI_RS_NATIVE_LIBRARY_PATH);
    } catch (err) {
      loadErrors.push(err)
    }
  } else if (process.platform === 'android') {
    if (process.arch === 'arm64') {
      try {
        return require('./index.android-arm64.node')
      } catch (e) {
        loadErrors.push(e)
      }
      try {
        return require('qpace-android-arm64')
      } catch (e) {
        loadErrors.push(e)
      }

    } else if (process.arch === 'arm') {
      try {
        return require('./index.android-arm-eabi.node')
      } catch (e) {
        loadErrors.push(e)
      }
      try {
        return require('qpace-android-arm-eabi')
      } catch (e) {
        loadErrors.push(e)
      }

    } else {
      loadErrors.push(new Error(`Unsupported architecture on Android ${process.arch}`))
    }
  } else if (process.platform === 'win32') {
    if (process.arch === 'x64') {
      try {
        return require('./index.win32-x64-msvc.node')
      } catch (e) {
        loadErrors.push(e)
      }
      try {
        return require('qpace-win32-x64-msvc')
      } catch (e) {
        loadErrors.push(e)
      }

    } else if (process.arch === 'ia32') {
      try {
        return require('./index.win32-ia32-msvc.node')
      } catch (e) {
        loadErrors.push(e)
      }
      try {
        return require('qpace-win32-ia32-msvc')
      } catch (e) {
        loadErrors.push(e)
      }

    } else if (process.arch === 'arm64') {
      try {
        return require('./index.win32-arm64-msvc.node')
      } catch (e) {
        loadErrors.push(e)
      }
      try {
        return require('qpace-win32-arm64-msvc')
      } catch (e) {
        loadErrors.push(e)
      }

    } else {
      loadErrors.push(new Error(`Unsupported architecture on Windows: ${process.arch}`))
    }
  } else if (process.platform === 'darwin') {
    try {
        return require('./index.darwin-universal.node')
      } catch (e) {
        loadErrors.push(e)
      }
      try {
        return require('qpace-darwin-universal')
      } catch (e) {
        loadErrors.push(e)
      }

    if (process.arch === 'x64') {
      try {
        return require('./index.darwin-x64.node')
      } catch (e) {
        loadErrors.push(e)
      }
      try {
        return require('qpace-darwin-x64')
      } catch (e) {
        loadErrors.push(e)
      }

    } else if (process.arch === 'arm64') {
      try {
        return require('./index.darwin-arm64.node')
      } catch (e) {
        loadErrors.push(e)
      }
      try {
        return require('qpace-darwin-arm64')
      } catch (e) {
        loadErrors.push(e)
      }

    } else {
      loadErrors.push(new Error(`Unsupported architecture on macOS: ${process.arch}`))
    }
  } else if (process.platform === 'freebsd') {
    if (process.arch === 'x64') {
      try {
        return require('./index.freebsd-x64.node')
      } catch (e) {
        loadErrors.push(e)
      }
      try {
        return require('qpace-freebsd-x64')
      } catch (e) {
        loadErrors.push(e)
      }

    } else if (process.arch === 'arm64') {
      try {
        return require('./index.freebsd-arm64.node')
      } catch (e) {
        loadErrors.push(e)
      }
      try {
        return require('qpace-freebsd-arm64')
      } catch (e) {
        loadErrors.push(e)
      }

    } else {
      loadErrors.push(new Error(`Unsupported architecture on FreeBSD: ${process.arch}`))
    }
  } else if (process.platform === 'linux') {
    if (process.arch === 'x64') {
      if (isMusl()) {
        try {
        return require('./index.linux-x64-musl.node')
      } catch (e) {
        loadErrors.push(e)
      }
      try {
        return require('qpace-linux-x64-musl')
      } catch (e) {
        loadErrors.push(e)
      }

      } else {
        try {
        return require('./index.linux-x64-gnu.node')
      } catch (e) {
        loadErrors.push(e)
      }
      try {
        return require('qpace-linux-x64-gnu')
      } catch (e) {
        loadErrors.push(e)
      }

      }
    } else if (process.arch === 'arm64') {
      if (isMusl()) {
        try {
        return require('./index.linux-arm64-musl.node')
      } catch (e) {
        loadErrors.push(e)
      }
      try {
        return require('qpace-linux-arm64-musl')
      } catch (e) {
        loadErrors.push(e)
      }

      } else {
        try {
        return require('./index.linux-arm64-gnu.node')
      } catch (e) {
        loadErrors.push(e)
      }
      try {
        return require('qpace-linux-arm64-gnu')
      } catch (e) {
        loadErrors.push(e)
      }

      }
    } else if (process.arch === 'arm') {
      if (isMusl()) {
        try {
        return require('./index.linux-arm-musleabihf.node')
      } catch (e) {
        loadErrors.push(e)
      }
      try {
        return require('qpace-linux-arm-musleabihf')
      } catch (e) {
        loadErrors.push(e)
      }

      } else {
        try {
        return require('./index.linux-arm-gnueabihf.node')
      } catch (e) {
        loadErrors.push(e)
      }
      try {
        return require('qpace-linux-arm-gnueabihf')
      } catch (e) {
        loadErrors.push(e)
      }

      }
    } else if (process.arch === 'riscv64') {
      if (isMusl()) {
        try {
        return require('./index.linux-riscv64-musl.node')
      } catch (e) {
        loadErrors.push(e)
      }
      try {
        return require('qpace-linux-riscv64-musl')
      } catch (e) {
        loadErrors.push(e)
      }

      } else {
        try {
        return require('./index.linux-riscv64-gnu.node')
      } catch (e) {
        loadErrors.push(e)
      }
      try {
        return require('qpace-linux-riscv64-gnu')
      } catch (e) {
        loadErrors.push(e)
      }

      }
    } else if (process.arch === 'ppc64') {
      try {
        return require('./index.linux-ppc64-gnu.node')
      } catch (e) {
        loadErrors.push(e)
      }
      try {
        return require('qpace-linux-ppc64-gnu')
      } catch (e) {
        loadErrors.push(e)
      }

    } else if (process.arch === 's390x') {
      try {
        return require('./index.linux-s390x-gnu.node')
      } catch (e) {
        loadErrors.push(e)
      }
      try {
        return require('qpace-linux-s390x-gnu')
      } catch (e) {
        loadErrors.push(e)
      }

    } else {
      loadErrors.push(new Error(`Unsupported architecture on Linux: ${process.arch}`))
    }
  } else {
    loadErrors.push(new Error(`Unsupported OS: ${process.platform}, architecture: ${process.arch}`))
  }
}

nativeBinding = requireNative()

if (!nativeBinding || process.env.NAPI_RS_FORCE_WASI) {
  try {
    nativeBinding = require('./index.wasi.cjs')
  } catch (err) {
    if (process.env.NAPI_RS_FORCE_WASI) {
      loadErrors.push(err)
    }
  }
  if (!nativeBinding) {
    try {
      nativeBinding = require('qpace-wasm32-wasi')
    } catch (err) {
      if (process.env.NAPI_RS_FORCE_WASI) {
        loadErrors.push(err)
      }
    }
  }
}

if (!nativeBinding) {
  if (loadErrors.length > 0) {
    throw new Error(
      `Cannot find native binding. ` +
        `npm has a bug related to optional dependencies (https://github.com/npm/cli/issues/4828). ` +
        'Please try `npm i` again after removing both package-lock.json and node_modules directory.',
      { cause: loadErrors }
    )
  }
  throw new Error(`Failed to load native binding`)
}

module.exports = nativeBinding
module.exports.NodeBacktest = nativeBinding.NodeBacktest
module.exports.NodeCtx = nativeBinding.NodeCtx
module.exports.NodeCtxSkip = nativeBinding.NodeCtxSkip
module.exports.NodeOhlcv = nativeBinding.NodeOhlcv
module.exports.NodeOhlcvBar = nativeBinding.NodeOhlcvBar
module.exports.NodeSignal = nativeBinding.NodeSignal
module.exports.NodeSym = nativeBinding.NodeSym
module.exports.NodeSymKind = nativeBinding.NodeSymKind
module.exports.NodeTimeframe = nativeBinding.NodeTimeframe
module.exports.NodeTrade = nativeBinding.NodeTrade
module.exports.NodeTradeDirection = nativeBinding.NodeTradeDirection
module.exports.NodeTradeEvent = nativeBinding.NodeTradeEvent
module.exports._getCoreVersion = nativeBinding._getCoreVersion
module.exports.accuracy = nativeBinding.accuracy
module.exports.annualization_factor = nativeBinding.annualization_factor
module.exports.avgLosingTrade = nativeBinding.avgLosingTrade
module.exports.avgTrade = nativeBinding.avgTrade
module.exports.avgWinLossRatio = nativeBinding.avgWinLossRatio
module.exports.avgWinningTrade = nativeBinding.avgWinningTrade
module.exports.expectancy = nativeBinding.expectancy
module.exports.expectancyScore = nativeBinding.expectancyScore
module.exports.f1 = nativeBinding.f1
module.exports.grossLossPct = nativeBinding.grossLossPct
module.exports.grossProfitPct = nativeBinding.grossProfitPct
module.exports.longNetProfitPct = nativeBinding.longNetProfitPct
module.exports.longNetProfitRatio = nativeBinding.longNetProfitRatio
module.exports.mean = nativeBinding.mean
module.exports.netProfitPct = nativeBinding.netProfitPct
module.exports.omegaRatio = nativeBinding.omegaRatio
module.exports.omegaRatioFromReturns = nativeBinding.omegaRatioFromReturns
module.exports.orderSize = nativeBinding.orderSize
module.exports.orderSizeForEquityPct = nativeBinding.orderSizeForEquityPct
module.exports.pnl = nativeBinding.pnl
module.exports.precision = nativeBinding.precision
module.exports.profitFactor = nativeBinding.profitFactor
module.exports.recall = nativeBinding.recall
module.exports.returns = nativeBinding.returns
module.exports.roundContracts = nativeBinding.roundContracts
module.exports.roundToMinTick = nativeBinding.roundToMinTick
module.exports.sharpeRatio = nativeBinding.sharpeRatio
module.exports.sharpeRatioFromReturns = nativeBinding.sharpeRatioFromReturns
module.exports.shortNetProfitPct = nativeBinding.shortNetProfitPct
module.exports.sortinoRatio = nativeBinding.sortinoRatio
module.exports.sortinoRatioFromReturns = nativeBinding.sortinoRatioFromReturns
module.exports.stdev = nativeBinding.stdev
module.exports.sum = nativeBinding.sum
module.exports.validateContracts = nativeBinding.validateContracts
module.exports.variance = nativeBinding.variance
module.exports.winRate = nativeBinding.winRate
module.exports.zipOhlcvBars = nativeBinding.zipOhlcvBars
