/**
 * ldesign v0.4.2
 * (c) 2023 ldesign
 * @license MIT
 */

'use strict';

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

var isString = require('lodash/isString');
var dayjs = require('dayjs');
var _commonjsHelpers = require('../../../_chunks/dep-7f0559c3.js');
var _common_js_log_log = require('../log/log.js');
var _common_js_datePicker_utils = require('./utils.js');
require('@babel/runtime/helpers/slicedToArray');
require('@babel/runtime/helpers/objectDestructuringEmpty');
require('@babel/runtime/helpers/extends');
require('@babel/runtime/helpers/defineProperty');
require('lodash/isFunction');
require('../../../_chunks/dep-69993bc1.js');
require('lodash/chunk');

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

var isString__default = /*#__PURE__*/_interopDefaultLegacy(isString);
var dayjs__default = /*#__PURE__*/_interopDefaultLegacy(dayjs);

var isoWeeksInYear$1 = {exports: {}};

(function (module, exports) {
  !function (e, n) {
    module.exports = n() ;
  }(_commonjsHelpers.commonjsGlobal, function () {

    return function (e, n) {
      n.prototype.isoWeeksInYear = function () {
        var e = this.isLeapYear(),
          n = this.endOf("y").day();
        return 4 === n || e && 5 === n ? 53 : 52;
      };
    };
  });
})(isoWeeksInYear$1);
var isoWeeksInYear = isoWeeksInYear$1.exports;

var isLeapYear$1 = {exports: {}};

(function (module, exports) {
  !function (e, t) {
    module.exports = t() ;
  }(_commonjsHelpers.commonjsGlobal, function () {

    return function (e, t) {
      t.prototype.isLeapYear = function () {
        return this.$y % 4 == 0 && this.$y % 100 != 0 || this.$y % 400 == 0;
      };
    };
  });
})(isLeapYear$1);
var isLeapYear = isLeapYear$1.exports;

