"use strict";

exports.__esModule = true;
exports["default"] = void 0;

var _react = _interopRequireWildcard(require("react"));

var _Tabbar = _interopRequireDefault(require("./../Tabbar"));

var _InputDate = _interopRequireDefault(require("./../InputDate"));

var _locale = _interopRequireDefault(require("./../locale"));

var _validateDate = _interopRequireDefault(require("./../InputDate/validateDate"));

var _excluded = ["list", "listVisible", "activeIndex", "onError", "inputpickerProps", "selected", "value", "onChange"];

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _extends() { _extends = Object.assign || 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 _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; }

// 日期类型选择控件: 年月日季
var DateType = (0, _react.forwardRef)(function (_ref, ref) {
  var _ref$list = _ref.list,
      list = _ref$list === void 0 ? [{
    type: 'date',
    id: 'date',
    name: (0, _locale["default"])('日', 'datetype_unit_date')
  }, {
    type: 'month',
    id: 'month',
    name: (0, _locale["default"])('月', 'datetype_unit_month')
  }, {
    type: 'quarter',
    id: 'quarter',
    name: (0, _locale["default"])('季', 'datetype_unit_quarter')
  }, {
    type: 'year',
    id: 'year',
    name: (0, _locale["default"])('年', 'datetype_unit_year')
  }] : _ref$list,
      _ref$listVisible = _ref.listVisible,
      listVisible = _ref$listVisible === void 0 ? true : _ref$listVisible,
      _ref$activeIndex = _ref.activeIndex,
      activeIndex = _ref$activeIndex === void 0 ? 0 : _ref$activeIndex,
      onError = _ref.onError,
      _ref$inputpickerProps = _ref.inputpickerProps,
      inputpickerProps = _ref$inputpickerProps === void 0 ? {} : _ref$inputpickerProps,
      selected = _ref.selected,
      value = _ref.value,
      onChange = _ref.onChange,
      others = _objectWithoutPropertiesLoose(_ref, _excluded);

  var rootRef = (0, _react.useRef)(null);
  var min = list[activeIndex].min;
  var max = list[activeIndex].max;
  (0, _react.useImperativeHandle)(ref, function () {
    return rootRef.current;
  });
  (0, _react.useEffect)(function () {
    // 如果默认没有值, 则默认为当天
    if (!value && typeof activeIndex === 'number' && list[activeIndex]) {
      var item = list[activeIndex];
      var newValue = getValue('', item.type, 0);
      handleChange({
        target: rootRef.current
      }, newValue, [item], activeIndex);
    }
  }, []); // eslint-disable-line
  // 统一的修改方法

  function handleChange(e, val, selected, index, enableOnError) {
    var _inputpickerProps$pic, _inputpickerProps$pic2;

    val = (0, _validateDate["default"])(val, {
      type: list[activeIndex].type,
      min: min,
      max: max,
      split: (inputpickerProps === null || inputpickerProps === void 0 ? void 0 : (_inputpickerProps$pic = inputpickerProps.pickerProps) === null || _inputpickerProps$pic === void 0 ? void 0 : _inputpickerProps$pic.split) || '-',
      timeSplit: (inputpickerProps === null || inputpickerProps === void 0 ? void 0 : (_inputpickerProps$pic2 = inputpickerProps.pickerProps) === null || _inputpickerProps$pic2 === void 0 ? void 0 : _inputpickerProps$pic2.timeSplit) || ':',
      event: e,
      onError: enableOnError !== false ? onError : null
    });
    if (val === false) return;
    if (onChange) onChange(e, val, selected, index);
  } // 点击Tab


  function handleTab(e, tabName, selected, index) {
    if (!selected) return;
    var item = selected[0];
    if (!item.type) return; // 设置值为今天

    var newValue = getValue('', item.type, 0);
    min = list[index].min;
    max = list[index].max;
    handleChange(e, newValue, [item], index, false);
  } // 向前


  function handlePrev(e) {
    if (!value || !list[activeIndex].type) return;
    var newValue = getValue(value, list[activeIndex].type, -1);
    handleChange(e, newValue, [list[activeIndex]], activeIndex);
  } // 向后


  function handleNext(e) {
    if (!value || !list[activeIndex].type) return;
    var newValue = getValue(value, list[activeIndex].type, 1);
    handleChange(e, newValue, [list[activeIndex]], activeIndex);
  } // 选择日期


  function handleDate(e, value) {
    handleChange(e, value, [list[activeIndex]], activeIndex);
  }
  /**
   * 切换日期
   * @param {String} currentValue 单位列表, 单位列表需要从大到小排序后才能计算
   * @param {String} dateType 日期类型, [date|month|quarter|year]
   * @param {Number} go 前行后退, 0: 当前; -1: 后退; 1: 前进;
   */


  function getValue(currentValue, dateType, go) {
    var date = new Date();
    var newValue = currentValue || '';

    switch (dateType) {
      // 年
      case 'year':
        {
          if (go === 0) {
            newValue = '' + date.getFullYear();
            break;
          }

          date.year(currentValue);
          go === -1 ? date.prevYear() : date.nextYear();
          newValue = date.year();
          break;
        }
      // 季

      case 'quarter':
        {
          if (go === 0) {
            newValue = date.getFullYear() + '-' + date.quarter();
            break;
          }

          date.year(currentValue.split('-')[0]);
          date.quarter(currentValue.split('-')[1]);
          var quarter = go === -1 ? date.prevQuarter() : date.nextQuarter();
          newValue = date.getFullYear() + '-' + quarter;
          break;
        }
      // 月

      case 'month':
        {
          if (go === 0) {
            newValue = date.format('YYYY-MM');
            break;
          }

          date = currentValue.toDate();
          go === -1 ? date.prevMonth() : date.nextMonth();
          newValue = date.format('YYYY-MM');
          break;
        }
      // 日

      case 'date':
        {
          if (go === 0) {
            newValue = date.format('YYYY-MM-DD');
            break;
          }

          date = currentValue.toDate();
          go === -1 ? date.prevDate() : date.nextDate();
          newValue = date.format('YYYY-MM-DD');
          break;
        }

      default:
        {
          newValue = '';
        }
    }

    return newValue;
  }

  return /*#__PURE__*/_react["default"].createElement("div", _extends({}, others, {
    className: "datetype" + (others.className ? ' ' + others.className : '')
  }), listVisible && Array.isArray(list) && /*#__PURE__*/_react["default"].createElement(_Tabbar["default"], {
    rectJustify: false,
    onChange: handleTab,
    list: list,
    activeIndex: activeIndex,
    className: "tabbar-rect datetype-tabbar"
  }), /*#__PURE__*/_react["default"].createElement("div", {
    className: "datetype-content"
  }, /*#__PURE__*/_react["default"].createElement("i", {
    className: "datetype-prev icon shape-arrow-left sm",
    onClick: handlePrev
  }), /*#__PURE__*/_react["default"].createElement(_InputDate["default"], _extends({}, inputpickerProps, {
    value: value,
    selected: selected,
    type: list[activeIndex].type,
    className: "datetype-date" + (inputpickerProps.className ? ' ' + inputpickerProps.className : ''),
    onChange: handleDate
  }), value), /*#__PURE__*/_react["default"].createElement("i", {
    className: "datetype-next icon shape-arrow-right sm",
    onClick: handleNext
  })));
});
var _default = DateType;
exports["default"] = _default;
module.exports = exports.default;