var _excluded = ["type", "min", "max", "value", "onChange", "className"];
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
import React, { forwardRef, useRef, useImperativeHandle } from 'react';
import dayjs from 'dayjs';
import Combo from "./../Combo";

// 内库使用-start
import DateUtil from "./../../../utils/DateUtil";
import Input from "./../../Input";
// 内库使用-end

/* 测试使用-start
import { DateUtil, Input } from 'seedsui-react'
测试使用-end */

// 日期类型选择控件: 年月日季
var DateCombo = /*#__PURE__*/forwardRef(function (_ref, ref) {
  var type = _ref.type,
    min = _ref.min,
    max = _ref.max,
    value = _ref.value,
    onChange = _ref.onChange,
    className = _ref.className,
    props = _objectWithoutProperties(_ref, _excluded);
  // 显示文本
  var displayValue = DateUtil.format(value, type);
  var rootRef = useRef(null);
  useImperativeHandle(ref, function () {
    return {
      rootDOM: rootRef === null || rootRef === void 0 ? void 0 : rootRef.current,
      getRootDOM: function getRootDOM() {
        return rootRef === null || rootRef === void 0 ? void 0 : rootRef.current;
      },
      // 显示文本
      displayValue: displayValue,
      getDisplayValue: function getDisplayValue() {
        return displayValue;
      }
    };
  });

  // 向前
  function handlePrev(e) {
    if (!value) return;
    var newValue = updateValue(value, -1);
    onChange && onChange(newValue);
  }

  // 向后
  function handleNext(e) {
    if (!value) return;
    var newValue = updateValue(value, 1);
    onChange && onChange(newValue);
  }

  /**
   * 切换日期
   * @param {Date} newValue 日期
   * @param {Number} go 前行后退, 0: 当前; -1: 后退; 1: 前进;
   */
  function updateValue(value) {
    var go = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
    var newValue = value;
    if (['year', 'quarter', 'month', 'date'].includes(type) === false) {
      return;
    }
    if (newValue instanceof Date === false) {
      newValue = new Date();
    }
    if (type === 'date') {
      // eslint-disable-next-line
      type = 'day';
    }
    if (go === -1) {
      return dayjs(newValue).subtract(1, type).toDate();
    }
    if (go === 1) {
      return dayjs(newValue).add(1, type).toDate();
    }
    return newValue;
  }
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Input.IconLeftArrow, {
    className: "datepicker-types-previous",
    onClick: handlePrev
  }), /*#__PURE__*/React.createElement(Combo, _extends({}, props, {
    value: value,
    type: type,
    className: "datepicker-types-date".concat(className ? ' ' + className : ''),
    onChange: onChange
  }), /*#__PURE__*/React.createElement("p", null, displayValue || '')), /*#__PURE__*/React.createElement(Input.IconRightArrow, {
    className: "datepicker-types-next",
    onClick: handleNext
  }));
});
export default DateCombo;