/**
 * tdesign v0.32.0
 * (c) 2021 tdesign
 * @license MIT
 */

import _extends from '@babel/runtime/helpers/extends';
import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
import _typeof from '@babel/runtime/helpers/typeof';
import chunk from 'lodash/chunk';

var nextMonth = function nextMonth(date) {
  var isPassYear = date.getMonth() === 11;
  var newMonth = isPassYear ? 0 : date.getMonth() + 1;
  var year = isPassYear ? date.getFullYear() + 1 : date.getFullYear();
  return new Date(year, newMonth, 1);
};
var prevMonth = function prevMonth(date) {
  var passYear = date.getMonth() === 0;
  var newMonth = passYear ? 11 : date.getMonth() - 1;
  var year = passYear ? date.getFullYear() - 1 : date.getFullYear();
  return new Date(year, newMonth, 1);
};
function empty(mixedVar) {
  var undef;
  var emptyValues = [undef, null, false, ""];

  for (var i = 0, len = emptyValues.length; i < len; i++) {
    if (mixedVar === emptyValues[i]) {
      return true;
    }
  }

  if (_typeof(mixedVar) === "object") {
    return Object.keys(mixedVar).length < 1;
  }

  return false;
}
function toDash(strCamelCase) {
  return String(strCamelCase).split(/(?=[A-Z])/).join("-").toLowerCase();
}
function setStyles(el, styles) {
  Object.entries(styles).forEach(function (_ref) {
    var _ref2 = _slicedToArray(_ref, 2),
        prop = _ref2[0],
        _ref2$ = _ref2[1],
        val = _ref2$ === void 0 ? null : _ref2$;

    if (empty(val)) {
      el.style.removeProperty(prop);
      return;
    }

    var _String$split = String(val).split("!"),
        _String$split2 = _slicedToArray(_String$split, 2),
        value = _String$split2[0],
        _String$split2$ = _String$split2[1],
        pri = _String$split2$ === void 0 ? "" : _String$split2$;

    el.style.setProperty(toDash(prop), value, pri);
  });
}
function firstUpperCase(str) {
  if (!str) return str;
  return str[0].toUpperCase().concat(str.substring(1, str.length));
}

function getFirstDayOfMonth(_ref3) {
  var year = _ref3.year,
      month = _ref3.month;
  return new Date(year, month, 1);
}

function getDaysInMonth(_ref4) {
  var year = _ref4.year,
      month = _ref4.month;
  return new Date(year, month + 1, 0).getDate();
}

function getLastDayOfMonth(_ref5) {
  var year = _ref5.year,
      month = _ref5.month;
  return new Date(year, month, getDaysInMonth({
    year: year,
    month: month
  }));
}

function isSameYear(date1, date2) {
  return date1.getFullYear() === date2.getFullYear();
}

function isSameMonth(date1, date2) {
  return isSameYear(date1, date2) && date1.getMonth() === date2.getMonth();
}

function isSameDate(date1, date2) {
  return isSameMonth(date1, date2) && date1.getDate() === date2.getDate();
}

function isBetween(value, _ref6) {
  var start = _ref6.start,
      end = _ref6.end;
  var date = new Date(value.getFullYear(), value.getMonth(), value.getDate());
  var startTime = new Date(start.getFullYear(), start.getMonth(), start.getDate());
  var endTime = new Date(end.getFullYear(), end.getMonth(), end.getDate());
  return startTime <= date && endTime >= date;
}

function compareAsc(date1, date2) {
  var d1 = date1.getTime();
  var d2 = date2.getTime();
  if (d1 < d2) return -1;
  if (d1 > d2) return 1;
  return 0;
}

