// const images = import.meta.globEager(`/src/assets/*/**/*.{png,svg,jpg,gif}`);
const allDialog = import.meta.glob('/src/components/{Dialog,Popup}/*.vue');
export const loadModule = async (modulePath) => {
  try {
    return await allDialog[modulePath]();
  } catch (e) {
    throw new ImportError(`找不到模块路径 ${modulePath}`);
  }
};

export function isFunction(functionToCheck) {
  return functionToCheck && {}.toString.call(functionToCheck) === '[object Function]';
}

// 代理密码校验
export function testPassword(value, maxLength = 20) {
  const reg = new RegExp(`^[0-9a-zA-Z]{6,${maxLength}}$`);
  return reg.test(value);
}

export function shuffle(array) {
  let currentIndex = array.length;
  let randomIndex;

  // While there remain elements to shuffle...
  while (currentIndex !== 0) {
    // Pick a remaining element...
    randomIndex = Math.floor(Math.random() * currentIndex);
    currentIndex -= 1;

    // And swap it with the current element.
    // eslint-disable-next-line no-unexpected-multiline
    const current = array[currentIndex];
    array[currentIndex] = array[randomIndex];
    array[randomIndex] = current;
    // [array[currentIndex], array[randomIndex]] = [array[randomIndex], array[currentIndex]];
  }

  return array;
}

export function validateEmail(email) {
  const re = /^(([^<>()[\]\\.,;:\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 re.test(String(email).toLowerCase());
}

export const priceFormat = (price) => (price.toString().indexOf('.') !== -1
  ? `${price
    .toString()
    .split('.')[0]
    .replace(/(\d)(?=(?:\d{3})+$)/g, '$1,')}.00`
  : `${price.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,')}.00`);

export const timeFilter = (str) => str.substr(0, 10);

export function dateFormat(formatString, dateValue) {
  const date = new Date(dateValue);
  let fmt = formatString;
  let ret;
  const opt = {
    'Y+': date.getFullYear().toString(), // 年
    'm+': (date.getMonth() + 1).toString(), // 月
    'd+': date.getDate().toString(), // 日
    'H+': date.getHours().toString(), // 时
    'M+': date.getMinutes().toString(), // 分
    'S+': date.getSeconds().toString(), // 秒
    // 有其他格式化字符需求可以继续添加，必须转化成字符串
  };
  Object.keys(opt).forEach((k) => {
    ret = new RegExp(`(${k})`).exec(fmt);
    if (ret) {
      fmt = fmt.replace(
        ret[1],
        ret[1].length === 1 ? opt[k] : opt[k].padStart(ret[1].length, '0'),
      );
    }
  });
  return fmt;
}
export const download = (url, name) => {
  const a = document.createElement('a');
  const event = new MouseEvent('click');
  a.download = `${name || new Date().getTime()}.png`;
  a.href = url;
  a.dispatchEvent(event);
};


export function getDate(dates) {
  const dd = new Date();
  const n = dates || 0;
  dd.setDate(dd.getDate() + n);
  const y = dd.getFullYear();
  let m = dd.getMonth() + 1;
  let d = dd.getDate();
  m = m < 10 ? `0${m}` : m;
  d = d < 10 ? `0${d}` : d;
  const day = `${y}-${m}-${d}`;
  return day;
}

/*
 *getMonth(type,months)  //type为字符串类型，有两种选择，"s"代表开始,"e"代表结束，months为数字类型，不传或0代表本月，-1代表上月，1代表下月
 *getMonth("s",1)  //得到下月第一天的yyyy-mm-dd格式日期
 *getMonth("e",1)  //得到下月最后一天的yyyy-mm-dd格式日期
 */

export function getMonth(type, monthArg) {
  const d = new Date();
  let year = d.getFullYear();
  let months = monthArg;
  let month = d.getMonth() + 1;
  if (Math.abs(months) > 12) {
    months %= 12;
  }
  if (months !== 0) {
    if (month + months > 12) {
      year += 1;
      month = (month + months) % 12;
    } else if (month + months < 1) {
      year -= 1;
      month = 12 + month + months;
    } else {
      month += months;
    }
  }
  month = month < 10 ? `0${month}` : month;
  const firstDay = `${year}-${month}-01`;
  let lastDay = '';
  if (
    month === '01'
    || month === '03'
    || month === '05'
    || month === '07'
    || month === '08'
    || month === '10'
    || month === '12'
  ) {
    lastDay = `${year}-${month}-${31}`;
  } else if (month === '02') {
    if (
      (year % 4 === 0 && year % 100 !== 0)
      || (year % 100 === 0 && year % 400 === 0)
    ) {
      lastDay = `${year}-${month}-${29}`;
    } else {
      lastDay = `${year}-${month}-${28}`;
    }
  } else {
    lastDay = `${year}-${month}-${30}`;
  }
  let day = '';
  if (type === 's') {
    day = firstDay;
  } else {
    day = lastDay;
  }
  return day;
}

// 密码校验
export function formatMobile(value) {
  return value?.replace(/(\d{3})\d{4}(\d{4})/, "$1****$2")
}

export function formatIdCard(value) {
  return value?.replace(/^(.{6})(?:\d+)(.{4})$/, "$1********$2")
}


