/**
 * tdesign v0.41.7
 * (c) 2022 tdesign
 * @license MIT
 */

'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var _extends = require('@babel/runtime/helpers/extends');
var chunk = require('lodash/chunk');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var _extends__default = /*#__PURE__*/_interopDefaultLegacy(_extends);
var chunk__default = /*#__PURE__*/_interopDefaultLegacy(chunk);

function firstUpperCase(str) {
  if (!str) return str;
  return str[0].toUpperCase().concat(str.substring(1, str.length));
}

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

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

function getLastDayOfMonth(_ref3) {
  var year = _ref3.year,
      month = _ref3.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, _ref4) {
  var start = _ref4.start,
      end = _ref4.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);
  var year = date.getFullYear();

  var month = date.getMonth() + _num;

  var day = newDate.getDate();
  newDate.setDate(1);
  newDate.setMonth(month);
  newDate.setDate(Math.min(day, getDaysInMonth({
    year: year,
    month: month
  })));
  return newDate;
}
function getWeeks(_ref5, _ref6) {
  var year = _ref5.year,
      month = _ref5.month;
  var firstDayOfWeek = _ref6.firstDayOfWeek,
      _ref6$disableDate = _ref6.disableDate,
      disableDate = _ref6$disableDate === void 0 ? function () {
    return false;
  } : _ref6$disableDate,
      minDate = _ref6.minDate,
      maxDate = _ref6.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) {
    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__default["default"](daysArr, 7);
}
function getYears(year, _ref7) {
  var _ref7$disableDate = _ref7.disableDate,
      disableDate = _ref7$disableDate === void 0 ? function () {
    return false;
  } : _ref7$disableDate,
      minDate = _ref7.minDate,
      maxDate = _ref7.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__default["default"](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__default["default"](MonthArr, 4);
}
function flagActive(data, _ref8) {
  var args = _extends__default["default"]({}, _ref8);

  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];
}

exports.addMonth = addMonth;
exports.extractTimeFormat = extractTimeFormat;
exports.firstUpperCase = firstUpperCase;
exports.flagActive = flagActive;
exports.getDateObj = getDateObj;
exports.getMonths = getMonths;
exports.getToday = getToday;
exports.getWeeks = getWeeks;
exports.getYears = getYears;
exports.isSame = isSame;
exports.outOfRanges = outOfRanges;
exports.setDateTime = setDateTime;
exports.subtractMonth = subtractMonth;
//# sourceMappingURL=utils.js.map
