import dayjs from 'dayjs';

var days_ZH = ['星期日','星期一','星期二','星期三','星期四','星期五','星期六'];
var days_CN = ['星期日','星期一','星期二','星期三','星期四','星期五','星期六'];

export const datetimeStr = (date) =>{
    
    return dateFormat(date,"DD/MM/YYYY HH:mm:ss")
};

export const dateStr = (date) =>{
    return dateFormat(date,"DD/MM/YYYY")
};

export const dateValue = (date) =>{
    return dateFormat(date,"YYYY-MM-DD")
};


export const dateStr_Year = (date) =>{
    return dateFormat(date,'YYYY')
};

export const datetimeStr_Cht = (date) =>{ 
    return dateFormat(date,"YYYY年MM月DD日 HH:mm:ss")
};

export const dateStr_Cht = (date) =>{
    return dateFormat(date,"YYYY年MM月DD日")
};

export const dateStr_Eng = (date) =>{
    return dateFormat(date,"DD MMM YYYY")
};

export const datetimeFieldFormat = (date) =>{
    return dateFormat(date,"YYYY-MM-DDTHH:mm");
};

export const convertToDate = (date)=>{
    if(typeof date == 'number'){
        return dayjs(date).toDate();
    }else if(Array.isArray(date)){
        // if(date.length==3){
        //     return new Date(date[0],date[1]-1,date[2],0,0,0);
        // }else if(date.length==4){
        //     return new Date(date[0],date[1]-1,date[2],0,0,0);
        // }
        return new Date(date[0]
            ,date[1]-1
            ,date[2]
            ,date.length>=4?date[3]:0
            ,date.length>=5?date[4]:0
            ,date.length>=6?date[5]:0);
    }else if(typeof date == "string"){
        return new Date(date);
    }
    
    return date;
}

export const formatDateForLocale = (date, intl, locale) => {
  const fmt = intl.formatMessage({ id: "paymentMethodDatetimeStrFormat" });
  let s = dateFormat(date, fmt) || "";

  return locale === "en"
    ? s.replace("am", "a.m.").replace("pm", "p.m.")
    : s.replace("am", "上午").replace("pm", "下午").replace("00分", "");
};

export const is18_00 = (date) => {
  const d = convertToDate(date);
  if (!d) return false;
  return d.getHours() === 18 && d.getMinutes() === 0;
};

export const dateFormat = (date, format) =>{
    date = convertToDate(date);
    if(date) return dayjs(date).format(format);
    return "";
};

export const dateFormatWithFix = (date, format, hours, minutes) =>{
    date = convertToDate(date);
    date.setHours(hours);
    date.setMinutes(minutes);
    if(date) return dayjs(date).format(format);
    return "";
};

export const getWeekdayStr_ZH = (date) =>{
    date = convertToDate(date);
    if(date) return days_ZH[date.getDay()]
    return "";
};

export const getWeekdayStr_CN = (date) =>{
    date = convertToDate(date);
    if(date) return days_CN[date.getDay()]
    return "";
};
