function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
var _excluded = ["min", "max", "list", "value", "pickerRender", "contentProps", "TabsProps", "DatePickerComboProps", "onError", "onChange"];
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 ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
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, useEffect, useImperativeHandle } from 'react';
import { validateMaxMin } from "./../utils";
import WeekCombo from "./WeekCombo";
import DateCombo from "./DateCombo";

// 内库使用-start
import TabBar from "./../../TabBar";
import LocaleUtil from "./../../../utils/LocaleUtil";
// 内库使用-end

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

// 日期类型选择控件: 年月日季
var Types = /*#__PURE__*/forwardRef(function (_ref, ref) {
  var min = _ref.min,
    max = _ref.max,
    _ref$list = _ref.list,
    list = _ref$list === void 0 ? [{
      type: 'date',
      name: LocaleUtil.locale('日', 'SeedsUI_unit_date')
    }, {
      type: 'week',
      id: 'week',
      name: LocaleUtil.locale('周', 'SeedsUI_unit_week')
    }, {
      type: 'month',
      name: LocaleUtil.locale('月', 'SeedsUI_unit_month')
    }, {
      type: 'quarter',
      name: LocaleUtil.locale('季', 'SeedsUI_unit_quarter')
    }, {
      type: 'year',
      name: LocaleUtil.locale('年', 'SeedsUI_unit_year')
    }] : _ref$list,
    value = _ref.value,
    pickerRender = _ref.pickerRender,
    _ref$contentProps = _ref.contentProps,
    contentProps = _ref$contentProps === void 0 ? {} : _ref$contentProps,
    _ref$TabsProps = _ref.TabsProps,
    TabsProps = _ref$TabsProps === void 0 ? {} : _ref$TabsProps,
    _ref$DatePickerComboP = _ref.DatePickerComboProps,
    DatePickerComboProps = _ref$DatePickerComboP === void 0 ? {} : _ref$DatePickerComboP,
    onError = _ref.onError,
    onChange = _ref.onChange,
    props = _objectWithoutProperties(_ref, _excluded);
  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;
      }
    };
  });
  useEffect(function () {
    // 如果默认没有值, 则默认为当天
    if (!value) {
      var newValue = list[0];
      if (!newValue.value) {
        newValue.value = new Date();
      }
      handleChange(newValue);
    }
  }, []); // eslint-disable-line

  // 统一的修改方法
  function handleChange(newValue) {
    var date = validateMaxMin(newValue.value, {
      type: newValue.type,
      min: min,
      max: max,
      onError: onError
    });
    if (date === false) {
      return;
    }
    newValue.value = date;
    if (onChange) onChange(newValue);
  }

  // 点击Tab
  function handleTabs(newValue) {
    if (!newValue.type) return;
    if (!newValue.value) newValue.value = (value === null || value === void 0 ? void 0 : value.value) || new Date();
    handleChange(newValue);
  }

  // 选择日期
  function handleDate(date) {
    value.value = date;
    handleChange(_objectSpread({}, value));
  }

  // 获取选择控件的node
  function getPickerNode() {
    var pickerNode = undefined;
    if (typeof pickerRender === 'function') {
      pickerNode = pickerRender(value, {
        onChange: handleDate
      });
    }
    if (pickerNode === undefined) {
      pickerNode = value.type === 'week' ? /*#__PURE__*/React.createElement(WeekCombo, _extends({}, DatePickerComboProps || {}, {
        value: value === null || value === void 0 ? void 0 : value.value,
        onChange: handleDate
      })) : /*#__PURE__*/React.createElement(DateCombo, _extends({}, DatePickerComboProps || {}, {
        type: value === null || value === void 0 ? void 0 : value.type,
        value: value === null || value === void 0 ? void 0 : value.value,
        onChange: handleDate
      }));
    }
    return pickerNode;
  }
  return /*#__PURE__*/React.createElement("div", _extends({}, props, {
    className: "datepicker-types".concat(props.className ? ' ' + props.className : ''),
    ref: rootRef
  }), Array.isArray(list) && /*#__PURE__*/React.createElement(TabBar.Slide, _extends({
    onChange: handleTabs,
    list: list,
    value: value
  }, TabsProps)), /*#__PURE__*/React.createElement("div", _extends({}, contentProps, {
    className: "datepicker-types-content".concat(contentProps.className ? ' ' + contentProps.className : '')
  }), (value === null || value === void 0 ? void 0 : value.value) instanceof Date ? getPickerNode() : null));
});
export default Types;