dayjs__default["default"].extend(isoWeeksInYear);
dayjs__default["default"].extend(isLeapYear);
var TIME_FORMAT = "HH:mm:ss";
function parseToDayjs(value, format, timeOfDay, dayjsLocale) {
  if (value === "") return dayjs__default["default"]();
  var dateText = value;
  if (/[w|W]/g.test(format)) {
    if (!isString__default["default"](dateText)) dateText = dayjs__default["default"](dateText).locale(dayjsLocale || "zh-cn").format(format);
    var yearStr = dateText.split(/[-/.\s]/)[0];
    var weekStr = dateText.split(/[-/.\s]/)[1];
    var weekFormatStr = format.split(/[-/.\s]/)[1];
    var firstWeek = dayjs__default["default"](yearStr, "YYYY").locale(dayjsLocale || "zh-cn").startOf("year");
    if (firstWeek.day() > 4 || firstWeek.day() === 0) firstWeek = firstWeek.add(1, "week");
    var weekCounts = dayjs__default["default"](yearStr, "YYYY").locale(dayjsLocale || "zh-cn").isoWeeksInYear();
    for (var i = 0; i <= weekCounts; i += 1) {
      var nextWeek = firstWeek.add(i, "week");
      if (timeOfDay === "start") nextWeek = nextWeek.subtract(5, "day");
      if (nextWeek.format(weekFormatStr) === weekStr) return nextWeek;
    }
  }
  if (/Q/g.test(format)) {
    if (!isString__default["default"](dateText)) dateText = dayjs__default["default"](dateText).locale(dayjsLocale || "zh-cn").format(format);
    var _yearStr = dateText.split(/[-/.\s]/)[0];
    var quarterStr = dateText.split(/[-/.\s]/)[1];
    var quarterFormatStr = format.split(/[-/.\s]/)[1];
    var firstQuarter = dayjs__default["default"](_yearStr, "YYYY").startOf("year");
    for (var _i = 0; _i < 4; _i += 1) {
      var nextQuarter = firstQuarter.add(_i, "quarter");
      if (nextQuarter.format(quarterFormatStr) === quarterStr) return nextQuarter;
    }
  }
  var result = dayjs__default["default"](dateText, format).isValid() ? dayjs__default["default"](dateText, format) : dayjs__default["default"](dateText);
  if (!result.isValid()) {
    _common_js_log_log["default"].error("DatePicker", "Check whether the format\u3001value format is valid.\n value: '".concat(value, "', format: '").concat(format, "'"));
    return dayjs__default["default"]();
  }
  return result;
}
function formatRange(_ref) {
  var newDate = _ref.newDate,
    format = _ref.format,
    dayjsLocale = _ref.dayjsLocale,
    targetFormat = _ref.targetFormat,
    autoSwap = _ref.autoSwap;
  if (!newDate || !Array.isArray(newDate)) return [];
  var dayjsDateList = newDate.map(function (d) {
    return d && parseToDayjs(d, format).locale(dayjsLocale);
  });
  if (autoSwap && dayjsDateList[0] && dayjsDateList[1] && dayjsDateList[0].toDate().getTime() > dayjsDateList[1].toDate().getTime()) {
    dayjsDateList = [dayjsDateList[1], dayjsDateList[0]];
  }
  if (dayjsDateList.some(function (r) {
    return r && !r.isValid();
  })) {
    _common_js_log_log["default"].error("DatePicker", "Check whether the value\u3001format\u3001valueType format is valid.\nformat: '".concat(format, "' value: '").concat(newDate, "' valueType: '").concat(targetFormat, "'"));
    return [];
  }
  if (targetFormat === "time-stamp") return dayjsDateList.map(function (da) {
    return da && da.toDate().getTime();
  });
  if (targetFormat === "Date") return dayjsDateList.map(function (da) {
    return da && da.toDate();
  });
  return dayjsDateList.map(function (da) {
    return da && da.format(targetFormat || format);
  });
}
function formatSingle(_ref2) {
  var newDate = _ref2.newDate,
    format = _ref2.format,
    targetFormat = _ref2.targetFormat,
    dayjsLocale = _ref2.dayjsLocale;
  if (!newDate) return "";
  var dayJsDate = parseToDayjs(newDate, format).locale(dayjsLocale);
  if (!dayJsDate.isValid()) {
    _common_js_log_log["default"].error("DatePicker", "Check whether the format\u3001value format is valid.\nformat: '".concat(format, "' value: '").concat(newDate, "'"));
    return "";
  }
  if (targetFormat === "time-stamp") return dayJsDate.toDate().getTime();
  if (targetFormat === "Date") return dayJsDate.toDate();
  return dayJsDate.format(targetFormat || format);
}
function isValidDate(value, format) {
  if (Array.isArray(value)) {
    return value.every(function (v) {
      if (v === "") return true;
      return dayjs__default["default"](v, format).isValid() || dayjs__default["default"](v).isValid();
    });
  }
  if (value === "") return true;
  return dayjs__default["default"](value, format).isValid() || dayjs__default["default"](value).isValid();
}
function formatDate(newDate, _ref3) {
  var format = _ref3.format,
    targetFormat = _ref3.targetFormat,
    _ref3$dayjsLocale = _ref3.dayjsLocale,
    dayjsLocale = _ref3$dayjsLocale === void 0 ? "zh-cn" : _ref3$dayjsLocale,
    autoSwap = _ref3.autoSwap;
  var result;
  if (Array.isArray(newDate)) result = formatRange({
    newDate: newDate,
    format: format,
    dayjsLocale: dayjsLocale,
    targetFormat: targetFormat,
    autoSwap: autoSwap
  });else result = formatSingle({
    newDate: newDate,
    format: format,
    dayjsLocale: dayjsLocale,
    targetFormat: targetFormat
  });
  return result;
}
function calcFormatTime(time, timeFormat) {
  if (time && timeFormat) {
    var timeArr = time.split(":");
    var timeFormatArr = timeFormat.split(":");
    return timeArr.slice(0, timeFormatArr.length).join(":");
  }
  return time;
}
function formatTime(value, format, timeFormat, defaultTime) {
  var result;
  if (Array.isArray(value)) {
    if (!Array.isArray(defaultTime)) defaultTime = [defaultTime, defaultTime];
    result = value.map(function (v, i) {
      return v ? dayjs__default["default"](v, format).format(timeFormat) : calcFormatTime(defaultTime[i], timeFormat);
    });
    result = result.length ? result : defaultTime.map(function (t) {
      return calcFormatTime(t, timeFormat);
    });
  } else {
    result = value ? dayjs__default["default"](value, format).format(timeFormat) : calcFormatTime(defaultTime, timeFormat);
  }
  return result;
}
function getDefaultFormat(_ref4) {
  var _ref4$mode = _ref4.mode,
    mode = _ref4$mode === void 0 ? "date" : _ref4$mode,
    format = _ref4.format,
    valueType = _ref4.valueType,
    enableTimePicker = _ref4.enableTimePicker;
  if (mode === "year") {
    return {
      format: format || "YYYY",
      valueType: valueType || format || "YYYY",
      timeFormat: TIME_FORMAT
    };
  }
  if (mode === "month") {
    return {
      format: format || "YYYY-MM",
      valueType: valueType || format || "YYYY-MM",
      timeFormat: TIME_FORMAT
    };
  }
  if (mode === "quarter") {
    return {
      format: format || "YYYY-[Q]Q",
      valueType: valueType || format || "YYYY-[Q]Q",
      timeFormat: TIME_FORMAT
    };
  }
  if (mode === "week") {
    return {
      format: format || "YYYY-wo",
      valueType: valueType || format || "YYYY-wo",
      timeFormat: TIME_FORMAT
    };
  }
  if (mode === "date") {
    return {
      format: format || "YYYY-MM-DD".concat(enableTimePicker ? " HH:mm:ss" : ""),
      valueType: valueType || format || "YYYY-MM-DD".concat(enableTimePicker ? " HH:mm:ss" : ""),
      timeFormat: _common_js_datePicker_utils.extractTimeFormat(format || "YYYY-MM-DD".concat(enableTimePicker ? " HH:mm:ss" : "")) || TIME_FORMAT
    };
  }
  _common_js_log_log["default"].error("DatePicker", "Invalid mode: ".concat(mode));
  return {};
}
function initYearMonthTime(_ref5) {
  var value = _ref5.value,
    _ref5$mode = _ref5.mode,
    mode = _ref5$mode === void 0 ? "date" : _ref5$mode,
    format = _ref5.format,
    _ref5$timeFormat = _ref5.timeFormat,
    timeFormat = _ref5$timeFormat === void 0 ? "HH:mm:ss" : _ref5$timeFormat,
    enableTimePicker = _ref5.enableTimePicker;
  var defaultYearMonthTime = {
    year: [dayjs__default["default"]().year(), dayjs__default["default"]().year()],
    month: [dayjs__default["default"]().month(), dayjs__default["default"]().month()],
    time: [dayjs__default["default"]().format(timeFormat), dayjs__default["default"]().format(timeFormat)]
  };
  if (mode === "year") {
    defaultYearMonthTime.year[1] += 10;
  } else if (mode === "month" || mode === "quarter") {
    defaultYearMonthTime.year[1] += 1;
  } else if ((mode === "date" || mode === "week") && !enableTimePicker) {
    if (defaultYearMonthTime.month[0] === 11) {
      defaultYearMonthTime.year[1] += 1;
      defaultYearMonthTime.month[1] = 0;
    } else {
      defaultYearMonthTime.month[1] += 1;
    }
  }
  if (!value || !Array.isArray(value) || !value.length) return defaultYearMonthTime;
  return {
    year: value.map(function (v) {
      return parseToDayjs(v, format).year();
    }),
    month: value.map(function (v) {
      return parseToDayjs(v, format).month();
    }),
    time: value.map(function (v) {
      return parseToDayjs(v, format).format(timeFormat);
    })
  };
}

exports.TIME_FORMAT = TIME_FORMAT;
exports.calcFormatTime = calcFormatTime;
exports.formatDate = formatDate;
exports.formatTime = formatTime;
exports.getDefaultFormat = getDefaultFormat;
exports.initYearMonthTime = initYearMonthTime;
exports.isValidDate = isValidDate;
exports.parseToDayjs = parseToDayjs;
//# sourceMappingURL=format.js.map
