import _extends from "@babel/runtime/helpers/extends";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
import { createBEM } from '@zarm-design/bem';
import clamp from 'lodash/clamp';
import isEqual from 'lodash/isEqual';
import * as React from 'react';
import { unstable_batchedUpdates as batchedUpdates } from 'react-dom';
import { ConfigContext } from '../config-provider';
import DatePicker from '../date-picker';
import { COLUMN_TYPE } from '../date-picker-view/interface';
import { dateToNumberArray, generateDatePickerColumns, useRenderLabel } from '../date-picker-view/utils';
import { resolved } from '../picker-view/utils';
import { useSafeState } from '../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 ? [COLUMN_TYPE.YEAR, COLUMN_TYPE.MONTH, 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 = _objectWithoutProperties(props, ["className", "style", "placeholder", "disabled", "hasArrow", "value", "defaultValue", "min", "max", "columnType", "filter", "renderLabel", "displayRender", "onCancel", "onConfirm"]);

  var _useSafeState = useSafeState(value || defaultValue),
      _useSafeState2 = _slicedToArray(_useSafeState, 2),
      innerValue = _useSafeState2[0],
      setInnerValue = _useSafeState2[1];

  var _useSafeState3 = useSafeState(false),
      _useSafeState4 = _slicedToArray(_useSafeState3, 2),
      visible = _useSafeState4[0],
      setVisible = _useSafeState4[1];

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

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

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

  var handleConfirm = function handleConfirm(changedValue, changedItems) {
    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(clamp(date.getTime(), min.getTime(), max.getTime()));
  }, [innerValue, min, max, columnType]);
  var pickerValue = React.useMemo(function () {
    return dateToNumberArray(currentValue, columnType);
  }, [currentValue, columnType]);

  var _resolved = resolved({
    dataSource: 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, _extends({}, rest, {
    visible: visible,
    columnType: columnType,
    value: innerValue,
    min: min,
    max: max,
    filter: filter,
    renderLabel: renderLabel,
    onConfirm: handleConfirm,
    onCancel: handleCancel
  })));
};

export default DateSelect;