import { Pipe, PipeTransform } from '@angular/core';
import { StringUtil } from '../utils/string.util';
import moment from 'moment';

@Pipe({
  name: 'dateToStringChatList',
})
export class DateToStringForChatRoomListPipe implements PipeTransform {
  transform(value: any): string {
    const curDate = new Date();
    const yesterDate = new Date(curDate.getTime() - 1 * 24 * 60 * 60 * 1000);
    let date: Date;
    if (typeof value === 'string') {
      date = moment(value.toString()).toDate();
    } else if (value instanceof Date) {
      date = value;
    } else {
      return value;
    }

    if (
      curDate.getFullYear() === date.getFullYear() &&
      curDate.getMonth() === date.getMonth() &&
      curDate.getDate() === date.getDate()
    ) {
      // 당일
      return StringUtil.dateFormat(date, 'a/p HH:mm');
    } else if (
      yesterDate.getFullYear() === date.getFullYear() &&
      yesterDate.getMonth() === date.getMonth() &&
      yesterDate.getDate() === date.getDate()
    ) {
      // 어제
      return '어제';
    } else {
      return StringUtil.dateFormat(date, 'MM.dd');
    }
  }
}

@Pipe({
  name: 'dateToStringFormat',
})
export class DateToStringFormatPipe implements PipeTransform {
  transform(value: any, format?: string): string {
    const date = moment(value.toString()).toDate();

    if (!!format) {
      return StringUtil.dateFormat(date, format);
    } else {
      return StringUtil.dateFormat(date, 'YYYY.MM.DD');
    }
  }
}
