"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _asyncStorage = _interopRequireDefault(require("@react-native-async-storage/async-storage"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
class AdAssetCache {
  static #instance;
  storageKey = 'amity.adAssetCache';
  pendingRequests = {};
  memoryCache = {};
  initialized = false;
  async initialize() {
    if (this.initialized) return;
    try {
      const data = await _asyncStorage.default.getItem(this.storageKey);
      if (data) {
        const assets = JSON.parse(data);
        assets.forEach(asset => {
          this.memoryCache[asset.fileUrl] = asset;
        });
      }
      this.initialized = true;
    } catch (error) {
      console.warn('Error initializing AdAssetCache:', error);
    }
  }
  static get instance() {
    if (!AdAssetCache.#instance) {
      AdAssetCache.#instance = new AdAssetCache();
      AdAssetCache.#instance.initialize();
    }
    return AdAssetCache.#instance;
  }

  // private async getAllAdAssetKey(): Promise<string[]> {
  //   const allKeys = await AsyncStorage.getAllKeys();
  //   return allKeys.filter((key) => key.startsWith(AdAssetCache.#adAssetPrefix));
  // }

  async getAllAdAssets() {
    try {
      if (!this.initialized) {
        await this.initialize();
      }

      // Return values from memory cache (much faster)
      return Object.values(this.memoryCache);
    } catch (error) {
      console.warn('Error getting all ad assets:', error);
      return [];
    }
  }
  async getAdAsset(fileUrl) {
    // Check memory cache first
    if (this.memoryCache[fileUrl]) {
      return this.memoryCache[fileUrl];
    }

    // Check if there's already a pending request for this asset
    if (this.pendingRequests[fileUrl]) {
      return this.pendingRequests[fileUrl];
    }

    // Make sure we're initialized
    if (!this.initialized) {
      await this.initialize();

      // Check again after initialization
      if (this.memoryCache[fileUrl]) {
        return this.memoryCache[fileUrl];
      }
    }

    // Create a new request promise
    this.pendingRequests[fileUrl] = this._fetchAdAsset(fileUrl);
    try {
      const result = await this.pendingRequests[fileUrl];
      return result;
    } finally {
      // Clean up the pending request
      delete this.pendingRequests[fileUrl];
    }
  }
  async _fetchAdAsset(fileUrl) {
    try {
      // Get all assets (optimizes by batching reads)
      const assets = await this.getAllAdAssets();

      // Find matching asset
      const asset = assets.find(asset => asset.fileUrl === fileUrl);
      if (asset) {
        // Cache it in memory
        this.memoryCache[fileUrl] = asset;
        return asset;
      }

      // Default asset if not found
      const defaultAsset = {
        fileUrl,
        downloadStatus: -1,
        downloadId: null
      };
      this.memoryCache[fileUrl] = defaultAsset;
      return defaultAsset;
    } catch (error) {
      console.warn('Error fetching ad asset:', error);

      // Return default on error
      const defaultAsset = {
        fileUrl,
        downloadStatus: -1,
        downloadId: null
      };
      return defaultAsset;
    }
  }
  async insertAdAsset(asset) {
    // Update memory cache immediately
    this.memoryCache[asset.fileUrl] = asset;
    try {
      // Update persistent storage in the background
      this._saveToStorage();
    } catch (error) {
      console.warn('Error inserting ad asset:', error);
    }
  }
  updateDownloadId(fileUrl, downloadId) {
    if (this.memoryCache[fileUrl]) {
      this.memoryCache[fileUrl].downloadId = downloadId;

      // Update storage in the background
      this._saveToStorage();
    }
  }
  updateDownloadStatus(fileUrl, status) {
    if (this.memoryCache[fileUrl]) {
      this.memoryCache[fileUrl].downloadStatus = status;

      // Update storage in the background
      this._saveToStorage();
    }
  }
  deleteAdAsset(fileUrl) {
    // Remove from memory cache
    delete this.memoryCache[fileUrl];

    // Update storage in the background
    this._saveToStorage();
  }
  async deleteAll() {
    // Clear memory cache
    this.memoryCache = {};

    // Clear storage
    try {
      await _asyncStorage.default.removeItem(this.storageKey);
    } catch (error) {
      console.warn('Error clearing ad asset cache:', error);
    }
  }
  _saveTimeout = null;
  _saveToStorage() {
    if (this._saveTimeout) {
      clearTimeout(this._saveTimeout);
    }
    this._saveTimeout = setTimeout(async () => {
      try {
        const assets = Object.values(this.memoryCache);
        await _asyncStorage.default.setItem(this.storageKey, JSON.stringify(assets));
      } catch (error) {
        console.warn('Error saving ad assets to storage:', error);
      }
    }, 300); // 300ms debounce
  }
}
var _default = exports.default = AdAssetCache;
//# sourceMappingURL=AdAssetCache.js.map