"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
var _vue = require("vue");
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _DateBody = _interopRequireDefault(require("./DateBody"));
var _DateHeader = _interopRequireDefault(require("./DateHeader"));
var _dateUtil = require("../../utils/dateUtil");
var _uiUtil = require("../../utils/uiUtil");
var _classNames = _interopRequireDefault(require("../../../_util/classNames"));
var _useMergeProps = _interopRequireDefault(require("../../hooks/useMergeProps"));
var _PanelContext = require("../../PanelContext");
const DATE_ROW_COUNT = 6;
function DatePanel(_props) {
  const props = (0, _useMergeProps.default)(_props);
  const {
    prefixCls,
    panelName = 'date',
    keyboardConfig,
    active,
    operationRef,
    generateConfig,
    value,
    viewDate,
    onViewDateChange,
    onPanelChange,
    onSelect,
    showWeek,
    disabledDate,
    locale
  } = props;
  const panelPrefixCls = `${prefixCls}-${panelName}-panel`;
  const cellPrefixCls = `${prefixCls}-cell`;
  const {
    onDateMouseenter,
    onDateMouseleave,
    mode
  } = (0, _PanelContext.useInjectPanel)();
  const showPrefixColumn = (0, _vue.computed)(() => showWeek === undefined ? mode.value === 'week' : showWeek);
  // ======================= Keyboard =======================
  operationRef.value = {
    onKeydown: event => (0, _uiUtil.createKeydownHandler)(event, (0, _extends2.default)({
      onLeftRight: diff => {
        onSelect(generateConfig.addDate(value || viewDate, diff), 'key');
      },
      onCtrlLeftRight: diff => {
        onSelect(generateConfig.addYear(value || viewDate, diff), 'key');
      },
      onUpDown: diff => {
        onSelect(generateConfig.addDate(value || viewDate, diff * _dateUtil.WEEK_DAY_COUNT), 'key');
      },
      onPageUpDown: diff => {
        onSelect(generateConfig.addMonth(value || viewDate, diff), 'key');
      }
    }, keyboardConfig))
  };
  // ==================== View Operation ====================
  const onYearChange = diff => {
    const newDate = generateConfig.addYear(viewDate, diff);
    onViewDateChange(newDate);
    onPanelChange(null, newDate);
  };
  const onMonthChange = diff => {
    const newDate = generateConfig.addMonth(viewDate, diff);
    onViewDateChange(newDate);
    onPanelChange(null, newDate);
  };
  const prefixColumn = showPrefixColumn.value ? date => {
    const disabled = disabledDate === null || disabledDate === void 0 ? void 0 : disabledDate(date);
    return (0, _vue.createVNode)("td", {
      "key": "week",
      "class": (0, _classNames.default)(cellPrefixCls, `${cellPrefixCls}-week`, {
        [`${cellPrefixCls}-disabled`]: disabled
      }),
      "onClick": e => {
        e.stopPropagation();
        if (!disabled) {
          onSelect(date, 'mouse');
        }
      },
      "onMouseenter": () => {
        if (!disabled && onDateMouseenter) {
          onDateMouseenter(date);
        }
      },
      "onMouseleave": () => {
        if (!disabled && onDateMouseleave) {
          onDateMouseleave(date);
        }
      }
    }, [(0, _vue.createVNode)("div", {
      "class": `${cellPrefixCls}-inner`
    }, [generateConfig.locale.getWeek(locale.locale, date)])]);
  } : null;
  return (0, _vue.createVNode)("div", {
    "class": (0, _classNames.default)(panelPrefixCls, {
      [`${panelPrefixCls}-active`]: active,
      [`${panelPrefixCls}-show-week`]: showWeek
    })
  }, [(0, _vue.createVNode)(_DateHeader.default, (0, _objectSpread2.default)((0, _objectSpread2.default)({}, props), {}, {
    "prefixCls": prefixCls,
    "value": value,
    "viewDate": viewDate,
    "onPrevYear": () => {
      onYearChange(-1);
    },
    "onNextYear": () => {
      onYearChange(1);
    },
    "onPrevMonth": () => {
      onMonthChange(-1);
    },
    "onNextMonth": () => {
      onMonthChange(1);
    },
    "onMonthClick": () => {
      onPanelChange('month', viewDate);
    },
    "onYearClick": () => {
      onPanelChange('year', viewDate);
    }
  }), null), (0, _vue.createVNode)(_DateBody.default, (0, _objectSpread2.default)((0, _objectSpread2.default)({}, props), {}, {
    "onSelect": date => onSelect(date, 'mouse'),
    "prefixCls": prefixCls,
    "value": value,
    "viewDate": viewDate,
    "rowCount": DATE_ROW_COUNT,
    "prefixColumn": prefixColumn
  }), null)]);
}
DatePanel.displayName = 'DatePanel';
DatePanel.inheritAttrs = false;
var _default = exports.default = DatePanel;