"use strict";
const React = require("react");
const index$3 = require("../Trigger/index.js");
const input = require("../_class/picker/input.js");
const is = require("../_util/is.js");
const classNames = require("../_util/classNames.js");
const index = require("../ConfigProvider/index.js");
const omit = require("../_util/omit.js");
const dayjs = require("../_util/dayjs.js");
const index$2 = require("../../icon/react-icon/IconCalendar/index.js");
const index$1 = require("../../icon/react-icon/IconCalendarClock/index.js");
const footer = require("./panels/footer.js");
const shortcuts = require("./panels/shortcuts.js");
const useMergeProps = require("../_util/hooks/useMergeProps.js");
const context = require("./context.js");
const usePrevious = require("../_util/hooks/usePrevious.js");
const useUpdate = require("../_util/hooks/useUpdate.js");
const util = require("./util.js");
const pick = require("../_util/pick.js");
const emotionReactJsxRuntime_browser_esm = require("../../../../../../@emotion_react@11.10.4_3toe27fv7etiytxb5kxc7fxaw4/node_modules/@emotion/react/jsx-runtime/dist/emotion-react-jsx-runtime.browser.esm.js");
const _interopDefaultLegacy = (e) => e && typeof e === "object" && "default" in e ? e : { default: e };
const React__default = /* @__PURE__ */ _interopDefaultLegacy(React);
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 __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;
};
function getFormat(props) {
  var format = props.format, picker = props.picker, showTime = props.showTime;
  var valueFormat;
  switch (picker.props.pickerType) {
    case "date":
      valueFormat = showTime ? "YYYY-MM-DD HH:mm:ss" : "YYYY-MM-DD";
      break;
    case "month":
      valueFormat = "YYYY-MM";
      break;
    case "year":
      valueFormat = "YYYY";
      break;
    case "week":
      valueFormat = "gggg-wo";
      break;
    case "quarter":
      valueFormat = "YYYY-[Q]Q";
      break;
    default:
      valueFormat = "YYYY-MM-DD";
  }
  if (format) {
    valueFormat = format;
  }
  return valueFormat;
}
var defaultProps = {
  allowClear: true,
  unmountOnExit: true,
  position: "bl",
  editable: true,
  showNowBtn: true
};
var Picker = function(baseProps) {
  var _a = React.useContext(index.ConfigContext), getPrefixCls = _a.getPrefixCls, locale = _a.locale, ctxSize = _a.size, componentConfig = _a.componentConfig, rtl = _a.rtl;
  if (rtl) {
    defaultProps.position = "br";
  }
  var props = useMergeProps(baseProps, defaultProps, componentConfig === null || componentConfig === void 0 ? void 0 : componentConfig.DatePicker);
  var allowClear = props.allowClear, className = props.className, style = props.style, placeholder = props.placeholder, getPopupContainer = props.getPopupContainer, disabled = props.disabled, position = props.position, error = props.error, unmountOnExit = props.unmountOnExit, editable = props.editable, triggerProps = props.triggerProps, picker = props.picker, shortcuts$1 = props.shortcuts, onSelect = props.onSelect, onVisibleChange = props.onVisibleChange, propsValue = props.value, onChange = props.onChange, icons = props.icons, disabledDate = props.disabledDate, showTime = props.showTime, showNowBtn = props.showNowBtn, onSelectShortcut = props.onSelectShortcut, extra = props.extra, shortcutsPlacementLeft = props.shortcutsPlacementLeft, onOk = props.onOk, defaultPickerValue = props.defaultPickerValue, pickerValue = props.pickerValue, onPickerValueChange = props.onPickerValueChange, triggerElement = props.triggerElement, utcOffset = props.utcOffset, timezone = props.timezone, panelRender = props.panelRender;
  var prefixCls = getPrefixCls("picker");
  var DATEPICKER_LOCALE = locale.DatePicker;
  var weekStart = is.isUndefined(props.dayStartOfWeek) ? util.getDefaultWeekStart(locale.dayjsLocale) : props.dayStartOfWeek;
  var mode = picker.props.pickerType;
  var refInput = React.useRef(null);
  var refPanel = React.useRef(null);
  var refShortcuts = React.useRef(null);
  var realFormat = getFormat(props);
  var format = realFormat;
  if (typeof format === "function") {
    format = showTime ? "YYYY-MM-DD HH:mm:ss" : "YYYY-MM-DD";
  }
  function getDefaultValue() {
    var value2;
    if (props.value) {
      value2 = dayjs.getDayjsValue(props.value, format, utcOffset, timezone);
    } else {
      value2 = dayjs.getDayjsValue(props.defaultValue, format, utcOffset, timezone);
    }
    return value2;
  }
  var _b = __read(React.useState(getDefaultValue()), 2), value = _b[0], setValue = _b[1];
  var _c = __read(React.useState(props.popupVisible), 2), popupVisible = _c[0], setPopupVisible = _c[1];
  var _d = __read(React.useState(false), 2), isTimePanel = _d[0], setIsTimePanel = _d[1];
  var _e = __read(React.useState(), 2), hoverPlaceholderValue = _e[0], setHoverPlaceholderValue = _e[1];
  var mergedPopupVisible = "popupVisible" in props ? props.popupVisible : popupVisible;
  var mergedValue = "value" in props ? dayjs.getDayjsValue(propsValue, format, utcOffset, timezone) : value;
  var defaultPageShowDate = mergedValue || dayjs.getDayjsValue(defaultPickerValue, format) || dayjs.getNow();
  var _f = __read(React.useState(), 2), inputValue = _f[0], setInputValue = _f[1];
  var _g = __read(React.useState(), 2), valueShow = _g[0], setValueShow = _g[1];
  var _h = __read(React.useState(), 2), shortcutValue = _h[0], setShortcutValue = _h[1];
  var _j = __read(React.useState(defaultPageShowDate), 2), pageShowDate = _j[0], setPageShowDate = _j[1];
  var mergedPageShowDate = dayjs.getDayjsValue(pickerValue, format) || pageShowDate;
  var panelValue = shortcutValue || valueShow || mergedValue;
  var _k = __read(React.useState(mode), 2), panelMode = _k[0], setPanelMode = _k[1];
  var defaultTimeValue = is.isObject(showTime) && dayjs.getDayjsValue(showTime.defaultValue, showTime.format || "HH:mm:ss", utcOffset, timezone) || dayjs.getNow(utcOffset, timezone);
  var timeValue = panelValue || defaultTimeValue;
  function focusInput() {
    refInput.current && refInput.current.blur && refInput.current.focus();
  }
  function blurInput() {
    refInput.current && refInput.current.blur && refInput.current.blur();
  }
  var previousUtcOffset = usePrevious(utcOffset);
  var previousTimezone = usePrevious(timezone);
  useUpdate(function() {
    if (value && (previousUtcOffset !== utcOffset || timezone !== previousTimezone)) {
      var localValue = dayjs.toLocal(value, previousUtcOffset, previousTimezone);
      setValue(dayjs.toTimezone(localValue, utcOffset, timezone));
    }
  }, [utcOffset, previousUtcOffset, timezone, previousTimezone]);
  React.useEffect(function() {
    setInputValue(void 0);
    setHoverPlaceholderValue(void 0);
    if (mergedPopupVisible) {
      setPageShowDate(defaultPageShowDate);
      if (shortcutsPlacementLeft) {
        refShortcuts.current.style.maxHeight = refPanel.current.clientHeight + "px";
      }
    } else {
      setValueShow(void 0);
      setShortcutValue(void 0);
      setTimeout(function() {
        setIsTimePanel(false);
        setPanelMode(mode);
        blurInput();
      }, 100);
    }
  }, [mergedPopupVisible]);
  function visibleChange(visible) {
    if (visible) {
      setOpen(visible, function() {
        focusInput();
      });
    } else {
      setOpen(false);
    }
  }
  function handlePickerValueChange(v) {
    onPickerValueChange && onPickerValueChange(v.format(format), v);
  }
  function setOpen(visible, callback) {
    setPopupVisible(visible);
    onVisibleChange && onVisibleChange(visible);
    callback && callback();
  }
  function onClear(e) {
    e.stopPropagation();
    setValue(void 0);
    setValueShow(void 0);
    onHandleChange(void 0);
    props.onClear && props.onClear();
  }
  function onClickConfirmBtn() {
    var pv = util.getLocaleDayjsValue(panelValue, locale.dayjsLocale);
    onConfirmValue();
    onOk && onOk(pv && pv.format(format), pv);
  }
  function onConfirmValue() {
    setValue(panelValue);
    onHandleChange(panelValue);
    setOpen(false);
  }
  function onHandleSelect(_, date, now) {
    setInputValue(void 0);
    setHoverPlaceholderValue(void 0);
    if (showTime) {
      var newTime = now ? date : dayjs.getValueWithTime(date, timeValue);
      setValueShow(newTime);
      setPageShowDate(newTime);
      var localTime = util.getLocaleDayjsValue(dayjs.toLocal(newTime, utcOffset, timezone), locale.dayjsLocale);
      onSelect && onSelect(localTime.format(format), localTime);
    } else {
      var localTime = util.getLocaleDayjsValue(dayjs.toLocal(date, utcOffset, timezone).locale(locale.dayjsLocale), locale.dayjsLocale);
      onSelect && onSelect(localTime ? localTime.format(format) : void 0, localTime);
      setValue(date);
      onHandleChange(date);
      setOpen(false);
    }
  }
  function onHandleChange(newValue) {
    if (dayjs.isDayjsChange(newValue, mergedValue)) {
      var localValue = util.getLocaleDayjsValue(dayjs.toLocal(newValue, utcOffset, timezone), locale.dayjsLocale);
      onChange && onChange(localValue ? localValue.format(format) : void 0, localValue);
    }
  }
  function onTimePickerSelect(_, time) {
    var _valueShow = panelValue || dayjs.getNow(utcOffset, timezone);
    var newValueShow = dayjs.getValueWithTime(_valueShow, time);
    setValueShow(newValueShow);
    var localNewValueShow = util.getLocaleDayjsValue(dayjs.toLocal(newValueShow, utcOffset, timezone), locale.dayjsLocale);
    onSelect && onSelect(localNewValueShow.format(format), localNewValueShow);
  }
  function isValid(time) {
    return typeof time === "string" && dayjs.dayjs(time, format).format(format) === time && (typeof disabledDate === "function" ? !disabledDate(dayjs.dayjs(time, format)) : true);
  }
  function onChangeInput(e) {
    var niv = e.target.value;
    setInputValue(niv);
    if (!mergedPopupVisible) {
      setOpen(true);
    }
    if (isValid(niv)) {
      var newValue = dayjs.getDayjsValue(niv, format, utcOffset, timezone);
      setValueShow(newValue);
      setPageShowDate(newValue);
      setInputValue(void 0);
    }
  }
  function onPressEnter() {
    if (panelValue) {
      onConfirmValue();
      blurInput();
    } else if (mergedPopupVisible) {
      setOpen(false);
    }
  }
  function changePageShowDate(type, unit, num) {
    if (num === void 0) {
      num = 1;
    }
    var newPageShowDate;
    if (type === "prev") {
      newPageShowDate = dayjs.methods.subtract(mergedPageShowDate, num, unit);
    }
    if (type === "next") {
      newPageShowDate = dayjs.methods.add(mergedPageShowDate, num, unit);
    }
    handlePickerValueChange(newPageShowDate);
    setPageShowDate(newPageShowDate);
  }
  function getHeaderOperations(pickMode) {
    if (pickMode === void 0) {
      pickMode = mode;
    }
    if (pickMode === "date" || pickMode === "week") {
      return {
        onPrev: function() {
          return changePageShowDate("prev", "month");
        },
        onNext: function() {
          return changePageShowDate("next", "month");
        },
        onSuperPrev: function() {
          return changePageShowDate("prev", "year");
        },
        onSuperNext: function() {
          return changePageShowDate("next", "year");
        }
      };
    }
    if (pickMode === "month" || pickMode === "quarter") {
      return {
        onSuperPrev: function() {
          return changePageShowDate("prev", "year");
        },
        onSuperNext: function() {
          return changePageShowDate("next", "year");
        }
      };
    }
    if (pickMode === "year") {
      return {
        onSuperPrev: function() {
          return changePageShowDate("prev", "year", 10);
        },
        onSuperNext: function() {
          return changePageShowDate("next", "year", 10);
        }
      };
    }
  }
  function onSelectNow() {
    var now = util.getLocaleDayjsValue(dayjs.getNow(utcOffset, timezone), locale.dayjsLocale);
    handlePickerValueChange(now);
    onHandleSelect(now.format(format), now, true);
  }
  function onMouseEnterCell(value2, disabled2) {
    if (!disabled2) {
      var placeHolderValue = showTime ? dayjs.getValueWithTime(value2, timeValue) : value2;
      setHoverPlaceholderValue(typeof realFormat === "function" ? realFormat(value2) : placeHolderValue.locale(locale.dayjsLocale).format(format));
    }
  }
  function onMouseLeaveCell() {
    setHoverPlaceholderValue(void 0);
  }
  function onMouseEnterShortcut(shortcut) {
    if (typeof shortcut.value === "function" && is.isDayjs(shortcut.value())) {
      var sv = dayjs.getDayjsValue(shortcut.value(), format, utcOffset, timezone);
      setPageShowDate(sv);
      handlePickerValueChange(sv);
      setShortcutValue(sv);
    }
  }
  function onMouseLeaveShortcut() {
    var newValue = valueShow || mergedValue || dayjs.getNow(utcOffset, timezone);
    setShortcutValue(void 0);
    setPageShowDate(newValue);
    handlePickerValueChange(newValue);
  }
  function onHandleSelectShortcut(shortcut) {
    onSelectShortcut && onSelectShortcut(shortcut);
    if (typeof shortcut.value === "function" && is.isDayjs(shortcut.value())) {
      var time = dayjs.getDayjsValue(shortcut.value(), format, utcOffset, timezone);
      setValue(time);
      onHandleChange(time);
      setOpen(false);
    }
  }
  function onClickSelectTimeBtn() {
    setIsTimePanel(!isTimePanel);
  }
  function renderPopup(panelOnly) {
    var _a2;
    var classNames$1 = classNames(prefixCls + "-container", (_a2 = {}, _a2[prefixCls + "-panel-only"] = panelOnly, _a2[prefixCls + "-container-shortcuts-placement-left"] = is.isArray(shortcuts$1) && shortcutsPlacementLeft, _a2[prefixCls + "-container-rtl"] = rtl, _a2), panelOnly ? className : "");
    var shortcutsProps = {
      prefixCls,
      showTime,
      shortcuts: shortcuts$1,
      onSelectNow,
      showNowBtn,
      onMouseEnterShortcut,
      onMouseLeaveShortcut,
      onSelectShortcut: onHandleSelectShortcut
    };
    var shouldShowFooter = showTime && panelMode === "date" || extra || is.isArray(shortcuts$1) && shortcuts$1.length && !shortcutsPlacementLeft || !showTime && panelMode === "date" && showNowBtn;
    var content = /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsxs(emotionReactJsxRuntime_browser_esm.Fragment, {
      children: [React__default.default.cloneElement(picker, __assign(__assign(__assign({}, omit(props, ["style"])), getHeaderOperations()), {
        getHeaderOperations,
        onSelect: onHandleSelect,
        onTimePickerSelect,
        onSelectNow,
        popupVisible: mergedPopupVisible,
        format,
        value: panelValue,
        pageShowDate: mergedPageShowDate,
        localeName: locale.dayjsLocale,
        setPageShowDate: function(v) {
          setPageShowDate(v);
          handlePickerValueChange(v);
        },
        timeValue,
        isTimePanel,
        panelMode,
        setPanelMode,
        onMouseEnterCell,
        onMouseLeaveCell
      })), shouldShowFooter && /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(footer, {
        ...__assign({}, shortcutsProps, {
          DATEPICKER_LOCALE,
          disabled: !panelValue,
          onClickConfirmBtn,
          extra,
          mode: panelMode,
          shortcutsPlacementLeft,
          onClickSelectTimeBtn,
          isTimePanel
        })
      })]
    });
    var contentWithShortcuts = shortcutsPlacementLeft ? /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsxs(emotionReactJsxRuntime_browser_esm.Fragment, {
      children: [/* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(shortcuts, {
        ...__assign({
          ref: refShortcuts
        }, shortcutsProps)
      }), /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx("div", {
        ref: refPanel,
        className: prefixCls + "-panel-wrapper",
        children: content
      })]
    }) : content;
    var panelNode = typeof panelRender === "function" ? panelRender(contentWithShortcuts) : contentWithShortcuts;
    return /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx("div", {
      className: classNames$1,
      onClick: function() {
        refInput.current && refInput.current.focus && refInput.current.focus();
      },
      style: panelOnly ? style : {},
      children: panelNode
    });
  }
  var size = props.size || ctxSize;
  var suffixIcon = icons && icons.inputSuffix === null ? null : icons && icons.inputSuffix || (showTime ? /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(index$1, {}) : /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(index$2, {}));
  var baseInputProps = {
    style,
    className,
    popupVisible: mergedPopupVisible,
    format: realFormat,
    disabled,
    error,
    size,
    onPressEnter,
    onClear,
    suffixIcon,
    editable: editable && typeof realFormat !== "function",
    allowClear
  };
  return /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(context.Provider, {
    value: {
      utcOffset,
      timezone,
      weekStart
    },
    children: triggerElement === null ? renderPopup(true) : /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(index$3.default, {
      ...__assign({
        popup: renderPopup,
        trigger: "click",
        clickToClose: false,
        position,
        disabled,
        popupAlign: {
          bottom: 4
        },
        getPopupContainer,
        onVisibleChange: visibleChange,
        popupVisible: mergedPopupVisible,
        classNames: "slideDynamicOrigin",
        unmountOnExit
      }, triggerProps),
      children: triggerElement || /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(input, {
        ...__assign({}, pick.pickDataAttributes(props), baseInputProps, {
          ref: refInput,
          placeholder: placeholder || DATEPICKER_LOCALE.placeholder[mode],
          popupVisible: mergedPopupVisible,
          value: valueShow || mergedValue,
          inputValue: hoverPlaceholderValue || inputValue,
          prefixCls,
          onChange: onChangeInput,
          isPlaceholder: !!hoverPlaceholderValue
        })
      })
    })
  });
};
const Picker$1 = Picker;
module.exports = Picker$1;
//# sourceMappingURL=picker.js.map
