'use strict';

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

var vue = require('vue');
require('../../../../../utils/index.cjs');
var index = require('../DatePanel/index.cjs');
var index$1 = require('../TimePanel/index.cjs');
var miscUtil = require('../../utils/miscUtil.cjs');
var timeUtil = require('../../utils/timeUtil.cjs');
var useMergeProps = require('../../hooks/useMergeProps.cjs');
var KeyCode = require('../../../../../utils/KeyCode.cjs');
var _class = require('../../../../../utils/dom/class.cjs');

const ACTIVE_PANEL = miscUtil.tuple("date", "time");
function DatetimePanel(_props) {
  const props = useMergeProps.default(_props);
  const {
    prefixCls,
    operationRef,
    generateConfig,
    value,
    defaultValue,
    disabledTime,
    showTime,
    onSelect
  } = props;
  const panelPrefixCls = `${prefixCls}-datetime-panel`;
  const activePanel = vue.ref(null);
  const dateOperationRef = vue.ref({});
  const timeOperationRef = vue.ref({});
  const timeProps = typeof showTime === "object" ? {
    ...showTime
  } : {};
  function getNextActive(offset) {
    const activeIndex = ACTIVE_PANEL.indexOf(activePanel.value) + offset;
    const nextActivePanel = ACTIVE_PANEL[activeIndex] || null;
    return nextActivePanel;
  }
  const onBlur = (e) => {
    if (timeOperationRef.value.onBlur)
      timeOperationRef.value.onBlur(e);
    activePanel.value = null;
  };
  operationRef.value = {
    onKeydown: (event) => {
      if (event.which === KeyCode.KeyCode.TAB) {
        const nextActivePanel = getNextActive(event.shiftKey ? -1 : 1);
        activePanel.value = nextActivePanel;
        if (nextActivePanel)
          event.preventDefault();
        return true;
      }
      if (activePanel.value) {
        const ref2 = activePanel.value === "date" ? dateOperationRef : timeOperationRef;
        if (ref2.value && ref2.value.onKeydown)
          ref2.value.onKeydown(event);
        return true;
      }
      if ([KeyCode.KeyCode.LEFT, KeyCode.KeyCode.RIGHT, KeyCode.KeyCode.UP, KeyCode.KeyCode.DOWN].includes(event.which)) {
        activePanel.value = "date";
        return true;
      }
      return false;
    },
    onBlur,
    onClose: onBlur
  };
  const onInternalSelect = (date, source) => {
    let selectedDate = date;
    if (source === "date" && !value && timeProps.defaultValue) {
      selectedDate = generateConfig.setHour(selectedDate, generateConfig.getHour(timeProps.defaultValue));
      selectedDate = generateConfig.setMinute(selectedDate, generateConfig.getMinute(timeProps.defaultValue));
      selectedDate = generateConfig.setSecond(selectedDate, generateConfig.getSecond(timeProps.defaultValue));
    } else if (source === "time" && !value && defaultValue) {
      selectedDate = generateConfig.setYear(selectedDate, generateConfig.getYear(defaultValue));
      selectedDate = generateConfig.setMonth(selectedDate, generateConfig.getMonth(defaultValue));
      selectedDate = generateConfig.setDate(selectedDate, generateConfig.getDate(defaultValue));
    }
    if (onSelect)
      onSelect(selectedDate, "mouse");
  };
  const disabledTimes = disabledTime ? disabledTime(value || null) : {};
  return vue.createVNode("div", {
    "class": _class.classNames(panelPrefixCls, {
      [`${panelPrefixCls}-active`]: activePanel.value
    })
  }, [vue.createVNode(index.default, {
    ...props,
    "operationRef": dateOperationRef,
    "active": activePanel.value === "date",
    "onSelect": (date) => {
      onInternalSelect(timeUtil.setDateTime(generateConfig, date, !value && typeof showTime === "object" ? showTime.defaultValue : null), "date");
    }
  }, null), vue.createVNode(index$1.default, {
    ...props,
    "format": void 0,
    ...timeProps,
    ...disabledTimes,
    "disabledTime": null,
    "defaultValue": void 0,
    "operationRef": timeOperationRef,
    "active": activePanel.value === "time",
    "onSelect": (date) => {
      onInternalSelect(date, "time");
    }
  }, null)]);
}
DatetimePanel.displayName = "DatetimePanel";
DatetimePanel.inheritAttrs = false;

exports.default = DatetimePanel;
//# sourceMappingURL=index.cjs.map
