var _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; };

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }

// require PrototypeDate.js和PrototypeString.js
import React, { forwardRef, useRef, useImperativeHandle, useState, useContext, Fragment } from 'react';
import InputText from './../InputText';
import PickerDate from './../PickerDate';
import Context from '../Context/instance.js';

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 === undefined ? 'date' : _ref$type,
      selected = _ref.selected,
      _ref$pickerProps = _ref.pickerProps,
      pickerProps = _ref$pickerProps === undefined ? {} : _ref$pickerProps,
      others = _objectWithoutProperties(_ref, ['min', 'max', 'onClick', 'onChange', 'onError', 'type', 'selected', 'pickerProps']);

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

  var _useState = useState(false),
      show = _useState[0],
      setShow = _useState[1];
  // context


  var context = useContext(Context) || {};
  var locale = context.locale || {};
  // 日期纠正
  function correctDate(val) {
    var split = pickerProps.split || '-';
    var timeSplit = pickerProps.timeSplit || ':';
    var text = val;
    var selectDate = text.toDate(split, timeSplit);
    var value = '';
    var e = {};
    if (refEl.current) {
      e.target = refEl.current;
      value = refEl.current.value;
    }
    if (min && (min.isDateTime(split, timeSplit) || min.isDate(split) || min.isMonth(split) || min.isTime(timeSplit))) {
      if (type === 'date' && selectDate.compareDate(min.toDate(split, timeSplit)) === -1) {
        if (onError) {
          onError(e, { errMsg: (locale['hint_cannot_be_less_than'] || '不能小于') + min, select: text, min: min, value: value });
          return false;
        }
        text = min;
      } else if (type === 'month' && selectDate.compareMonth(min.toDate(split, timeSplit)) === -1) {
        if (onError) {
          onError(e, { errMsg: (locale['hint_cannot_be_less_than'] || '不能小于') + min, select: text, min: min, value: value });
          return false;
        }
        text = min;
      } else if (type === 'time' && selectDate.compareTime(min.toDate(split, timeSplit)) === -1) {
        if (onError) {
          onError(e, { errMsg: (locale['hint_cannot_be_less_than'] || '不能小于') + min, select: text, min: min, value: value });
          return false;
        }
        text = min;
      } else if (type === 'datetime' && selectDate.compareDateTime(min.toDate(split, timeSplit)) === -1) {
        if (onError) {
          onError(e, { errMsg: (locale['hint_cannot_be_less_than'] || '不能小于') + min, select: text, min: min, value: value });
          return false;
        }
        text = min;
      }
    }
    if (max && (max.isDateTime(split, timeSplit) || max.isDate(split) || max.isMonth(split) || max.isTime(timeSplit))) {
      if (type === 'date' && selectDate.compareDate(max.toDate(split, timeSplit)) === 1) {
        if (onError) {
          onError(e, { errMsg: (locale['hint_cannot_be_greater_than'] || '不能大于') + max, select: text, max: max, value: value });
          return false;
        }
        text = max;
      } else if (type === 'month' && selectDate.compareMonth(max.toDate(split, timeSplit)) === 1) {
        if (onError) {
          onError(e, { errMsg: (locale['hint_cannot_be_greater_than'] || '不能大于') + max, select: text, max: max, value: value });
          return false;
        }
        text = max;
      } else if (type === 'time' && selectDate.compareTime(max.toDate(split, timeSplit)) === 1) {
        if (onError) {
          onError(e, { errMsg: (locale['hint_cannot_be_greater_than'] || '不能大于') + max, select: text, max: max, value: value });
          return false;
        }
        text = max;
      } else if (type === 'datetime' && selectDate.compareDateTime(max.toDate(split, timeSplit)) === 1) {
        if (onError) {
          onError(e, { errMsg: (locale['hint_cannot_be_greater_than'] || '不能大于') + max, select: text, max: max, value: value });
          return false;
        }
        text = max;
      }
    }
    return text;
  }
  // 点击文本框
  function onClickInput() {
    if (onClick) onClick.apply(undefined, arguments);
    if (others.readOnly) return;
    setShow(true);
  }
  // 点击遮罩
  function onClickMask(e) {
    if (pickerProps && pickerProps.maskAttribute && pickerProps.maskAttribute.onClick) {
      pickerProps.maskAttribute.onClick(e);
      return;
    }
    setShow(false);
  }
  // 点击确定按钮
  function onClickSubmit(e, value, options) {
    if (refEl.current) e.target = refEl.current;
    var val = correctDate(value);
    if (val === false) return;
    // 确定按钮回调
    if (pickerProps && pickerProps.submitAttribute && pickerProps.submitAttribute.onClick) {
      pickerProps.submitAttribute.onClick(e, val, options);
      return;
    }
    // 赋值
    if (onChange) {
      onChange(e, val, options);
    }
    // 隐藏框
    setShow(false);
  }
  // 点击取消按钮
  function onClickCancel(e) {
    if (pickerProps && pickerProps.cancelAttribute && pickerProps.cancelAttribute.onClick) {
      pickerProps.cancelAttribute.onClick(e);
      return;
    }
    setShow(false);
  }

  return React.createElement(
    Fragment,
    null,
    React.createElement(InputText, _extends({ ref: refEl }, others, { type: 'text', readOnly: true, onClick: onClickInput })),
    React.createElement(PickerDate, _extends({}, pickerProps, {
      maskAttribute: _extends({}, pickerProps.maskAttribute, {
        onClick: onClickMask
      }),
      submitAttribute: _extends({}, pickerProps.submitAttribute, {
        onClick: onClickSubmit
      }),
      cancelAttribute: _extends({}, pickerProps.cancelAttribute, {
        onClick: onClickCancel
      }),
      selected: selected,
      type: type,
      value: others.value,
      show: pickerProps.show === undefined ? show : pickerProps.show
    }))
  );
});

export default InputDate;