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

'use strict';

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

var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
var _extends = require('@babel/runtime/helpers/extends');
var dayjs = require('dayjs');
var isBetween = require('../../../_chunks/dep-e27efb35.js');
var chunk = require('lodash/chunk');
require('../../../_chunks/dep-1641a31f.js');

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

var _slicedToArray__default = /*#__PURE__*/_interopDefaultLegacy(_slicedToArray);
var _extends__default = /*#__PURE__*/_interopDefaultLegacy(_extends);
var dayjs__default = /*#__PURE__*/_interopDefaultLegacy(dayjs);
var chunk__default = /*#__PURE__*/_interopDefaultLegacy(chunk);

dayjs__default["default"].extend(isBetween.isBetween);
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 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(),
    milliseconds: tempDate.getMilliseconds(),
    meridiem: tempDate.getHours() > 11 ? "PM" : "AM"
  };
}
function setDateTime(date, hours, minutes, seconds, milliseconds) {
  return dayjs__default["default"](date).hour(hours).minute(minutes).second(seconds).millisecond(milliseconds).toDate();
}
function subtractMonth(date, num) {
  return dayjs__default["default"](date).subtract(num, "month").toDate();
}
function addMonth(date, num) {
  return dayjs__default["default"](date).add(num, "month").toDate();
}
function getWeeks(_ref4, _ref5) {
  var year = _ref4.year,
      month = _ref4.month;
  var firstDayOfWeek = _ref5.firstDayOfWeek,
      _ref5$disableDate = _ref5.disableDate,
      disableDate = _ref5$disableDate === void 0 ? function () {
    return false;
  } : _ref5$disableDate,
      minDate = _ref5.minDate,
      maxDate = _ref5.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, _ref6) {
  var _ref6$disableDate = _ref6.disableDate,
      disableDate = _ref6$disableDate === void 0 ? function () {
    return false;
  } : _ref6$disableDate,
      minDate = _ref6.minDate,
      maxDate = _ref6.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, 3);
}
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, 3);
}
function flagActive(data, _ref7) {
  var args = _extends__default["default"]({}, _ref7);

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

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

  return data.map(function (row) {
    return row.map(function (item) {
      var _item = item;
      var date = item.value;
      var isStart = start && isSame(start, date, type);
      var isHoverStart = hoverStart && isSame(hoverStart, date, type);
      var isEnd = end && isSame(end, date, type);
      var isHoverEnd = hoverEnd && isSame(hoverEnd, date, type);
      _item.active = (isStart || isEnd) && !_item.additional;

      if (start && end) {
        _item.highlight = dayjs__default["default"](date).isBetween(start, end, type, "[]") && !_item.additional;
        _item.startOfRange = isStart;
        _item.endOfRange = isEnd;
      }

      if (hoverStart && hoverEnd) {
        _item.hoverHighlight = dayjs__default["default"](date).isBetween(hoverStart, hoverEnd, type, "[]") && !_item.additional;
        _item.hoverStartOfRange = isHoverStart;
        _item.hoverEndOfRange = isHoverEnd;
      }

      return _item;
    });
  });
}
function extractTimeFormat() {
  var dateFormat = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "";
  var res = dateFormat.match(/(a\s)?h{1,2}:m{1,2}(:s{1,2})?(\sa)?/i);
  if (!res) return null;
  return res[0];
}
function extractTimeObj() {
  var timeFormat = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "";
  var matchedMeridiem = timeFormat.match(/[ap]m/i) || [""];
  var timeReg = /\d{1,2}:\d{1,2}(:\d{1,2})?(:\d{1,3})?/;
  var matchedTimeStr = timeFormat.match(timeReg) || ["0:0:0:0"];

  var _matchedTimeStr$0$spl = matchedTimeStr[0].split(":"),
      _matchedTimeStr$0$spl2 = _slicedToArray__default["default"](_matchedTimeStr$0$spl, 4),
      _matchedTimeStr$0$spl3 = _matchedTimeStr$0$spl2[0],
      hours = _matchedTimeStr$0$spl3 === void 0 ? 0 : _matchedTimeStr$0$spl3,
      _matchedTimeStr$0$spl4 = _matchedTimeStr$0$spl2[1],
      minutes = _matchedTimeStr$0$spl4 === void 0 ? 0 : _matchedTimeStr$0$spl4,
      _matchedTimeStr$0$spl5 = _matchedTimeStr$0$spl2[2],
      seconds = _matchedTimeStr$0$spl5 === void 0 ? 0 : _matchedTimeStr$0$spl5,
      _matchedTimeStr$0$spl6 = _matchedTimeStr$0$spl2[3],
      milliseconds = _matchedTimeStr$0$spl6 === void 0 ? 0 : _matchedTimeStr$0$spl6;

  return {
    hours: +hours,
    minutes: +minutes,
    seconds: +seconds,
    milliseconds: +milliseconds,
    meridiem: matchedMeridiem[0]
  };
}
function isEnabledDate(_ref8) {
  var value = _ref8.value,
      disableDate = _ref8.disableDate,
      mode = _ref8.mode,
      format = _ref8.format;
  if (!disableDate) return true;
  var isEnabled = true;

  if (typeof disableDate === "function") {
    return !disableDate(value);
  }

  if (Array.isArray(disableDate)) {
    var formattedDisabledDate = disableDate.map(function (item) {
      return dayjs__default["default"](item, format);
    });
    var isIncludes = formattedDisabledDate.some(function (item) {
      return item.isSame(dayjs__default["default"](value));
    });
    return !isIncludes;
  }

  var from = disableDate.from,
      to = disableDate.to,
      before = disableDate.before,
      after = disableDate.after;

  if (from && to) {
    var compareMin = dayjs__default["default"](new Date(from));
    var compareMax = dayjs__default["default"](new Date(to));
    return !dayjs__default["default"](value).isBetween(compareMin, compareMax, mode, "[]");
  }

  var min = before ? new Date(before) : null;
  var max = after ? new Date(after) : null;

  if (max && min) {
    var _compareMin = dayjs__default["default"](new Date(min));

    var _compareMax = dayjs__default["default"](new Date(max));

    isEnabled = dayjs__default["default"](value).isBetween(_compareMin, _compareMax, mode, "[]");
  } else if (min) {
    var _compareMin2 = dayjs__default["default"](new Date(min));

    isEnabled = !dayjs__default["default"](value).isBefore(_compareMin2, mode);
  } else if (max) {
    var _compareMax2 = dayjs__default["default"](new Date(max));

    isEnabled = !dayjs__default["default"](value).isAfter(_compareMax2, mode);
  }

  return isEnabled;
}

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