/**
 * ldesign v0.2.11
 * (c) 2022/10/31 17:48 built by swimly
 * @license MIT
 */

import { d as dayjs } from './dep-2894fe71.js';
import { l as log } from './dep-b91c9b9d.js';
import { b as extractTimeFormat } from './dep-7e7bb7f0.js';
import './dep-edf62bcd.js';

var TIME_FORMAT = "HH:mm:ss";
function parseToDayjs(value, format, timeOfDay) {
  if (value === "") return dayjs();
  var dateText = value;
  if (/[w|W]/g.test(format)) {
    if (typeof dateText !== "string") dateText = dayjs(dateText).format(format);
    var yearStr = dateText.split(/[-/.]/)[0];
    var weekStr = dateText.split(/[-/.]/)[1];
    var weekFormatStr = format.split(/[-/.]/)[1];
    var firstWeek = dayjs(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(dateText).format(format);
    var _yearStr = dateText.split(/[-/.]/)[0];
    var quarterStr = dateText.split(/[-/.]/)[1];
    var quarterFormatStr = format.split(/[-/.]/)[1];
    var _firstWeek = dayjs(_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;
    }
  }
  var result = dayjs(dateText, format).isValid() ? dayjs(dateText, format) : dayjs(dateText);
  if (!result.isValid()) {
    log.error("DatePicker", "Check whether the format\u3001value format is valid.\n value: '".concat(value, "', format: '").concat(format, "'"));
    return dayjs();
  }
  return result;
}
function formatRange(_ref) {
  var {
    newDate,
    format,
    targetFormat,
    autoSwap
  } = _ref;
  if (!newDate || !Array.isArray(newDate)) return [];
  var dayjsDateList = newDate.map(d => d && parseToDayjs(d, format));
  if (autoSwap && dayjsDateList[0] && dayjsDateList[1] && dayjsDateList[0].toDate().getTime() > dayjsDateList[1].toDate().getTime()) {
    dayjsDateList = [dayjsDateList[1], dayjsDateList[0]];
  }
  if (dayjsDateList.some(r => r && !r.isValid())) {
    log.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(da => da && da.toDate().getTime());
  if (targetFormat === "Date") return dayjsDateList.map(da => da && da.toDate());
  return dayjsDateList.map(da => da && da.format(targetFormat || format));
}
function formatSingle(_ref2) {
  var {
    newDate,
    format,
    targetFormat
  } = _ref2;
  if (!newDate) return "";
  var dayJsDate = parseToDayjs(newDate, format);
  if (!dayJsDate.isValid()) {
    log.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(v => dayjs(v, format).isValid() || dayjs(v).isValid());
  return dayjs(value, format).isValid() || dayjs(value).isValid();
}
function formatDate(newDate, _ref3) {
  var {
    format,
    targetFormat,
    autoSwap
  } = _ref3;
  var result;
  if (Array.isArray(newDate)) result = formatRange({
    newDate,
    format,
    targetFormat,
    autoSwap
  });else result = formatSingle({
    newDate,
    format,
    targetFormat
  });
  return result;
}
function formatTime(value, timeFormat) {
  var result;
  if (Array.isArray(value)) result = value.map(v => dayjs(v || new Date(new Date().setHours(0, 0, 0, 0))).format(timeFormat));else result = dayjs(value || new Date(new Date().setHours(0, 0, 0, 0))).format(timeFormat);
  return result;
}
function getDefaultFormat(_ref4) {
  var {
    mode = "date",
    format,
    valueType,
    enableTimePicker
  } = _ref4;
  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: extractTimeFormat(format || "YYYY-MM-DD".concat(enableTimePicker ? " HH:mm:ss" : "")) || TIME_FORMAT
    };
  }
  log.error("DatePicker", "Invalid mode: ".concat(mode));
  return {};
}

export { formatTime as a, formatDate as f, getDefaultFormat as g, isValidDate as i, parseToDayjs as p };
//# sourceMappingURL=dep-583bcd3d.js.map
