//元素Options配置-图片的公共配置
// 全局公共枚举参数
import emptyImg from '@/assets/images/emptyImg.png';
import { IMG_URL } from './variables'
export const errorImg = emptyImg;
export const imageOptions = {
  fallback: errorImg,
  placeholder: true,
};
//获取地址栏参数
export const getUrlQuery = (name: string) => {
  var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)');
  var r = window.location.search.substring(1).match(reg);
  if (r != null) return decodeURI(r[2]);
  return '';
};

/**
 * 根据&转为数组
 */
export const splitStrToArr = (str: string, sign: string = ',') => {
  if (str) {
    return str.split(sign);
  }
  return [];
};

//填充7位元素
export const ensureLength = (arr: any[], length = 7, fillValue = undefined) => {
  return Array.from({ length }, (_, index) =>
    index < arr.length ? arr[index] : fillValue,
  );
};

/**
 * 图片转换，无http，拼接前缀
 * @param imgUrl 图片地址
 */
export const joinPictureHttp = (imgUrl: string) => {
  if(!imgUrl) {
    return ''
  }
  return imgUrl?.includes('http') ? imgUrl : (IMG_URL + imgUrl);
};
