var _excluded = ["visible", "value", "type", "min", "max", "hourStep", "minuteStep", "disabledStart", "disabledEnd", "allowClear", "onChange", "rangeId", "ranges", "portal", "selectorProps", "datePickerModalProps"];
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 getTitleByType from "./../utils/getTitleByType";
import updateRangeValue from "./../RangeMain/updateRangeValue";
import getCustomRangeId from "./getCustomRangeId";
import getDefaultRanges from "./../RangeMain/getDefaultRanges";
import getDefaultRangeId from "./getDefaultRangeId";
import Buttons from "./Buttons";
import Dates from "./Dates";

// 日期快捷选择
function RangeSelector(_ref, ref) {
  var visible = _ref.visible,
    value = _ref.value,
    _ref$type = _ref.type,
    type = _ref$type === void 0 ? 'date' : _ref$type,
    min = _ref.min,
    max = _ref.max,
    hourStep = _ref.hourStep,
    minuteStep = _ref.minuteStep,
    disabledStart = _ref.disabledStart,
    disabledEnd = _ref.disabledEnd,
    allowClear = _ref.allowClear,
    onChange = _ref.onChange,
    rangeId = _ref.rangeId,
    ranges = _ref.ranges,
    portal = _ref.portal,
    selectorProps = _ref.selectorProps,
    datePickerModalProps = _ref.datePickerModalProps,
    props = _objectWithoutProperties(_ref, _excluded);
  if (ranges === undefined) {
    // eslint-disable-next-line
    ranges = getDefaultRanges();
  }

  // 自定义项id
  var customRangeId = getCustomRangeId(ranges);

  // 当前选中项id
  var defaultRangeId = getDefaultRangeId(value, ranges, type);
  var currentRangeId = rangeId !== null && rangeId !== void 0 ? rangeId : defaultRangeId;
  var mainRef = useRef(null);
  useImperativeHandle(ref, function () {
    return {
      mainDOM: mainRef.current,
      getMainDOM: function getMainDOM() {
        return mainRef.current;
      },
      getTitle: function getTitle() {
        return getTitleByType(type);
      }
    };
  });

  // unify onChange
  function handleChange(newValue, _ref2) {
    var rangeId = _ref2.rangeId;
    onChange && onChange(updateRangeValue(newValue, type), {
      rangeId: rangeId || null,
      ranges: ranges
    });
  }
  return /*#__PURE__*/React.createElement("div", _extends({}, props, {
    className: "datepicker-rangeselector-selector".concat(props.className ? ' ' + props.className : ''),
    ref: mainRef
  }), /*#__PURE__*/React.createElement(Buttons, {
    value: value,
    onChange: handleChange,
    rangeId: currentRangeId,
    ranges: ranges,
    selectorProps: selectorProps,
    allowClear: allowClear
  }), customRangeId && currentRangeId === customRangeId && /*#__PURE__*/React.createElement(Dates, {
    datePickerModalProps: datePickerModalProps,
    portal: portal,
    type: type,
    allowClear: allowClear,
    value: value,
    min: min,
    max: max,
    hourStep: hourStep,
    minuteStep: minuteStep,
    disabledStart: disabledStart,
    disabledEnd: disabledEnd,
    onChange: handleChange
  }));
}
export default /*#__PURE__*/forwardRef(RangeSelector);