import { Injectable } from '@angular/core';
// import { TranslateService } from '@ngx-translate/core';
import Parse from "parse";

@Injectable({
  providedIn: 'root'
})
export class ParseDataService {

  /**
   * 数据源内存缓存
   */
  ParseObjectCacheMap:{
    [key:string]:Parse.Object
  } = {}
  async cachePointerList(pointers:Array<Parse.Pointer|any>):Promise<any>{
    let plist = pointers.map(pointer=>{
      return new Promise(async (resolve)=>{
        let id = pointer?.objectId || pointer?.id
        let query = new Parse.Query(pointer?.className);
        try{
          this.ParseObjectCacheMap[id] = await query.get(id);
        }catch(err){
        }
        resolve(true)
      })
    })
    await Promise.all(plist);
    console.log(this.ParseObjectCacheMap)
    return true
  }
  async showNameByObj(obj:Parse.Object,showName:string="${name}"){
    if(!obj?.id) return '无'
    let text = showName;
    let regex = /\${([^}]+)}/g;
    let matches = [];
    let match;
    while ((match = regex.exec(text)) !== null) {
      matches.push(match[1]);
    }

    // 获取模板标记数组
    // console.log(matches);
    matches.forEach(keyword=>{
      text = text.replaceAll("${"+keyword+"}",obj?.get(keyword)||"")
    })
    // console.log(text)

    // 特殊数据名称设定
    // if(obj.className=="MinerCluster"){
    //   let cname = obj?.get("parent")?.get("name") || ""
    //   let uname = await this.getTrans( `${obj?.get("index")==1?'A单元':'B单元'}`);
    //   text = cname + uname;
    //   if(!obj?.get("index")){
    //     return obj.get("cluster")?.get("name") || obj.get("name") // 非A/B单元，直接显示名称
    //   }
    // }
    return text
  }
  constructor(
    // private translate:TranslateService
  ) { }
  // getTrans(key:any){
  //   return new Promise(resolve=>{
  //     this.translate.get(key).subscribe(data=>{
  //       resolve(data)
  //     })
  //   })
  // }
}
