export function formatterValueToLabel(
  value,
  labelDatas,
  valuePropName,
  labelPropName,
  nonLabel = '',
) {
  let formatterResult = nonLabel;
  if (labelDatas && labelDatas instanceof Array) {
    // labelDatas 是数组
    labelDatas.forEach((item, index) => {
      if (
        !Object.hasOwn(item, valuePropName) ||
        !Object.hasOwn(item, labelPropName)
      ) {
        console.log('formatterValueToLabel:error:字典数组元素缺失指定属性');
        return;
      }

      if (item[valuePropName] == value) {
        formatterResult = item[labelPropName];
      }
    });
    return formatterResult;
  }
}

export function formatterArrayToLabel(
  arr,
  labelPropName,
  splitMark = ',',
  nonLabel = '',
  isAddNonLabel = true,
) {
  const formatterResult = '';
  const labelsArr = [];
  if (arr && arr instanceof Array) {
    arr.forEach((item, index) => {
      if (Object.hasOwn(item, labelPropName)) {
        labelsArr.push(item[labelPropName]);
      } else {
        console.log('formatterArrayToLabel:warning:数组中有元素缺失指定属性');
        if (isAddNonLabel) {
          labelsArr.push(nonLabel);
        }
      }
    });
    return labelsArr.join(splitMark);
  }
}

export function nextMonthLastDay() {
  //日期显示为次月最后一天

  var time = new Date();

  var year = time.getFullYear();

  //var year = 1900; //用于测试

  var month = time.getMonth() + 2;

  //var month = 0 + 2; //用于测试

  if (month > 12) {
    month = month - 12;

    year = year + 1;
  }

  var day = nextMonthDay(year, month);

  return year + '-' + month + '-' + day;
}

function nextMonthDay(year, month) {
  //判断每月多少天

  var day31 = [1, 3, 5, 7, 8, 10, 12];

  var day30 = [4, 6, 9, 11];

  if (day31.indexOf(month) > -1) {
    return 31;
  } else if (day30.indexOf(month) > -1) {
    return 30;
  } else {
    if (isLeapYear(year)) {
      return 29;
    } else {
      return 28;
    }
  }
}

function isLeapYear(year) {
  //判断是否为闰年

  return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
}
