function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

import moment from 'moment';
import _ from 'lodash';
/**
 * 返回近7天的时间字符串
 * YYYY-MM-DD
 * @returns {*[]}
 */

function getNear7Day() {
  var dateArr = []; // eslint-disable-next-line no-plusplus

  for (var i = 0; i < 7; i++) {
    dateArr.push(moment().subtract(i, 'days').format('YYYY-MM-DD'));
  }

  return dateArr.reverse();
}
/**
 * 切割补全时间
 * 小于1天返回时间频数
 * 大于1天返回按天的聚合
 * @param start 开始时间
 * @param end 结束时间
 * @param date 时间数组
 * @returns {{}}
 */


function dateSplitAndPad(start, end, date) {
  // eslint-disable-next-line no-param-reassign
  start = moment(start, 'x'); // eslint-disable-next-line no-param-reassign

  end = moment(end, 'x');

  if (start > end) {
    return {};
  }

  var dateArr = {};
  var timeDiff = end - start;

  if (timeDiff < 3600 * 25 * 1000) {
    var oneDay = _.countBy(date, function (item) {
      return item.slice(0, 10);
    });

    dateArr = !_.isEmpty(oneDay) ? oneDay : _defineProperty({}, moment().format('YYYY-MM-DD'), 0);
  } else {
    var days = timeDiff / (3600 * 24 * 1000);

    var dateGroup = _.countBy(date, function (item) {
      return item.slice(0, 10);
    }); // eslint-disable-next-line no-plusplus


    for (var i = 0; i <= Math.floor(days); i++) {
      var d = moment(end).subtract(i, 'days').format('YYYY-MM-DD');

      if (dateGroup[d] || dateGroup[d] === 0) {
        dateArr[d] = dateGroup[d];
      } else {
        dateArr[d] = 0;
      }
    }
  }

  return dateArr;
}
/**
 * 提取出对象的键和值，并形成对应的两个数组
 * @param obj
 * @param obj
 * @returns {*}
 */


function pickEntries(obj) {
  if (Object.prototype.toString.call(obj) !== '[object Object]') {
    return {};
  }

  var keys = Object.keys(obj);
  var values = keys.map(function (item) {
    return obj[item];
  });
  return {
    keys: keys,
    values: values
  };
}
/**
 * 返回次数报表横纵坐标数组
 * @param startTime 开始时间 时间戳
 * @param endTime 结束时间 时间戳
 * @param oldxAxis 横坐标数据 数组
 * @param oldyAxis 纵坐标数据 {a: [], b: [], ...}
 * @returns {xAixs: [], yAxis: {a: [], b: [], ...}}
 */


function getAxis(startTime, endTime) {
  var oldxAxis = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
  var oldyAxis = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
  var xAxis = []; // eslint-disable-next-line no-param-reassign

  for (; startTime <= endTime; startTime += 86400000) {
    var tmp = new Date(startTime);
    xAxis.push("".concat(tmp.getFullYear(), "-").concat(padZero(tmp.getMonth() + 1), "-").concat(padZero(tmp.getDate())));
  }

  var yAxis = {};

  _.forEach(oldyAxis, function (value, key) {
    yAxis[key] = [];
    var data = oldyAxis[key] || [];

    if (oldxAxis.length) {
      _.map(oldxAxis, function (str, index) {
        yAxis[key][xAxis.indexOf(str)] = data[index];
      });
    }
  });

  return {
    xAxis: xAxis,
    yAxis: yAxis
  };
}
/**
 * 参数 长度低于2则前面加 0，否则不加
 * @param {string | number} str
 * @returns {string}
 */


function padZero(str) {
  return str.toString().padStart(2, '0');
}

export { getNear7Day, dateSplitAndPad, pickEntries, getAxis, padZero };