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

'use strict';

var dayjs_minBe98bc0e = require('./dayjs.min-be98bc0e.js');
var utils = require('./utils2.js');
var log = require('./log.js');
require('./isObjectLike-a2de5c2f.js');

/**
 * LDesignUtility v0.2.1
 * (c) 2022 undefined
 * @license MIT
 */
var TIME_FORMAT = "HH:mm:ss";

function parseToDayjs(value, format, timeOfDay) {
  if (value === "") return dayjs_minBe98bc0e.dayjs();
  var dateText = value;

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

    var yearStr = dateText.split(/[-/.]/)[0];
    var weekStr = dateText.split(/[-/.]/)[1];
    var weekFormatStr = format.split(/[-/.]/)[1];
    var firstWeek = dayjs_minBe98bc0e.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_minBe98bc0e.dayjs(dateText).format(format);
    }

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

    var _firstWeek = dayjs_minBe98bc0e.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_minBe98bc0e.dayjs(dateText, format).isValid() ? dayjs_minBe98bc0e.dayjs(dateText, format) : dayjs_minBe98bc0e.dayjs(dateText);
  return result.isValid() ? result : dayjs_minBe98bc0e.dayjs();
}

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

  return dayjsDateList.map(da => da && da.format(format));
}

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

  if (!dayJsDate.isValid()) {
    log.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(v => dayjs_minBe98bc0e.dayjs(v).isValid());
    return value.every(v => dayjs_minBe98bc0e.dayjs(v, format).isValid() || dayjs_minBe98bc0e.dayjs(v).isValid());
  }

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

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

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

  return result;
}

function formatTime(value, timeFormat) {
  var result;

  if (Array.isArray(value)) {
    result = value.map(v => dayjs_minBe98bc0e.dayjs(v || new Date(new Date().setHours(0, 0, 0, 0))).format(timeFormat));
  } else {
    result = dayjs_minBe98bc0e.dayjs(value || new Date(new Date().setHours(0, 0, 0, 0))).format(timeFormat);
  }

  return result;
}

function getDefaultFormat(_ref4) {
  var {
    mode = "date",
    format,
    enableTimePicker
  } = _ref4;

  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: utils.extractTimeFormat(format || "YYYY-MM-DD".concat(enableTimePicker ? " HH:mm:ss" : "")) || TIME_FORMAT
    };
  }

  log.log.error("DatePicker", "Invalid mode: ".concat(mode));
  return {};
}

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