'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var vue = require('vue');
require('../../../utils/index.cjs');
require('../../../hooks/index.cjs');
var index$3 = require('./panels/TimePanel/index.cjs');
var index$1 = require('./panels/DatetimePanel/index.cjs');
var index$2 = require('./panels/DatePanel/index.cjs');
var index$4 = require('./panels/WeekPanel/index.cjs');
var index$6 = require('./panels/MonthPanel/index.cjs');
var index$5 = require('./panels/QuarterPanel/index.cjs');
var index$7 = require('./panels/YearPanel/index.cjs');
var index$8 = require('./panels/DecadePanel/index.cjs');
var dateUtil = require('./utils/dateUtil.cjs');
var PanelContext = require('./PanelContext.cjs');
var uiUtil = require('./utils/uiUtil.cjs');
var RangeContext = require('./RangeContext.cjs');
var getExtraFooter = require('./utils/getExtraFooter.cjs');
var getRanges = require('./utils/getRanges.cjs');
var timeUtil = require('./utils/timeUtil.cjs');
var log = require('../../../utils/log.cjs');
var index = require('../../../hooks/use-merged-state/index.cjs');
var KeyCode = require('../../../utils/KeyCode.cjs');
var _class = require('../../../utils/dom/class.cjs');

function PickerPanel() {
  return /* @__PURE__ */ vue.defineComponent({
    name: "PickerPanel",
    inheritAttrs: false,
    props: {
      prefixCls: String,
      locale: Object,
      generateConfig: Object,
      value: Object,
      defaultValue: Object,
      pickerValue: Object,
      defaultPickerValue: Object,
      disabledDate: Function,
      mode: String,
      picker: {
        type: String,
        default: "date"
      },
      tabindex: {
        type: [Number, String],
        default: 0
      },
      showNow: {
        type: Boolean,
        default: void 0
      },
      showTime: [Boolean, Object],
      showToday: Boolean,
      renderExtraFooter: Function,
      dateRender: Function,
      hideHeader: {
        type: Boolean,
        default: void 0
      },
      onSelect: Function,
      onChange: Function,
      onPanelChange: Function,
      onMousedown: Function,
      onPickerValueChange: Function,
      onOk: Function,
      components: Object,
      direction: String,
      hourStep: {
        type: Number,
        default: 1
      },
      minuteStep: {
        type: Number,
        default: 1
      },
      secondStep: {
        type: Number,
        default: 1
      }
    },
    setup(props, {
      attrs
    }) {
      const needConfirmButton = vue.computed(() => props.picker === "date" && !!props.showTime || props.picker === "time");
      const isHourStepValid = vue.computed(() => 24 % props.hourStep === 0);
      const isMinuteStepValid = vue.computed(() => 60 % props.minuteStep === 0);
      const isSecondStepValid = vue.computed(() => 60 % props.secondStep === 0);
      if (process.env.NODE_ENV !== "production") {
        vue.watchEffect(() => {
          const {
            generateConfig,
            value,
            hourStep = 1,
            minuteStep = 1,
            secondStep = 1
          } = props;
          log.warning(!value || generateConfig.isValidate(value), "Invalidate date pass to `value`.");
          log.warning(!value || generateConfig.isValidate(value), "Invalidate date pass to `defaultValue`.");
          log.warning(isHourStepValid.value, `\`hourStep\` ${hourStep} is invalid. It should be a factor of 24.`);
          log.warning(isMinuteStepValid.value, `\`minuteStep\` ${minuteStep} is invalid. It should be a factor of 60.`);
          log.warning(isSecondStepValid.value, `\`secondStep\` ${secondStep} is invalid. It should be a factor of 60.`);
        });
      }
      const panelContext = PanelContext.useInjectPanel();
      const {
        operationRef,
        onSelect: onContextSelect,
        hideRanges,
        defaultOpenValue
      } = panelContext;
      const {
        inRange,
        panelPosition,
        rangedValue,
        hoverRangedValue
      } = RangeContext.useInjectRange();
      const panelRef = vue.ref({});
      const [mergedValue, setInnerValue] = index.useMergedState(null, {
        value: vue.toRef(props, "value"),
        defaultValue: props.defaultValue,
        postState: (val) => {
          if (!val && (defaultOpenValue == null ? void 0 : defaultOpenValue.value) && props.picker === "time")
            return defaultOpenValue.value;
          return val;
        }
      });
      const [viewDate, setInnerViewDate] = index.useMergedState(null, {
        value: vue.toRef(props, "pickerValue"),
        defaultValue: props.defaultPickerValue || mergedValue.value,
        postState: (date) => {
          const {
            generateConfig,
            showTime,
            defaultValue
          } = props;
          const now = generateConfig.getNow();
          if (!date)
            return now;
          if (!mergedValue.value && props.showTime) {
            if (typeof showTime === "object") {
              return timeUtil.setDateTime(generateConfig, Array.isArray(date) ? date[0] : date, showTime.defaultValue || now);
            }
            if (defaultValue) {
              return timeUtil.setDateTime(generateConfig, Array.isArray(date) ? date[0] : date, defaultValue);
            }
            return timeUtil.setDateTime(generateConfig, Array.isArray(date) ? date[0] : date, now);
          }
          return date;
        }
      });
      const setViewDate = (date) => {
        setInnerViewDate(date);
        if (props.onPickerValueChange)
          props.onPickerValueChange(date);
      };
      const getInternalNextMode = (nextMode) => {
        const getNextMode = uiUtil.PickerModeMap[props.picker];
        if (getNextMode)
          return getNextMode(nextMode);
        return nextMode;
      };
      const [mergedMode, setInnerMode] = index.useMergedState(() => {
        if (props.picker === "time")
          return "time";
        return getInternalNextMode("date");
      }, {
        value: vue.toRef(props, "mode")
      });
      vue.watch(() => props.picker, () => {
        setInnerMode(props.picker);
      });
      const sourceMode = vue.ref(mergedMode.value);
      const setSourceMode = (val) => {
        sourceMode.value = val;
      };
      const onInternalPanelChange = (newMode, viewValue) => {
        const {
          onPanelChange,
          generateConfig
        } = props;
        const nextMode = getInternalNextMode(newMode || mergedMode.value);
        setSourceMode(mergedMode.value);
        setInnerMode(nextMode);
        if (onPanelChange && (mergedMode.value !== nextMode || dateUtil.isEqual(generateConfig, viewDate.value, viewDate.value)))
          onPanelChange(viewValue, nextMode);
      };
      const triggerSelect = (date, type, forceTriggerSelect = false) => {
        const {
          picker,
          generateConfig,
          onSelect,
          onChange,
          disabledDate
        } = props;
        if (mergedMode.value === picker || forceTriggerSelect) {
          setInnerValue(date);
          if (onSelect)
            onSelect(date);
          if (onContextSelect)
            onContextSelect(date, type);
          if (onChange && !dateUtil.isEqual(generateConfig, date, mergedValue.value) && !(disabledDate == null ? void 0 : disabledDate(date)))
            onChange(date);
        }
      };
      const onInternalKeydown = (e) => {
        if (panelRef.value && panelRef.value.onKeydown) {
          if ([KeyCode.KeyCode.LEFT, KeyCode.KeyCode.RIGHT, KeyCode.KeyCode.UP, KeyCode.KeyCode.DOWN, KeyCode.KeyCode.PAGE_UP, KeyCode.KeyCode.PAGE_DOWN, KeyCode.KeyCode.ENTER].includes(e.which))
            e.preventDefault();
          return panelRef.value.onKeydown(e);
        }
        {
          log.warning(false, "Panel not correct handle keyDown event. Please help to fire issue about this.");
          return false;
        }
      };
      const onInternalBlur = (e) => {
        if (panelRef.value && panelRef.value.onBlur)
          panelRef.value.onBlur(e);
      };
      const onNow = () => {
        const {
          generateConfig,
          hourStep,
          minuteStep,
          secondStep
        } = props;
        const now = generateConfig.getNow();
        const lowerBoundTime = timeUtil.getLowerBoundTime(generateConfig.getHour(now), generateConfig.getMinute(now), generateConfig.getSecond(now), isHourStepValid.value ? hourStep : 1, isMinuteStepValid.value ? minuteStep : 1, isSecondStepValid.value ? secondStep : 1);
        const adjustedNow = timeUtil.setTime(
          generateConfig,
          now,
          lowerBoundTime[0],
          // hour
          lowerBoundTime[1],
          // minute
          lowerBoundTime[2]
          // second
        );
        triggerSelect(adjustedNow, "submit");
      };
      const classString = vue.computed(() => {
        const {
          prefixCls,
          direction
        } = props;
        return _class.classNames(`${prefixCls}-panel`, {
          [`${prefixCls}-panel-has-range`]: rangedValue && rangedValue.value && rangedValue.value[0] && rangedValue.value[1],
          [`${prefixCls}-panel-has-range-hover`]: hoverRangedValue && hoverRangedValue.value && hoverRangedValue.value[0] && hoverRangedValue.value[1],
          [`${prefixCls}-panel-rtl`]: direction === "rtl"
        });
      });
      PanelContext.useProvidePanel({
        ...panelContext,
        mode: mergedMode,
        hideHeader: vue.computed(() => {
          var _a;
          return props.hideHeader !== void 0 ? props.hideHeader : (_a = panelContext.hideHeader) == null ? void 0 : _a.value;
        }),
        hidePrevBtn: vue.computed(() => inRange.value && panelPosition.value === "right"),
        hideNextBtn: vue.computed(() => inRange.value && panelPosition.value === "left")
      });
      vue.watch(() => props.value, () => {
        if (props.value)
          setInnerViewDate(props.value);
      });
      return () => {
        const {
          prefixCls = "ant-picker",
          locale,
          generateConfig,
          disabledDate,
          picker = "date",
          tabindex = 0,
          showNow,
          showTime,
          showToday,
          renderExtraFooter,
          onMousedown,
          onOk,
          components
        } = props;
        if (operationRef && panelPosition.value !== "right") {
          operationRef.value = {
            onKeydown: onInternalKeydown,
            onClose: () => {
              if (panelRef.value && panelRef.value.onClose)
                panelRef.value.onClose();
            }
          };
        }
        let panelNode;
        const pickerProps = {
          ...attrs,
          ...props,
          operationRef: panelRef,
          prefixCls,
          viewDate: viewDate.value,
          value: mergedValue.value,
          onViewDateChange: setViewDate,
          sourceMode: sourceMode.value,
          onPanelChange: onInternalPanelChange,
          disabledDate
        };
        delete pickerProps.onChange;
        delete pickerProps.onSelect;
        switch (mergedMode.value) {
          case "decade":
            panelNode = vue.createVNode(index$8.default, {
              ...pickerProps,
              "onSelect": (date, type) => {
                setViewDate(date);
                triggerSelect(date, type);
              }
            }, null);
            break;
          case "year":
            panelNode = vue.createVNode(index$7.default, {
              ...pickerProps,
              "onSelect": (date, type) => {
                setViewDate(date);
                triggerSelect(date, type);
              }
            }, null);
            break;
          case "month":
            panelNode = vue.createVNode(index$6.default, {
              ...pickerProps,
              "onSelect": (date, type) => {
                setViewDate(date);
                triggerSelect(date, type);
              }
            }, null);
            break;
          case "quarter":
            panelNode = vue.createVNode(index$5.default, {
              ...pickerProps,
              "onSelect": (date, type) => {
                setViewDate(date);
                triggerSelect(date, type);
              }
            }, null);
            break;
          case "week":
            panelNode = vue.createVNode(index$4.default, {
              ...pickerProps,
              "onSelect": (date, type) => {
                setViewDate(date);
                triggerSelect(date, type);
              }
            }, null);
            break;
          case "time":
            delete pickerProps.showTime;
            panelNode = vue.createVNode(index$3.default, {
              ...pickerProps,
              ...typeof showTime === "object" ? showTime : null,
              "onSelect": (date, type) => {
                setViewDate(date);
                triggerSelect(date, type);
              }
            }, null);
            break;
          default:
            if (showTime) {
              panelNode = vue.createVNode(index$1.default, {
                ...pickerProps,
                "onSelect": (date, type) => {
                  setViewDate(date);
                  triggerSelect(date, type);
                }
              }, null);
            } else {
              panelNode = vue.createVNode(index$2.default, {
                ...pickerProps,
                "onSelect": (date, type) => {
                  setViewDate(date);
                  triggerSelect(date, type);
                }
              }, null);
            }
        }
        let extraFooter;
        let rangesNode;
        if (!(hideRanges == null ? void 0 : hideRanges.value)) {
          extraFooter = getExtraFooter.default(prefixCls, mergedMode.value, renderExtraFooter);
          rangesNode = getRanges.default({
            prefixCls,
            components,
            needConfirmButton: needConfirmButton.value,
            okDisabled: !mergedValue.value || disabledDate && disabledDate(mergedValue.value),
            locale,
            showNow,
            onNow: needConfirmButton.value && onNow,
            onOk: () => {
              if (mergedValue.value) {
                triggerSelect(mergedValue.value, "submit", true);
                if (onOk)
                  onOk(mergedValue.value);
              }
            }
          });
        }
        let todayNode;
        if (showToday && mergedMode.value === "date" && picker === "date" && !showTime) {
          const now = generateConfig.getNow();
          const todayCls = `${prefixCls}-today-btn`;
          const disabled = disabledDate && disabledDate(now);
          todayNode = vue.createVNode("a", {
            "class": _class.classNames(todayCls, disabled && `${todayCls}-disabled`),
            "aria-disabled": disabled,
            "onClick": () => {
              if (!disabled)
                triggerSelect(now, "mouse", true);
            }
          }, [locale.today]);
        }
        return vue.createVNode("div", {
          "tabindex": tabindex,
          "class": _class.classNames(classString.value, attrs.class),
          "style": attrs.style,
          "onKeydown": onInternalKeydown,
          "onBlur": onInternalBlur,
          "onMousedown": onMousedown
        }, [panelNode, extraFooter || rangesNode || todayNode ? vue.createVNode("div", {
          "class": `${prefixCls}-footer`
        }, [extraFooter, rangesNode, todayNode]) : null]);
      };
    }
  });
}
const InterPickerPanel = PickerPanel();
var VcPickerPanel = (props) => vue.createVNode(InterPickerPanel, props);

exports.default = VcPickerPanel;
//# sourceMappingURL=PickerPanel.cjs.map
