function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import { isChinese as checkIsChinese } from "./localeUtils";

/**
 * 格式化日期
 * @param date dayjs 日期对象
 * @param config 日期格式配置
 * @returns 格式化后的日期字符串
 *
 * @example
 * // 英文环境
 * formatDate(dayjs(), { order: 'YMD', shortMonth: true, showYear: true })
 * // => "2026-Mar-21"
 *
 * // 中文环境
 * formatDate(dayjs(), { order: 'YMD', showChineseUnit: true, showYear: true })
 * // => "2026年3月21日"
 *
 * formatDate(dayjs(), { order: 'MDY', shortMonth: false, showYear: true })
 * // => "March 21, 2026"
 */
export function formatDate(date, config) {
  var _ref = config || {},
    _ref$order = _ref.order,
    order = _ref$order === void 0 ? 'YMD' : _ref$order,
    _ref$shortMonth = _ref.shortMonth,
    shortMonth = _ref$shortMonth === void 0 ? true : _ref$shortMonth,
    _ref$numericMonth = _ref.numericMonth,
    numericMonth = _ref$numericMonth === void 0 ? false : _ref$numericMonth,
    _ref$showChineseUnit = _ref.showChineseUnit,
    showChineseUnit = _ref$showChineseUnit === void 0 ? false : _ref$showChineseUnit,
    _ref$separator = _ref.separator,
    separator = _ref$separator === void 0 ? '-' : _ref$separator,
    _ref$showYear = _ref.showYear,
    showYear = _ref$showYear === void 0 ? false : _ref$showYear,
    _ref$showMonth = _ref.showMonth,
    showMonth = _ref$showMonth === void 0 ? true : _ref$showMonth,
    _ref$showDay = _ref.showDay,
    showDay = _ref$showDay === void 0 ? true : _ref$showDay,
    _ref$useCommaBeforeYe = _ref.useCommaBeforeYear,
    useCommaBeforeYear = _ref$useCommaBeforeYe === void 0 ? true : _ref$useCommaBeforeYe;

  // 判断是否为中文环境
  var locale = date.locale();
  var isChinese = checkIsChinese(locale);

  // 构建格式化字符串的各部分
  var parts = [];

  // 年份格式
  var yearFormat = isChinese && showChineseUnit ? 'YYYY年' : 'YYYY';

  // 月份格式
  var monthFormat = '';
  if (numericMonth) {
    // 数字月份
    monthFormat = isChinese && showChineseUnit ? 'M月' : 'MM';
  } else {
    // 文本月份（英文）或数字月份（中文）
    if (isChinese) {
      monthFormat = showChineseUnit ? 'M月' : 'M';
    } else {
      monthFormat = shortMonth ? 'MMM' : 'MMMM';
    }
  }

  // 日期格式
  var dayFormat = isChinese && showChineseUnit ? 'D日' : 'DD';

  // 根据日期顺序组装
  switch (order) {
    case 'YMD':
      if (showYear) parts.push(yearFormat);
      if (showMonth) parts.push(monthFormat);
      if (showDay) parts.push(dayFormat);
      break;
    case 'MDY':
      if (showMonth) parts.push(monthFormat);
      if (showDay) parts.push(dayFormat);
      if (showYear) parts.push(yearFormat);
      break;
    case 'DMY':
      if (showDay) parts.push(dayFormat);
      if (showMonth) parts.push(monthFormat);
      if (showYear) parts.push(yearFormat);
      break;
  }

  // 如果没有要显示的部分，返回空字符串
  if (parts.length === 0) {
    return '';
  }

  // 中文且有单位时，直接连接（不使用分隔符）
  if (isChinese && showChineseUnit) {
    return date.format(parts.join(''));
  }

  // MDY 格式特殊处理（英文习惯）Mar 21, 2026
  if (order === 'MDY' && !isChinese) {
    var formatted = date.format(parts.join(' '));
    if (showYear && showMonth && showDay) {
      var _formatted$split = formatted.split(' '),
        _formatted$split2 = _slicedToArray(_formatted$split, 3),
        month = _formatted$split2[0],
        day = _formatted$split2[1],
        year = _formatted$split2[2];
      return "".concat(month, " ").concat(day, ", ").concat(year);
    }
    if (showYear && showDay && !showMonth) {
      var _formatted$split3 = formatted.split(' '),
        _formatted$split4 = _slicedToArray(_formatted$split3, 2),
        _day = _formatted$split4[0],
        _year = _formatted$split4[1];
      return "".concat(_day, ", ").concat(_year);
    }
    return formatted;
  }

  // DMY 格式英文：21 Mar, 2026 或 22 Jan 2025（useCommaBeforeYear 控制）
  if (order === 'DMY' && !isChinese) {
    var _formatted = date.format(parts.join(' '));
    if (showYear && showMonth && showDay && useCommaBeforeYear) {
      var _formatted$split5 = _formatted.split(' '),
        _formatted$split6 = _slicedToArray(_formatted$split5, 3),
        _day2 = _formatted$split6[0],
        _month = _formatted$split6[1],
        _year2 = _formatted$split6[2];
      return "".concat(_day2, " ").concat(_month, ", ").concat(_year2);
    }
    if (showYear && showDay && !showMonth && useCommaBeforeYear) {
      var _formatted$split7 = _formatted.split(' '),
        _formatted$split8 = _slicedToArray(_formatted$split7, 2),
        _day3 = _formatted$split8[0],
        _year3 = _formatted$split8[1];
      return "".concat(_day3, ", ").concat(_year3);
    }
    return _formatted;
  }

  // 其他情况使用分隔符
  return date.format(parts.join(separator));
}