"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

var _typeof = require("@babel/runtime/helpers/typeof");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));

var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

var _newArrowCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/newArrowCheck"));

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

var _Popover = _interopRequireDefault(require("../../components/Popover"));

var _Input = _interopRequireDefault(require("../../components/Input"));

var _Calendar = _interopRequireDefault(require("../../components/Calendar"));

var _Notice = _interopRequireDefault(require("../../components/Notice"));

var _Time = _interopRequireDefault(require("../../components/TimePicker/Time"));

var _SvgIcon = _interopRequireDefault(require("../../components/SvgIcon"));

var _usePopoverConfig = _interopRequireDefault(require("../../hooks/usePopoverConfig"));

var _DatePicker = require("./DatePicker");

var _Month = require("./Month");

var _style = require("./style");

var _usePicker3 = _interopRequireDefault(require("./usePicker"));

var _Footer = _interopRequireDefault(require("./Footer"));

var _excluded = ["prefix", "onActiveChange", "type", "readonly", "tip", "error", "footerTip"];

var _this = void 0;

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 ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

var RangePickerWithoutMemo = /*#__PURE__*/(0, _react.forwardRef)(function (_ref, ref) {
  var _this2 = this,
      _timeProps$mode;

  (0, _newArrowCheck2.default)(this, _this);
  var prefix = _ref.prefix,
      onActiveChange = _ref.onActiveChange,
      _ref$type = _ref.type,
      type = _ref$type === void 0 ? 'date' : _ref$type,
      readonly = _ref.readonly,
      tip = _ref.tip,
      error = _ref.error,
      footerTip = _ref.footerTip,
      pickProps = (0, _objectWithoutProperties2.default)(_ref, _excluded);
  var isMonth = type === 'month';

  var _usePicker = (0, _usePicker3.default)(_objectSpread({}, pickProps), isMonth ? _Month.displayToFormatAndTimeMode : _DatePicker.displayToFormatAndTimeMode, type),
      _usePicker2 = (0, _slicedToArray2.default)(_usePicker, 8),
      inputProps = _usePicker2[0],
      popoverProps = _usePicker2[2],
      popupProps = _usePicker2[3],
      calendarProps = _usePicker2[4],
      timeProps = _usePicker2[5],
      footerProps = _usePicker2[6],
      _usePicker2$ = _usePicker2[7],
      pickerError = _usePicker2$.error,
      active = _usePicker2$.active;

  var inputRef = /*#__PURE__*/(0, _react.createRef)();
  (0, _react.useImperativeHandle)(ref, function () {
    var _this3 = this;

    (0, _newArrowCheck2.default)(this, _this2);
    return {
      focus: function focus() {
        var _inputRef$current;

        (0, _newArrowCheck2.default)(this, _this3);
        return inputRef === null || inputRef === void 0 ? void 0 : (_inputRef$current = inputRef.current) === null || _inputRef$current === void 0 ? void 0 : _inputRef$current.focus();
      }.bind(this)
    };
  }.bind(this), [inputRef]);
  (0, _react.useEffect)(function () {
    (0, _newArrowCheck2.default)(this, _this2);
    onActiveChange(active);
  }.bind(this), [active, onActiveChange]);
  var popoverConfigProps = (0, _usePopoverConfig.default)();
  var hasTime = !!((_timeProps$mode = timeProps.mode) !== null && _timeProps$mode !== void 0 && _timeProps$mode.length);
  var CalendarComp = isMonth ? _Calendar.default.Month : _Calendar.default;
  return readonly ? /*#__PURE__*/_react.default.createElement("span", {
    className: _style.readonlyInputCls
  }, inputProps.value) : /*#__PURE__*/_react.default.createElement(_style.RangeInputWrap, {
    isMonth: isMonth,
    hasTime: hasTime,
    hasPrefix: prefix
  }, /*#__PURE__*/_react.default.createElement(_Popover.default, (0, _extends2.default)({
    popup: /*#__PURE__*/_react.default.createElement(_style.SPopup, popupProps, /*#__PURE__*/_react.default.createElement(CalendarComp, (0, _extends2.default)({}, calendarProps, {
      sidebar: isMonth ? null : hasTime ? /*#__PURE__*/_react.default.createElement(_Time.default, timeProps) : null
    })), pickerError && /*#__PURE__*/_react.default.createElement(_Notice.default, {
      styleType: "error",
      closable: false
    }, pickerError), error && /*#__PURE__*/_react.default.createElement(_Notice.default, {
      styleType: "error",
      closable: false
    }, error), tip && /*#__PURE__*/_react.default.createElement(_Notice.default, {
      closable: false
    }, tip), /*#__PURE__*/_react.default.createElement(_Footer.default, (0, _extends2.default)({}, footerProps, {
      tip: footerTip
    })))
  }, popoverConfigProps, popoverProps), /*#__PURE__*/_react.default.createElement(_Input.default, (0, _extends2.default)({}, inputProps, {
    customStyle: {
      border: 'none',
      boxShadow: 'none',
      background: 'none'
    },
    ref: inputRef,
    block: true,
    prefix: prefix ? /*#__PURE__*/_react.default.createElement(_SvgIcon.default, {
      type: "calendar"
    }) : null
  }))));
}.bind(void 0));

var _default = /*#__PURE__*/(0, _react.memo)(RangePickerWithoutMemo);

exports.default = _default;