import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'utilnow',
  standalone: true
})
export class UtilnowPipe implements PipeTransform {

  transform(value: Date, args?: any): any {
    let now = new Date();
    let time = now.getTime() - value.getTime();
    let seconds = time / 1000
    if(seconds<=60){
      return this.handleArgs(seconds.toFixed(0),"秒钟前",args)
    }
    let minutes = seconds / 60
    if(minutes < 60){
      return this.handleArgs(minutes.toFixed(0),"分钟前",args)
    }
    let hours = minutes / 60
    if(hours < 24){
      return this.handleArgs(hours.toFixed(0),"小时前",args)
    }
    let days = hours / 24
    if(days < 7){
      return this.handleArgs(days.toFixed(0),"天前",args)
    }

    let dateStr = `${value?.getFullYear()}-${value?.getMonth()+1}-${value?.getDate()}`
    return dateStr
  }

     // 时间标记
  enLocale:any = {
      "秒钟前":" seconds ago",
      "分钟前":" minutes ago",
      "小时前":" hours ago",
      "天前":" days ago",
  }
  handleArgs(value:any,unit:any,args:any):any{
    if(args=="en"){
      unit = this.enLocale[unit]
    }
    if(args?.constructor?.name == "TranslateService"){
      console.log(unit)
      if(args?.getDefaultLang()=="en"){
        unit = this.enLocale[unit]
      }
      // unit = await new Promise(resolve=>{
      //   args?.get(unit).subscribe(data=>{
      // console.log(data)
      // resolve(data)  
      //   })
      // })
    }
    if(args=="json"){
      return {unit,value}
    }else{
      return value+unit
    }

  }

}
