"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.ArchiveService = void 0;
var FileSystem = _interopRequireWildcard(require("expo-file-system/legacy"));
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
/**
 * Type of model supported by Sherpa-onnx
 */

class ArchiveService {
  constructor(api) {
    this.baseDir = `${FileSystem.documentDirectory}sherpa-onnx/`;
    this.api = api;
  }

  /**
   * Ensure the base directory exists
   */
  async ensureBaseDir() {
    const dirInfo = await FileSystem.getInfoAsync(this.baseDir);
    if (!dirInfo.exists) {
      await FileSystem.makeDirectoryAsync(this.baseDir, {
        intermediates: true
      });
    }
  }

  /**
   * Get the path for a model
   * @param modelType Type of the model
   * @param modelName Name of the model
   * @returns Promise resolving to the model path
   */
  async getModelPath(modelType, modelName) {
    await this.ensureBaseDir();
    return `${this.baseDir}${modelType}/${modelName}`;
  }

  /**
   * Check if a model is downloaded
   * @param modelType Type of the model
   * @param modelName Name of the model
   * @returns Promise resolving to whether the model is downloaded
   */
  async isModelDownloaded(modelType, modelName) {
    const modelPath = await this.getModelPath(modelType, modelName);
    const modelInfo = await FileSystem.getInfoAsync(modelPath);
    return modelInfo.exists;
  }

  /**
   * Download a model
   * @param modelType Type of the model
   * @param modelName Name of the model
   * @param url URL to download from
   * @param onProgress Optional progress callback
   * @returns Promise resolving to the model path
   */
  async downloadModel(modelType, modelName, url, onProgress) {
    const modelPath = await this.getModelPath(modelType, modelName);
    const modelDir = modelPath.substring(0, modelPath.lastIndexOf('/'));
    await this.ensureBaseDir();
    const dirInfo = await FileSystem.getInfoAsync(modelDir);
    if (!dirInfo.exists) {
      await FileSystem.makeDirectoryAsync(modelDir, {
        intermediates: true
      });
    }
    const downloadResumable = FileSystem.createDownloadResumable(url, modelPath, {}, downloadProgress => {
      const progress = downloadProgress.totalBytesWritten / downloadProgress.totalBytesExpectedToWrite;
      onProgress?.(progress);
    });
    await downloadResumable.downloadAsync();
    return modelPath;
  }

  /**
   * Delete a model
   * @param modelType Type of the model
   * @param modelName Name of the model
   * @returns Promise resolving when deletion is complete
   */
  async deleteModel(modelType, modelName) {
    const modelPath = await this.getModelPath(modelType, modelName);
    const modelInfo = await FileSystem.getInfoAsync(modelPath);
    if (modelInfo.exists) {
      await FileSystem.deleteAsync(modelPath);
    }
  }

  /**
   * Get the size of a model
   * @param modelType Type of the model
   * @param modelName Name of the model
   * @returns Promise resolving to the model size in bytes
   */
  async getModelSize(modelType, modelName) {
    const modelPath = await this.getModelPath(modelType, modelName);
    const modelInfo = await FileSystem.getInfoAsync(modelPath);
    return modelInfo.exists ? modelInfo.size || 0 : 0;
  }

  /**
   * Extract a tar.bz2 file to a target directory
   * @param sourcePath Path to the tar.bz2 file
   * @param targetDir Directory to extract to
   * @returns Promise with extraction result
   */
  async extractTarBz2(sourcePath, targetDir) {
    try {
      return await this.api.extractTarBz2({
        sourcePath,
        targetDir
      });
    } catch (error) {
      console.error('Error extracting tar.bz2:', error);
      return {
        success: false,
        message: error instanceof Error ? error.message : String(error)
      };
    }
  }
}
exports.ArchiveService = ArchiveService;
//# sourceMappingURL=ArchiveService.js.map