// noPage

import { MarketAds_homeProperty } from 'esoftplay/cache/market/ads_home/import';
import { MarketProduct_list_searchProperty } from 'esoftplay/cache/market/product_list_search/import';


export default class m {
  static deleteAdsCache(): void {
    const date: any = new Date()
    const advertConfig = MarketAds_homeProperty.state().get()
    const adsCache = MarketProduct_list_searchProperty.getProductCache()
    const adsCacheMerchant = MarketProduct_list_searchProperty.getProductCacheMerchant()

    removeAds(adsCache, 'product')
    removeAds(adsCacheMerchant, 'merchant')

    function removeAds(result: string, type: 'product' | 'merchant'): void {
      const _delay = (type == 'product' ? advertConfig.ads_cache : advertConfig.ads_merchant_cache) * 1000

      let cache = JSON.parse(result)
      let _cache = Object.values(cache)
      let _key = Object.keys(cache)

      function deleteExpired(newCache: any, key: any, index: any) {
        let cache = newCache
        delete cache[key][index]
        //set new_cache

        if (type == "product") {
          MarketProduct_list_searchProperty.setProductCache(cache)
        } else {
          MarketProduct_list_searchProperty.setProductCacheMerchant(cache)
        }
      }

      _cache.map((item: any, i) => Object.values(item).map((x, y) => x)
        .map((item2: any, i2: number) => {
          if (Number(date - item2[2]) > _delay) {
            let index = item2[1] + '-' + item2[0]
            deleteExpired(cache, _key[i], index)
          }
        })
      )


    }
  }
}