/**
 * ldesign v0.2.2
 * (c) 2022年9月20日 swimly
 * @license MIT
 */

import { d as dayjs } from './dayjs.min-12eb376c.js';
import { b as extractTimeFormat } from './utils2.js';
import { l as log } from './log.js';
import './_commonjsHelpers-ea2bee21.js';
import './defineProperty-28395935.js';
import './_isIndex-1bf92538.js';
import './isObjectLike-2443c6ff.js';

/**
 * LDesignUtility v0.1.1
 * (c) 2022 undefined
 * @license MIT
 */
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;
      }
    }
  }

  return dayjs(dateText, format).isValid() ? dayjs(dateText, format) : dayjs(dateText);
}

function formatRange(_ref) {
  var newDate = _ref.newDate,
      format = _ref.format;
  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();
  })) {
    log.error("DatePicker", "\u8BF7\u68C0\u67E5 format\u3001value \u683C\u5F0F\u662F\u5426\u6709\u6548.\nformat: '".concat(format, "' value: '").concat(newDate, "'"));
    return [];
  }

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

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

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

  return dayJsDate.format(format);
}

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

  if (format === "time-stamp") return dayjs(value).isValid();
  return dayjs(value, format).isValid() || dayjs(value).isValid();
}

function formatDate(newDate, _ref3) {
  var format = _ref3.format;
  var result;

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

  return result;
}

function formatTime(value, timeFormat) {
  var result;

  if (Array.isArray(value)) {
    result = value.map(function (v) {
      return 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 _ref4$mode = _ref4.mode,
      mode = _ref4$mode === void 0 ? "date" : _ref4$mode,
      format = _ref4.format,
      enableTimePicker = _ref4.enableTimePicker;

  if (mode === "year") {
    return {
      format: format || "YYYY",
      timeFormat: TIME_FORMAT
    };
  }

  if (mode === "month") {
    return {
      format: format || "YYYY-MM",
      timeFormat: TIME_FORMAT
    };
  }

  if (mode === "quarter") {
    return {
      format: format || "YYYY-[Q]Q",
      timeFormat: TIME_FORMAT
    };
  }

  if (mode === "week") {
    return {
      format: format || "YYYY-wo",
      timeFormat: TIME_FORMAT
    };
  }

  if (mode === "date") {
    return {
      format: 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 { formatDate as a, formatTime as f, getDefaultFormat as g, isValidDate as i, parseToDayjs as p };
//# sourceMappingURL=format.js.map
