var _excluded = ["min", "max", "onClick", "onChange", "onError", "type", "selected", "pickerProps", "children"];

function _extends() { _extends = Object.assign || 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 _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; }

// require PrototypeDate.js和PrototypeString.js
import React, { forwardRef, useRef, useImperativeHandle, useState, Fragment } from 'react';
import InputText from './../InputText';
import PickerDate from './../PickerDate';
import validateDate from './validateDate';
var InputDate = forwardRef(function (_ref, ref) {
  var min = _ref.min,
      max = _ref.max,
      onClick = _ref.onClick,
      onChange = _ref.onChange,
      onError = _ref.onError,
      _ref$type = _ref.type,
      type = _ref$type === void 0 ? 'date' : _ref$type,
      selected = _ref.selected,
      _ref$pickerProps = _ref.pickerProps,
      pickerProps = _ref$pickerProps === void 0 ? {} : _ref$pickerProps,
      children = _ref.children,
      others = _objectWithoutPropertiesLoose(_ref, _excluded);

  var inputTextRef = useRef(null);
  useImperativeHandle(ref, function () {
    return inputTextRef.current;
  });

  var _useState = useState(false),
      show = _useState[0],
      setShow = _useState[1]; // 点击文本框


  function handleClickInput() {
    if (onClick) onClick.apply(void 0, arguments);
    if (others.readOnly) return;
    setShow(true);
  } // 点击遮罩


  function handleClickMask(e) {
    if (pickerProps && pickerProps.maskAttribute && pickerProps.maskAttribute.onClick) {
      pickerProps.maskAttribute.onClick(e);
      return;
    }

    setShow(false);
  } // 点击确定按钮


  function handleClickSubmit(e, value, options) {
    if (inputTextRef.current) e.target = inputTextRef.current;
    var val = validateDate(value, {
      type: type,
      min: min,
      max: max,
      split: pickerProps.split || '-',
      timeSplit: pickerProps.timeSplit || ':',
      e: e,
      onError: onError
    });
    if (val === false) return; // 确定按钮回调

    if (pickerProps && pickerProps.submitAttribute && pickerProps.submitAttribute.onClick) {
      // 允许确定错误时中断隐藏
      if (pickerProps.submitAttribute.onClick(e, val, options) === false) return;
    } else if (onChange) {
      onChange(e, val, options);
    } // 隐藏框


    setShow(false);
  } // 点击取消按钮


  function handleClickCancel(e) {
    if (pickerProps && pickerProps.cancelAttribute && pickerProps.cancelAttribute.onClick) {
      pickerProps.cancelAttribute.onClick(e);
      return;
    }

    setShow(false);
  }

  return /*#__PURE__*/React.createElement(Fragment, null, children && /*#__PURE__*/React.createElement("div", _extends({
    ref: inputTextRef
  }, others, {
    onClick: handleClickInput
  }), children), !children && /*#__PURE__*/React.createElement(InputText, _extends({
    ref: inputTextRef
  }, others, {
    type: "text",
    readOnly: true,
    onClick: handleClickInput
  })), /*#__PURE__*/React.createElement(PickerDate, _extends({}, pickerProps, {
    maskAttribute: _extends({}, pickerProps.maskAttribute, {
      onClick: handleClickMask
    }),
    submitAttribute: _extends({}, pickerProps.submitAttribute, {
      onClick: handleClickSubmit
    }),
    cancelAttribute: _extends({}, pickerProps.cancelAttribute, {
      onClick: handleClickCancel
    }),
    selected: selected,
    type: type,
    value: others.value,
    show: pickerProps.show === undefined ? show : pickerProps.show
  })));
});
export default InputDate;