"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

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

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

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

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

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

var _bem = require("@zarm-design/bem");

var _clamp = _interopRequireDefault(require("lodash/clamp"));

var _isEqual = _interopRequireDefault(require("lodash/isEqual"));

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

var _reactDom = require("react-dom");

var _configProvider = require("../config-provider");

var _datePicker = _interopRequireDefault(require("../date-picker"));

var _interface = require("../date-picker-view/interface");

var _utils = require("../date-picker-view/utils");

var _utils2 = require("../picker-view/utils");

var _hooks = require("../utils/hooks");

var currentYear = new Date().getFullYear();

var DateSelect = function DateSelect(props) {
  var className = props.className,
      style = props.style,
      placeholder = props.placeholder,
      _props$disabled = props.disabled,
      disabled = _props$disabled === void 0 ? false : _props$disabled,
      _props$hasArrow = props.hasArrow,
      hasArrow = _props$hasArrow === void 0 ? true : _props$hasArrow,
      value = props.value,
      defaultValue = props.defaultValue,
      _props$min = props.min,
      min = _props$min === void 0 ? new Date(new Date().setFullYear(currentYear - 10)) : _props$min,
      _props$max = props.max,
      max = _props$max === void 0 ? new Date(new Date().setFullYear(currentYear + 10)) : _props$max,
      _props$columnType = props.columnType,
      columnType = _props$columnType === void 0 ? [_interface.COLUMN_TYPE.YEAR, _interface.COLUMN_TYPE.MONTH, _interface.COLUMN_TYPE.DAY] : _props$columnType,
      filter = props.filter,
      renderLabel = props.renderLabel,
      _props$displayRender = props.displayRender,
      displayRender = _props$displayRender === void 0 ? function (items) {
    return items === null || items === void 0 ? void 0 : items.map(function (item) {
      return item && item.label;
    });
  } : _props$displayRender,
      onCancel = props.onCancel,
      onConfirm = props.onConfirm,
      rest = (0, _objectWithoutProperties2.default)(props, ["className", "style", "placeholder", "disabled", "hasArrow", "value", "defaultValue", "min", "max", "columnType", "filter", "renderLabel", "displayRender", "onCancel", "onConfirm"]);

  var _useSafeState = (0, _hooks.useSafeState)(value || defaultValue),
      _useSafeState2 = (0, _slicedToArray2.default)(_useSafeState, 2),
      innerValue = _useSafeState2[0],
      setInnerValue = _useSafeState2[1];

  var _useSafeState3 = (0, _hooks.useSafeState)(false),
      _useSafeState4 = (0, _slicedToArray2.default)(_useSafeState3, 2),
      visible = _useSafeState4[0],
      setVisible = _useSafeState4[1];

  var _React$useContext = React.useContext(_configProvider.ConfigContext),
      globalLocal = _React$useContext.locale,
      prefixCls = _React$useContext.prefixCls;

  var defaultRenderLabel = (0, _utils.useRenderLabel)(renderLabel);
  var locale = globalLocal.DateSelect;
  var bem = (0, _bem.createBEM)('date-select', {
    prefixCls: prefixCls
  });
  React.useEffect(function () {
    if ((0, _isEqual.default)(value, innerValue)) return;
    setInnerValue(value);
  }, [value]);

  var handleClick = function handleClick() {
    if (disabled) return;
    setVisible(true);
  };

  var handleConfirm = function handleConfirm(changedValue, changedItems) {
    (0, _reactDom.unstable_batchedUpdates)(function () {
      setInnerValue(changedValue);
      setVisible(false);
    });
    onConfirm === null || onConfirm === void 0 ? void 0 : onConfirm(changedValue, changedItems);
  };

  var handleCancel = function handleCancel() {
    setVisible(false);
    onCancel === null || onCancel === void 0 ? void 0 : onCancel();
  };

  var now = React.useMemo(function () {
    return new Date();
  }, []);
  var currentValue = React.useMemo(function () {
    var date = innerValue || now;
    return new Date((0, _clamp.default)(date.getTime(), min.getTime(), max.getTime()));
  }, [innerValue, min, max, columnType]);
  var pickerValue = React.useMemo(function () {
    return (0, _utils.dateToNumberArray)(currentValue, columnType);
  }, [currentValue, columnType]);

  var _resolved = (0, _utils2.resolved)({
    dataSource: (0, _utils.generateDatePickerColumns)(currentValue, min, max, columnType, defaultRenderLabel, filter),
    value: pickerValue
  }),
      items = _resolved.items;

  var arrowRender = /*#__PURE__*/React.createElement("div", {
    className: bem('arrow')
  });
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
    className: bem([{
      placeholder: !innerValue,
      disabled: disabled,
      visible: visible
    }, className]),
    style: style,
    onClick: handleClick
  }, /*#__PURE__*/React.createElement("div", {
    className: bem('input')
  }, /*#__PURE__*/React.createElement("div", {
    className: bem('value')
  }, innerValue && (displayRender === null || displayRender === void 0 ? void 0 : displayRender(items)) || placeholder || locale.placeholder)), hasArrow ? arrowRender : null), /*#__PURE__*/React.createElement(_datePicker.default, (0, _extends2.default)({}, rest, {
    visible: visible,
    columnType: columnType,
    value: innerValue,
    min: min,
    max: max,
    filter: filter,
    renderLabel: renderLabel,
    onConfirm: handleConfirm,
    onCancel: handleCancel
  })));
};

var _default = DateSelect;
exports.default = _default;