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

'use strict';

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

var dayjs = require('dayjs');
var _common_js_datePicker_utils = require('./utils.js');
var _common_js_log_log = require('../log/log.js');
require('@babel/runtime/helpers/slicedToArray');
require('@babel/runtime/helpers/extends');
require('@babel/runtime/helpers/defineProperty');
require('../../../_chunks/dep-f8992a6f.js');
require('../../../_chunks/dep-200196ec.js');
require('lodash/chunk');

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

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

var TIME_FORMAT = "HH:mm:ss";
function parseToDayjs(value, format, timeOfDay) {
  if (value === "") return dayjs__default["default"]();
  var dateText = value;

  if (/[w|W]/g.test(format)) {
    if (typeof dateText !== "string") {
      dateText = dayjs__default["default"](dateText).format(format);
    }

    var yearStr = dateText.split(/[-/.]/)[0];
    var weekStr = dateText.split(/[-/.]/)[1];
    var weekFormatStr = format.split(/[-/.]/)[1];
    var firstWeek = dayjs__default["default"](yearStr, "YYYY").startOf("year");

    for (var i = 0; i <= 52; 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 (typeof dateText !== "string") {
      dateText = dayjs__default["default"](dateText).format(format);
    }

    var _yearStr = dateText.split(/[-/.]/)[0];
    var quarterStr = dateText.split(/[-/.]/)[1];
    var quarterFormatStr = format.split(/[-/.]/)[1];

    var _firstWeek = dayjs__default["default"](_yearStr, "YYYY").startOf("year");

    for (var _i = 0; _i <= 52; _i += 1) {
      var nextQuarter = _firstWeek.add(_i, "quarter");

      if (nextQuarter.format(quarterFormatStr) === quarterStr) {
        return nextQuarter;
      }
    }
  }

  return dayjs__default["default"](dateText, format).isValid() ? dayjs__default["default"](dateText, format) : dayjs__default["default"](dateText);
}

function formatRange(_ref) {
  var newDate = _ref.newDate,
      format = _ref.format,
      targetFormat = _ref.targetFormat;
  if (!newDate || !Array.isArray(newDate)) return [];
  var dayjsDateList = newDate.map(function (d) {
    return d && parseToDayjs(d, format);
  });

  if (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", "\u8BF7\u68C0\u67E5 format\u3001valueType\u3001value \u683C\u5F0F\u662F\u5426\u6709\u6548.\nformat: '".concat(format, "' valueType: '").concat(targetFormat, "' value: '").concat(newDate, "'"));
    return [];
  }

  if (targetFormat === "time-stamp") {
    return dayjsDateList.map(function (da) {
      return da && da.toDate().getTime();
    });
  }

  return dayjsDateList.map(function (da) {
    return da && da.format(targetFormat);
  });
}

function formatSingle(_ref2) {
  var newDate = _ref2.newDate,
      format = _ref2.format,
      targetFormat = _ref2.targetFormat;
  if (!newDate) return "";
  var dayJsDate = parseToDayjs(newDate, format);

  if (!dayJsDate.isValid()) {
    _common_js_log_log["default"].error("DatePicker", "\u8BF7\u68C0\u67E5 format\u3001valueType\u3001value \u683C\u5F0F\u662F\u5426\u6709\u6548.\nformat: '".concat(format, "' valueType: '").concat(targetFormat, "' value: '").concat(newDate, "'"));
    return "";
  }

  if (targetFormat === "time-stamp") return dayJsDate.toDate().getTime();
  return dayJsDate.format(targetFormat);
}

function isValidDate(value, format) {
  if (Array.isArray(value)) {
    if (format === "time-stamp") return value.every(function (v) {
      return dayjs__default["default"](v).isValid();
    });
    return value.every(function (v) {
      return dayjs__default["default"](v, format).isValid() || dayjs__default["default"](v).isValid();
    });
  }

  if (format === "time-stamp") return dayjs__default["default"](value).isValid();
  return dayjs__default["default"](value, format).isValid() || dayjs__default["default"](value).isValid();
}
function formatDate(newDate, _ref3) {
  var format = _ref3.format,
      targetFormat = _ref3.targetFormat;
  var result;

  if (Array.isArray(newDate)) {
    result = formatRange({
      newDate: newDate,
      format: format,
      targetFormat: targetFormat
    });
  } else {
    result = formatSingle({
      newDate: newDate,
      format: format,
      targetFormat: targetFormat
    });
  }

  return result;
}
function formatTime(value, timeFormat) {
  var result;

  if (Array.isArray(value)) {
    result = value.map(function (v) {
      return dayjs__default["default"](v).format(timeFormat);
    });
  } else {
    result = dayjs__default["default"](value || new Date()).format(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 {};
}

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