import config from '@config/index.js';
//函数类工具,对函数进行操作 返回函数
//延时操作
export function debounce(func, delay) {
    let timer = null;
    return function (...args) {
        if (timer) {
            clearTimeout(timer);
        }
        timer = setTimeout(() => {
            func.apply(this, args);
        }, delay);
    }
}

// 时间戳转换日期
export function dateParser(timestamp,link = '-'){
    let time = new Date(timestamp);
    let year = time.getFullYear();
    let month = time.getMonth()+1;
    let date = time.getDate();
    let hour = time.getHours().toString().padStart(2,'0');
    let minute = time.getMinutes().toString().padStart(2,'0');
    // let result = year+link+month+link+date;
    let result = year+link+(month<10?"0"+month:month)+link+(date<10?"0"+date:date)+' '+hour+':'+minute;
    return result;
}

//时间搓转换年龄
export const getAge = (time) => {
  const birthday = new Date(time),
    year = birthday.getFullYear(),
    month = birthday.getMonth() + 1,
    day = birthday.getDate(),
    now = new Date(),
    now_year = now.getFullYear(),
    now_month = now.getMonth() + 1,
    now_day = now.getDate();
  let age= now_year - year;
  if (now_month > month) {
    age += 1;
  } else if (now_month === month) {
    if (now_day >= day) {
      age += 1;
    }
  }
  return age;
};

//获取URL参数-返回json对象
export const parseUrl = (url) => {
  const r = url.substr(1).split("&"),
    obj = {};
  r.forEach((v) => {
    const index = v.indexOf('=');
    if (index > -1) {
      obj[v.substring(0, index)] = v.substring(index + 1);
    }
  });
  return obj;
}

//字符串去空格
export const strTrim = (str) =>{
  return str.replace(/&nbsp;|<div>|<\/div>|<br>|\s/g,'');
};

//获取组合组件已填文字填入saveText
function getSaveText(data){//console.log(data)
  const arr = data.questionMapping.map((it)=>{
    return it.value?(it.labelPrefix||'')+(it.value||'')+(it.labelSuffix||''):'';
  });
  return arr.join('');
}

//添加自由文本标签
function notTextLabel(label){
  return +label.tagType!==8;
}
/*
* 给标签组添加自由文本标签
* 入参：arr源数组，
*       noPre是否不添加前置文本标签，默认false即添加
*       noEnd是否不添加后置文本标签，默认false即添加
*       ifEmpty是否添加空标签，默认true即添加，传false添加逗号，如查体,
*       showInCheck是否默认在查体中展开
* */
export const fullfillText = (arr,noPre=false,noEnd=false,ifEmpty=true)=>{
  let newArr =[],
      pre={},
      textLabel={},
      _textLabel={},
      notText = true,
      saveText=[],
      tempText = '',
      value = '',
      item={},
      cNum = 0,
      checkHiddenDefault=false;//console.log(arr)
  arr&&arr.map((it,i)=>{
    notText = notTextLabel(it);
    cNum = i;
    value = it.value||'';
    textLabel = !ifEmpty&&i==0?Object.assign({},JSON.parse(config.textLabel),{showInCheck:true}):JSON.parse(config.textLabel);
    _textLabel = !ifEmpty&&cNum<config.showCheckNum+1?Object.assign({},JSON.parse(config._textLabel),{showInCheck:true}):JSON.parse(config._textLabel);
    if(i===0){
      //第一个标签不是文本标签时在前面添加文本标签
      if(!noPre&&notText){
        newArr.push(textLabel);
        saveText.push('');
      }
      item = ifEmpty?it:Object.assign({},it,{showInCheck:true});
      newArr.push(item);
      if(it.tagType != 3){
        tempText = value?it.labelPrefix+value+it.labelSuffix:'';
        tempText = notText?tempText:it.value||it.name;
      }else{
        tempText = getSaveText(it);
      }
      saveText.push(tempText);
      // 模板只有一个标签时第一项后面也要加空标签
      if(arr.length==1){
        newArr.push(textLabel);
        saveText.push('');
      }
    }else{
      pre = arr[i-1];
      item = !ifEmpty&&cNum<config.showCheckNum?Object.assign({},it,{showInCheck:true}):it;
      //判断单选项是否有默认选中，位置在隐藏区域时，查体所有标签展示
      if(!ifEmpty&&!checkHiddenDefault&&cNum>config.showCheckNum&&+it.tagType===1&&(+it.controlType===0||+it.controlType===1)){
        if(it.questionDetailList.find((it)=>it.defaultSelect=='1')){
          checkHiddenDefault=true;
        }
      }
      //如果本身不是文本标签且前面一个也不是文本标签,该标签前面添加文本标签
      if(notTextLabel(pre)&&notText){
          // newArr.push(textLabel,it);
          ifEmpty?newArr.push(textLabel,it):newArr.push(_textLabel,item);
          if(it.tagType != 3) {
            tempText = value ? it.labelPrefix + value + it.labelSuffix : '';
          }else{
            tempText = getSaveText(it);
          }
          saveText.push("",tempText);
      }else{    //本身是或者前面是文本标签时，前面不添加文本标签
        newArr.push(item);
        if(it.tagType != 3) {
          tempText = value ? it.labelPrefix + value + it.labelSuffix : '';
          // tempText = notText?tempText:it.value||it.name;
          tempText = notText ? tempText : (it.value || it.value == "" ? it.value : it.name);
        }else{
          tempText = getSaveText(it);
        }
        saveText.push(tempText);
      }
      if(notText&&!noEnd&&i===arr.length-1){//最后一个非文本标签，后面添加一个文本标签
        //不能用上面的变量textLabel，因为上一个if可能也进了，这样就是同一个对象，值改变时会同步
        newArr.push(ifEmpty?textLabel:_textLabel);
        saveText.push("");
      }
    }
  });
  return {newArr,saveText,checkHiddenDefault};
};


//获取标签index,入参：病例项index+标签index+标签内index
export const getLabelIndex = (index)=>{
  let ikey = '';
  if(index.length == 3){
    ikey = index.substr(1,1);
  }else if(index.length == 4){
    ikey = index.substr(1,2);
  }else if(index.length == 5){
    ikey = index.substr(1,3);
  }
  return ikey;
};

export const getWindowInnerHeight = ()=>{
  if(window.innerHeight!=undefined){
    return window.innerHeight;
  }else{
    let by = document.body, ele = document.documentElement;
    return Math.min(by.clientHeight,ele.clientHeight);
  }
};
export const getWindowInnerWidth = ()=>{
  let width = window.innerWidth || document.body.clientWidth || document.documentElement.clientWidth 
  return width
};

export const getIds = (data)=>{
  let ids = [];
  data&&data.forEach((it,i)=>{
   ids.push(it.id);
  })
  return ids;
}