function isSame(date1, date2) {
  var type = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : "date";
  var func = {
    isSameYear: isSameYear,
    isSameMonth: isSameMonth,
    isSameDate: isSameDate
  };
  return func["isSame".concat(firstUpperCase(type))](date1, date2);
}
function outOfRanges(d, min, max) {
  return min && compareAsc(d, min) === -1 || max && compareAsc(d, max) === 1;
}
function getToday() {
  var now = new Date();
  return new Date(now.getFullYear(), now.getMonth(), now.getDate(), 0, 0, 0);
}
function getDateObj(date) {
  var tempDate = date;

  if (!(date instanceof Date)) {
    tempDate = getToday();
  }

  return {
    year: tempDate.getFullYear(),
    month: tempDate.getMonth(),
    date: tempDate.getDate(),
    hours: tempDate.getHours(),
    minutes: tempDate.getMinutes(),
    seconds: tempDate.getSeconds(),
    meridiem: tempDate.getHours() > 11 ? "PM" : "AM"
  };
}
function setDateTime(d, hour, min, sec) {
  var _getDateObj = getDateObj(d),
      year = _getDateObj.year,
      month = _getDateObj.month,
      date = _getDateObj.date;

  return new Date(year, month, date, hour, min, sec, 0);
}
function subtractMonth(date, num) {
  var day = date.getDate();
  var newDate = new Date(date);
  var _num = num;

  while (_num--) {
    newDate.setDate(0);
  }

  newDate.setDate(day);
  return newDate;
}
function addMonth(date, num) {
  var _num = num;
  if (num < 0) _num = 0;
  var newDate = new Date(date);
  newDate.setMonth(date.getMonth() + _num);
  return newDate;
}
function getWeeks(_ref7, _ref8) {
  var year = _ref7.year,
      month = _ref7.month;
  var firstDayOfWeek = _ref8.firstDayOfWeek,
      _ref8$disableDate = _ref8.disableDate,
      disableDate = _ref8$disableDate === void 0 ? function () {
    return false;
  } : _ref8$disableDate,
      minDate = _ref8.minDate,
      maxDate = _ref8.maxDate;
  var prependDay = getFirstDayOfMonth({
    year: year,
    month: month
  });
  var appendDay = getLastDayOfMonth({
    year: year,
    month: month
  });
  var maxDays = getDaysInMonth({
    year: year,
    month: month
  });
  var daysArr = [];
  var i = 1;
  var today = getToday();

  for (i; i <= maxDays; i++) {
    var currentDay = new Date(year, month, i);
    daysArr.push({
      text: i,
      active: false,
      value: currentDay,
      disabled: typeof disableDate === "function" && disableDate(currentDay) || outOfRanges(currentDay, minDate, maxDate),
      now: isSame(today, currentDay),
      firstDayOfMonth: i === 1,
      lastDayOfMonth: i === maxDays,
      type: "current-month"
    });
  }

  if (prependDay.getDay() !== firstDayOfWeek - 1) {
    prependDay.setDate(0);

    while (true) {
      daysArr.unshift({
        text: prependDay.getDate().toString(),
        active: false,
        value: new Date(prependDay),
        disabled: typeof disableDate === "function" && disableDate(prependDay) || outOfRanges(prependDay, minDate, maxDate),
        additional: true,
        type: "prev-month"
      });
      prependDay.setDate(prependDay.getDate() - 1);
      if (prependDay.getDay() === Math.abs(firstDayOfWeek + 6) % 7) break;
    }
  }

  var LEN = 42;

  while (daysArr.length < LEN) {
    appendDay.setDate(appendDay.getDate() + 1);
    daysArr.push({
      text: appendDay.getDate(),
      active: false,
      value: new Date(appendDay),
      disabled: typeof disableDate === "function" && disableDate(appendDay) || outOfRanges(appendDay, minDate, maxDate),
      additional: true,
      type: "next-month"
    });
  }

  return chunk(daysArr, 7);
}
function getYears(year, _ref9) {
  var _ref9$disableDate = _ref9.disableDate,
      disableDate = _ref9$disableDate === void 0 ? function () {
    return false;
  } : _ref9$disableDate,
      minDate = _ref9.minDate,
      maxDate = _ref9.maxDate;
  var startYear = parseInt((year / 10).toString(), 10) * 10;
  var endYear = startYear + 9;
  var yearArr = [];
  var today = getToday();

  for (var i = startYear; i <= endYear; i++) {
    var date = new Date(i, 1);
    var disabledMonth = 0;
    var outOfRangeMonth = 0;

    for (var j = 0; j < 12; j++) {
      var d = new Date(i, j);
      if (typeof disableDate === "function" && disableDate(d)) disabledMonth += 1;
      if (outOfRanges(d, minDate, maxDate)) outOfRangeMonth += 1;
    }

    yearArr.push({
      value: date,
      now: isSame(date, today, "year"),
      disabled: disabledMonth === 12 || outOfRangeMonth === 12,
      active: false,
      text: "".concat(date.getFullYear())
    });
  }

  return chunk(yearArr, 4);
}
function getMonths(year, params) {
  var _params$disableDate = params.disableDate,
      disableDate = _params$disableDate === void 0 ? function () {
    return false;
  } : _params$disableDate,
      minDate = params.minDate,
      maxDate = params.maxDate,
      monthLocal = params.monthLocal;
  var MonthArr = [];
  var today = getToday();

  for (var i = 0; i <= 11; i++) {
    var date = new Date(year, i);
    var disabledDay = 0;
    var outOfRangeDay = 0;
    var daysInMonth = getDaysInMonth({
      year: year,
      month: i
    });

    for (var j = 1; j <= daysInMonth; j++) {
      var d = new Date(year, i, j);
      if (typeof disableDate === "function" && disableDate(d)) disabledDay += 1;
      if (outOfRanges(d, minDate, maxDate)) outOfRangeDay += 1;
    }

    MonthArr.push({
      value: date,
      now: isSame(date, today, "month"),
      disabled: disabledDay === daysInMonth || outOfRangeDay === daysInMonth,
      active: false,
      text: monthLocal[date.getMonth()]
    });
  }

  return chunk(MonthArr, 4);
}
function flagActive(data, _ref10) {
  var args = _extends({}, _ref10);

  var start = args.start,
      end = args.end,
      _args$type = args.type,
      type = _args$type === void 0 ? "date" : _args$type;

  if (!end) {
    return data.map(function (row) {
      return row.map(function (item) {
        var _item = item;
        _item.active = isSame(item.value, start, type);
        return _item;
      });
    });
  }

  return data.map(function (row) {
    return row.map(function (item) {
      var _item = item;
      var date = item.value;
      var isStart = isSame(start, date, type);
      var isEnd = isSame(end, date, type);
      _item.active = isStart || isEnd;
      _item.highlight = isBetween(date, {
        start: start,
        end: end
      });
      _item.startOfRange = isStart;
      _item.endOfRange = isEnd;
      return _item;
    });
  });
}
function extractTimeFormat(dateFormat) {
  var res = dateFormat.match(/(a\s)?h{1,2}:m{1,2}(:s{1,2})?(\sa)?/i);
  if (!res) return null;
  return res[0];
}

export { addMonth, empty, extractTimeFormat, firstUpperCase, flagActive, getDateObj, getMonths, getToday, getWeeks, getYears, isSame, nextMonth, outOfRanges, prevMonth, setDateTime, setStyles, subtractMonth, toDash };
//# sourceMappingURL=utils.js.map
