import {dayjs} from '@/plugin/index'
import * as R from 'ramda'

// 店铺营业时间格式化
export function businessTimeFormat(weekNums: string, timePeriod: string) {
    if (!weekNums || !timePeriod) {
        return ''
    }
    if (!weekNums.length || !timePeriod.length) {
        return ''
    }
    let timeString = ''
    const weeks: string[] = dayjs.weekdays()
    console.log('weeks', weeks)
    const weekNumsArr = R.map(item => +item, R.split(',', R.trim(weekNums || '')))
    const groupWeek = R.groupWith((a, b) => +a + 1 === +b, weekNumsArr)
    console.log('weekNums', groupWeek)
    R.forEach((weekNumsArr) => {
        if (R.gt(weekNumsArr.length, 1)) {
            timeString += `${weeks[weekNumsArr[0] - 1]}-${weeks[weekNumsArr[weekNumsArr.length - 1] - 1]} `
        } else {
            timeString += `${weeks[weekNumsArr[0] - 1]} `
        }
    }, groupWeek)

    const groupTimePeriod = R.map(item => R.map(time => time + ':00', item.split('-')), R.split(',', R.trim(timePeriod || '')))
    console.log('timePeriodArr', groupTimePeriod)
    R.forEach((timePeriodArr) => {
        timeString += `${timePeriodArr[0]}-${timePeriodArr[1]} `
    }, groupTimePeriod)
    return timeString
}


// 转换手机号码中间四位为*
export function isPhone(mobile: string) {
    const regex = /^1[3-9]\d{9}$/;
    const regex1 = /^[1-9]\d{7}$/
    return regex.test(mobile) || regex1.test(mobile);
}


/**
 * 验证邮箱
 */
export function isEmail(email: string) {
    const reg = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
    return reg.test(email)
}


// 转换手机号码中间四位为*
export function conversionMobile(mobile: string) {
    if (!mobile) {
        return
    }
    return mobile.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2')
}

