"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const shared_esmBundler = require("../../../../../@vue/shared/dist/shared.esm-bundler.js");
const utils = require("../../time-picker/src/utils.js");
const dayjs_min = require("../../../../../../_virtual/dayjs.min.js");
const isValidRange = (range) => {
  if (!shared_esmBundler.isArray(range)) return false;
  const [left, right] = range;
  return dayjs_min.default.isDayjs(left) && dayjs_min.default.isDayjs(right) && dayjs_min.default(left).isValid() && dayjs_min.default(right).isValid() && left.isSameOrBefore(right);
};
const getDefaultValue = (defaultValue, { lang, step = 1, unit, unlinkPanels }) => {
  let start;
  if (shared_esmBundler.isArray(defaultValue)) {
    let [left, right] = defaultValue.map((d) => dayjs_min.default(d).locale(lang));
    if (!unlinkPanels) right = left.add(step, unit);
    return [left, right];
  } else if (defaultValue) start = dayjs_min.default(defaultValue);
  else start = dayjs_min.default();
  start = start.locale(lang);
  return [start, start.add(step, unit)];
};
const buildPickerTable = (dimension, rows, { columnIndexOffset, startDate, nextEndDate, now, unit, relativeDateGetter, setCellMetadata, setRowMetadata }) => {
  for (let rowIndex = 0; rowIndex < dimension.row; rowIndex++) {
    const row = rows[rowIndex];
    for (let columnIndex = 0; columnIndex < dimension.column; columnIndex++) {
      let cell = row[columnIndex + columnIndexOffset];
      if (!cell) cell = {
        row: rowIndex,
        column: columnIndex,
        type: "normal",
        inRange: false,
        start: false,
        end: false
      };
      const nextStartDate = relativeDateGetter(rowIndex * dimension.column + columnIndex);
      cell.dayjs = nextStartDate;
      cell.date = nextStartDate.toDate();
      cell.timestamp = nextStartDate.valueOf();
      cell.type = "normal";
      cell.inRange = !!(startDate && nextStartDate.isSameOrAfter(startDate, unit) && nextEndDate && nextStartDate.isSameOrBefore(nextEndDate, unit)) || !!(startDate && nextStartDate.isSameOrBefore(startDate, unit) && nextEndDate && nextStartDate.isSameOrAfter(nextEndDate, unit));
      if (startDate == null ? void 0 : startDate.isSameOrAfter(nextEndDate)) {
        cell.start = !!nextEndDate && nextStartDate.isSame(nextEndDate, unit);
        cell.end = startDate && nextStartDate.isSame(startDate, unit);
      } else {
        cell.start = !!startDate && nextStartDate.isSame(startDate, unit);
        cell.end = !!nextEndDate && nextStartDate.isSame(nextEndDate, unit);
      }
      if (nextStartDate.isSame(now, unit)) cell.type = "today";
      setCellMetadata == null ? void 0 : setCellMetadata(cell, {
        rowIndex,
        columnIndex
      });
      row[columnIndex + columnIndexOffset] = cell;
    }
    setRowMetadata == null ? void 0 : setRowMetadata(row);
  }
};
const datesInMonth = (date, year, month, lang) => {
  const firstDay = dayjs_min.default().locale(lang).startOf("month").month(month).year(year).hour(date.hour()).minute(date.minute()).second(date.second());
  return utils.rangeArr(firstDay.daysInMonth()).map((n) => firstDay.add(n, "day").toDate());
};
const getValidDateOfMonth = (date, year, month, lang, disabledDate) => {
  const _value = dayjs_min.default().year(year).month(month).startOf("month").hour(date.hour()).minute(date.minute()).second(date.second());
  const _date = datesInMonth(date, year, month, lang).find((date2) => {
    return !(disabledDate == null ? void 0 : disabledDate(date2));
  });
  if (_date) return dayjs_min.default(_date).locale(lang);
  return _value.locale(lang);
};
const getValidDateOfYear = (value, lang, disabledDate) => {
  const year = value.year();
  if (!(disabledDate == null ? void 0 : disabledDate(value.toDate()))) return value.locale(lang);
  const month = value.month();
  if (!datesInMonth(value, year, month, lang).every(disabledDate)) return getValidDateOfMonth(value, year, month, lang, disabledDate);
  for (let i = 0; i < 12; i++) if (!datesInMonth(value, year, i, lang).every(disabledDate)) return getValidDateOfMonth(value, year, i, lang, disabledDate);
  return value;
};
const correctlyParseUserInput = (value, format, lang, defaultFormat) => {
  if (shared_esmBundler.isArray(value)) return value.map((v) => correctlyParseUserInput(v, format, lang, defaultFormat));
  if (shared_esmBundler.isString(value)) {
    const dayjsValue = (defaultFormat == null ? void 0 : defaultFormat.value) ? dayjs_min.default(value) : dayjs_min.default(value, format);
    if (!dayjsValue.isValid()) return dayjsValue;
  }
  return dayjs_min.default(value, format).locale(lang);
};
exports.buildPickerTable = buildPickerTable;
exports.correctlyParseUserInput = correctlyParseUserInput;
exports.datesInMonth = datesInMonth;
exports.getDefaultValue = getDefaultValue;
exports.getValidDateOfMonth = getValidDateOfMonth;
exports.getValidDateOfYear = getValidDateOfYear;
exports.isValidRange = isValidRange;
