import { formatDate } from '@/utils/assist.js'
import { switchWeight as computedWeight } from '@/utils'

// 过滤
const img120 = (src) => {
    return src + '?imageView2/1/w/120/h/120'
}

const img150 = (src) => {
    return src + '?imageView2/1/w/150/h/150'
}

const img200 = (src) => {
    return src + '?imageView2/1/w/200/h/200'
}

const img300 = (src) => {
    return src + '?imageView2/1/w/300/h/300'
}

const img150type2 = (src) => {
    return src + '?imageView2/2/w/150'
}

const img345type2 = (src) => {
    return src + '?imageView2/2/w/345'
}

const imgLowQ = (src) => {
    return src + '?imageView2/2/w/750/q/10'
}

const img750 = (src) => {
    return src + '?imageView2/1/w/750/h/750'
}

const img50 = (src) => {
    return src + '?imageView2/1/w/50/h/50'
}

const img750type2 = (src) => {
    return src + '?imageView2/2/w/750'
}

const imgSlim = (src) => {
    return src + '?imageSlim'
}

/**
 * http 转 https
 */
const urlToHttps = (url, type) => {
    if (!url) return ''
    let src = url && url.includes('https') ? url : url.replace('http', 'https')
    switch (type) {
        case 'img50':
            src = img50(src)
            break
        case 'img120':
            src = img120(src)
            break
        case 'img150':
            src = img150(src)
            break
        case 'img200':
            src = img200(src)
            break
        case 'img300':
            src = img300(src)
            break
        case 'img150type2':
            src = img150type2(src)
            break
        case 'imgLowQ':
            src = imgLowQ(src)
            break
        case 'img750':
            src = img750(src)
            break
        case 'img750type2':
            src = img750type2(src)
            break
        case 'imgSlim':
            src = imgSlim(src)
            break
        case 'img345type2':
            src = img345type2(src)
            break
    }
    return src
}

/**
 * 营业时间 前缀补0
 */
const processingHours = (num) => {
    let n = 4
    let str = (Array(n).join(0) + num).slice(-n)
    return str.slice(0, 2) + ':' + str.slice(2)
}

/**
 * 2023-08-16T14:30:34+08:00 日期转换 YYYY-MM-DD hh:mm:ss
 */
const dateToLLLL = (str) => {
    return formatDate(str, 'YYYY-MM-DD hh:mm:ss')
}

/**
 * 分 转 元 并保留两位数
 */
const toFixed2 = (num) => {
    if (num || num == 0) {
        return (num / 100).toFixed(2)
    } else {
        return '--'
    }
}

/**
 * 重量
 */
const switchWeight = (weight) => {
    return computedWeight(weight)
}

export {
    urlToHttps,         // 图片转换
    processingHours,    // 营业时间 前缀补0
    dateToLLLL,         // 日期转换
    toFixed2,           // 分 转 元
    switchWeight        // 重量转换
}
