"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.OnnxSession = exports.OnnxInferenceService = void 0;
var _fileUtils = require("../utils/fileUtils.js");
var _tensorUtils = require("../utils/tensorUtils.js");
class OnnxSession {
  constructor(api, info) {
    this.api = api;
    this.sessionId = info.sessionId;
    this.inputNames = info.inputNames;
    this.outputNames = info.outputNames;
    this.inputTypes = info.inputTypes ?? [];
    this.outputTypes = info.outputTypes ?? [];
  }
  async run(feeds) {
    // Convert TypedArray feeds to OnnxTensorData (base64) for the bridge
    const inputs = {};
    for (const [name, tensor] of Object.entries(feeds)) {
      inputs[name] = {
        type: tensor.type,
        dims: tensor.dims,
        data: (0, _tensorUtils.typedArrayToBase64)(tensor.data)
      };
    }
    const result = await this.api.runOnnxSession(this.sessionId, inputs);
    if (!result.success || !result.outputs) {
      throw new Error(result.error || 'ONNX inference failed');
    }

    // Convert output base64 back to TypedArrays
    const outputs = {};
    for (const [name, tensorData] of Object.entries(result.outputs)) {
      outputs[name] = {
        type: tensorData.type,
        data: (0, _tensorUtils.base64ToTypedArray)(tensorData.data, tensorData.type),
        dims: tensorData.dims
      };
    }
    return outputs;
  }
  async release() {
    await this.api.releaseOnnxSession(this.sessionId);
  }
}
exports.OnnxSession = OnnxSession;
class OnnxInferenceService {
  sessions = new Set();
  constructor(api) {
    this.api = api;
  }
  validateLibrary() {
    return this.api.validateLibraryLoaded();
  }
  async createSession(config) {
    const nativeConfig = {
      modelPath: (0, _fileUtils.cleanFilePath)(config.modelPath),
      numThreads: config.numThreads
    };
    const result = await this.api.createOnnxSession(nativeConfig);
    if (!result.success) {
      throw new Error(result.error || 'Failed to create ONNX session');
    }
    this.sessions.add(result.sessionId);
    return new OnnxSession(this.api, result);
  }
  async run(sessionId, inputs) {
    try {
      return await this.api.runOnnxSession(sessionId, inputs);
    } catch (error) {
      return {
        success: false,
        error: error instanceof Error ? error.message : String(error)
      };
    }
  }
  async releaseSession(sessionId) {
    try {
      const result = await this.api.releaseOnnxSession(sessionId);
      if (result.released) {
        this.sessions.delete(sessionId);
      }
      return result;
    } catch (error) {
      console.error('Error releasing ONNX session:', error);
      return {
        released: false
      };
    }
  }
  async releaseAll() {
    const sessionIds = Array.from(this.sessions);
    for (const sessionId of sessionIds) {
      await this.releaseSession(sessionId);
    }
  }
}
exports.OnnxInferenceService = OnnxInferenceService;
//# sourceMappingURL=OnnxInferenceService.js.map