import { useContext, useMemo } from "react";
import merge_1 from "../../../../../../../../lodash@4.17.21/node_modules/lodash/merge.js";
import cs from "../../../_util/classNames.js";
import { isObject } from "../../../_util/is.js";
import { getTimeFormat, getDayjsValue, methods } from "../../../_util/dayjs.js";
import { ConfigContext } from "../../../ConfigProvider/index.js";
import TimePicker from "../../../TimePicker/time-picker.js";
import Header from "../header.js";
import Body from "../body.js";
import MonthPicker from "../month/index.js";
import YearPicker from "../year/index.js";
import { newArray } from "../../../_util/constant.js";
import PickerContext from "../../context.js";
import { jsx, jsxs } from "../../../../../../../../@emotion_react@11.10.4_3toe27fv7etiytxb5kxc7fxaw4/node_modules/@emotion/react/jsx-runtime/dist/emotion-react-jsx-runtime.browser.esm.js";
var __assign = globalThis && globalThis.__assign || function() {
  __assign = Object.assign || function(t) {
    for (var s, i = 1, n = arguments.length; i < n; i++) {
      s = arguments[i];
      for (var p in s)
        if (Object.prototype.hasOwnProperty.call(s, p))
          t[p] = s[p];
    }
    return t;
  };
  return __assign.apply(this, arguments);
};
var __rest = globalThis && globalThis.__rest || function(s, e) {
  var t = {};
  for (var p in s)
    if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
      t[p] = s[p];
  if (s != null && typeof Object.getOwnPropertySymbols === "function")
    for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
      if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
        t[p[i]] = s[p[i]];
    }
  return t;
};
var __read = globalThis && globalThis.__read || function(o, n) {
  var m = typeof Symbol === "function" && o[Symbol.iterator];
  if (!m)
    return o;
  var i = m.call(o), r, ar = [], e;
  try {
    while ((n === void 0 || n-- > 0) && !(r = i.next()).done)
      ar.push(r.value);
  } catch (error) {
    e = {
      error
    };
  } finally {
    try {
      if (r && !r.done && (m = i["return"]))
        m.call(i);
    } finally {
      if (e)
        throw e.error;
    }
  }
  return ar;
};
var __spreadArray = globalThis && globalThis.__spreadArray || function(to, from, pack) {
  if (pack || arguments.length === 2)
    for (var i = 0, l = from.length, ar; i < l; i++) {
      if (ar || !(i in from)) {
        if (!ar)
          ar = Array.prototype.slice.call(from, 0, i);
        ar[i] = from[i];
      }
    }
  return to.concat(ar || Array.prototype.slice.call(from));
};
var allDaysInOnePage = 6 * 7;
var getReturn = function(time) {
  return {
    year: time.year(),
    month: time.month() + 1,
    day: time.day(),
    name: time.date(),
    time
  };
};
var getTimeObj = function(time) {
  return __assign(__assign({}, getReturn(methods.startOf(time, "month"))), {
    days: time.daysInMonth()
  });
};
function getAllDaysByTime(props, time) {
  var dayStartOfWeek = props.dayStartOfWeek, isWeek = props.isWeek;
  var current = getTimeObj(time);
  var flatRows = newArray(allDaysInOnePage).map(function() {
    return {};
  });
  var startIndex = current.day - dayStartOfWeek < 0 ? 7 + (current.day - dayStartOfWeek) : current.day - dayStartOfWeek;
  flatRows[startIndex] = __assign({}, current);
  for (var i = 0; i < startIndex; i++) {
    flatRows[startIndex - i - 1] = __assign(__assign({}, getReturn(methods.subtract(current.time, i + 1, "day"))), {
      isPrev: true
    });
  }
  for (var i = 0; i < allDaysInOnePage - startIndex - 1; i++) {
    flatRows[startIndex + i + 1] = __assign(__assign({}, getReturn(methods.add(current.time, i + 1, "day"))), {
      isNext: i >= current.days - 1
    });
  }
  var rows = newArray(6).map(function() {
    return [];
  });
  for (var i = 0; i < 6; i++) {
    rows[i] = flatRows.slice(i * 7, 7 * (i + 1));
    if (isWeek) {
      var weekTime = rows[i][0].time;
      var weekRows = __spreadArray([], __read(rows[i]), false);
      rows[i].unshift({
        weekRows,
        weekOfYear: weekTime.week()
      });
    }
  }
  return rows;
}
function DatePicker(props) {
  var isWeek = props.isWeek, popupVisible = props.popupVisible, format = props.format, pageShowDate = props.pageShowDate, showTime = props.showTime, style = props.style, timepickerProps = props.timepickerProps, onMouseEnterCell = props.onMouseEnterCell, onMouseLeaveCell = props.onMouseLeaveCell, dateRender = props.dateRender, disabledDate = props.disabledDate, disabledTime = props.disabledTime, value = props.value, rangeValues = props.rangeValues, locale = props.locale, isRangePicker = props.isRangePicker, onSelect = props.onSelect, onTimePickerSelect = props.onTimePickerSelect, onPrev = props.onPrev, onNext = props.onNext, onSuperPrev = props.onSuperPrev, onSuperNext = props.onSuperNext, isSameTime = props.isSameTime, index = props.index, getHeaderOperations = props.getHeaderOperations, setPageShowDate = props.setPageShowDate, timeValue = props.timeValue, hideNotInViewDates = props.hideNotInViewDates, icons = props.icons, isTimePanel = props.isTimePanel, panelMode = props.panelMode, setPanelMode = props.setPanelMode, rest = __rest(props, ["isWeek", "popupVisible", "format", "pageShowDate", "showTime", "style", "timepickerProps", "onMouseEnterCell", "onMouseLeaveCell", "dateRender", "disabledDate", "disabledTime", "value", "rangeValues", "locale", "isRangePicker", "onSelect", "onTimePickerSelect", "onPrev", "onNext", "onSuperPrev", "onSuperNext", "isSameTime", "index", "getHeaderOperations", "setPageShowDate", "timeValue", "hideNotInViewDates", "icons", "isTimePanel", "panelMode", "setPanelMode"]);
  var _a = useContext(ConfigContext), globalLocale = _a.locale, getPrefixCls = _a.getPrefixCls, rtl = _a.rtl;
  var _b = useContext(PickerContext), utcOffset = _b.utcOffset, timezone = _b.timezone, weekStart = _b.weekStart;
  var DATEPICKER_LOCALE = merge_1(globalLocale.DatePicker, locale);
  var prefixCls = getPrefixCls(isWeek ? "panel-week" : "panel-date");
  var classNames = cs(prefixCls);
  var bodyProps = isRangePicker ? {
    rangeValues
  } : {
    value
  };
  var timeFormat = isObject(showTime) && showTime.format || getTimeFormat(format);
  var dayjsLocale = globalLocale.dayjsLocale;
  var rows = useMemo(function() {
    return getAllDaysByTime(__assign(__assign({}, props), {
      dayStartOfWeek: weekStart
    }), pageShowDate.locale(dayjsLocale));
  }, [pageShowDate.toString(), weekStart, dayjsLocale]);
  var disabledTimeProps;
  if (isRangePicker) {
    disabledTimeProps = typeof disabledTime === "function" ? disabledTime(rangeValues[index], index === 0 ? "start" : "end") : {};
  } else {
    disabledTimeProps = typeof disabledTime === "function" ? disabledTime(getDayjsValue(value, format)) : {};
  }
  function renderCalendar() {
    return /* @__PURE__ */ jsx(Body, {
      ...__assign({}, rest, bodyProps, {
        showWeekList: true,
        isWeek,
        prefixCls: getPrefixCls("picker"),
        rows,
        isSameTime: isSameTime || function(current, target) {
          return current.isSame(target, "day");
        },
        onSelectDate: onSelect,
        onMouseEnterCell,
        onMouseLeaveCell,
        dateRender,
        disabledDate,
        CALENDAR_LOCALE: DATEPICKER_LOCALE.Calendar,
        mode: isWeek ? "week" : "date",
        format,
        hideNotInViewDates
      })
    });
  }
  function renderTimePicker() {
    var showTimeProps = isObject(showTime) ? showTime : {};
    return /* @__PURE__ */ jsxs("div", {
      className: prefixCls + "-timepicker",
      children: [/* @__PURE__ */ jsx("header", {
        className: prefixCls + "-timepicker-title",
        children: DATEPICKER_LOCALE.selectTime
      }), /* @__PURE__ */ jsx(TimePicker, {
        ...__assign({}, timepickerProps, showTimeProps, disabledTimeProps, {
          hideFooter: true,
          format: timeFormat,
          valueShow: timeValue.format(timeFormat),
          onSelect: onTimePickerSelect,
          popupVisible,
          utcOffset,
          timezone
        })
      })]
    });
  }
  var headerOperations = {
    onPrev,
    onSuperPrev,
    onNext,
    onSuperNext
  };
  function onChangePanel(mode) {
    setPanelMode(mode);
  }
  if (panelMode === "year") {
    return /* @__PURE__ */ jsx(YearPicker, {
      ...__assign({}, getHeaderOperations(panelMode), {
        pageShowDate,
        onSelect: function(_, date) {
          setPanelMode("month");
          setPageShowDate(date);
        },
        disabledDate,
        originMode: "date"
      })
    });
  }
  if (panelMode === "month") {
    return /* @__PURE__ */ jsx(MonthPicker, {
      ...__assign({}, getHeaderOperations(panelMode), {
        setPageShowDate,
        pageShowDate,
        panelMode,
        getHeaderOperations,
        onSelect: function(_, date) {
          setPanelMode("date");
          setPageShowDate(date);
        },
        disabledDate,
        originMode: "date",
        setPanelMode
      })
    });
  }
  return /* @__PURE__ */ jsx("div", {
    className: classNames,
    style,
    children: showTime && isTimePanel ? renderTimePicker() : /* @__PURE__ */ jsxs("div", {
      className: prefixCls + "-inner",
      children: [/* @__PURE__ */ jsx(Header, {
        ...__assign({}, headerOperations, {
          icons,
          prefixCls: getPrefixCls("picker"),
          value: pageShowDate,
          mode: panelMode,
          onChangePanel,
          rtl
        })
      }), renderCalendar()]
    })
  });
}
DatePicker.defaultProps = {
  pickerType: "date"
};
export {
  DatePicker as default
};
//# sourceMappingURL=index.js.map
