/**
 * @file Handler file for choosing the correct version of ONNX Runtime, based on the environment.
 *
 * This module is not directly exported, but can be accessed through the environment variables:
 * ```javascript
 * import { env } from '@xenova/transformers';
 * console.log(env.backends.onnx);
 * ```
 *
 * @module backends/onnx
 */

import * as ONNX_target from 'onnxruntime-selector';

/** @type {import('onnxruntime-web')} The ONNX runtime module. */
export const ONNX = ONNX_target.default ?? ONNX_target;

export const executionProviders = ['wasm'];

if (ONNX_target.target === 'node') {
  executionProviders.unshift('cpu');
} else {
  // SIMD for WebAssembly does not operate correctly in some recent versions of iOS (16.4.x).
  // As a temporary fix, we disable it for now.
  // For more information, see: https://github.com/microsoft/onnxruntime/issues/15644
  const isIOS =
    typeof navigator !== 'undefined' &&
    /iP(hone|od|ad).+16_4.+AppleWebKit/.test(navigator.userAgent);
  if (isIOS) {
    ONNX.env.wasm.simd = false;
  }
}
