'use strict';

exports.__esModule = true;

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

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _InputText = require('./../InputText');

var _InputText2 = _interopRequireDefault(_InputText);

var _PickerDate = require('./../PickerDate');

var _PickerDate2 = _interopRequireDefault(_PickerDate);

var _instance = require('../Context/instance.js');

var _instance2 = _interopRequireDefault(_instance);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

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


var InputDate = (0, _react.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 = (0, _react.useRef)(null);
  (0, _react.useImperativeHandle)(ref, function () {
    return refEl.current;
  });

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


  var context = (0, _react.useContext)(_instance2.default) || {};
  var locale = context.locale || function (key) {
    return key || '';
  };
  // 日期纠正
  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 _react2.default.createElement(
    _react.Fragment,
    null,
    _react2.default.createElement(_InputText2.default, _extends({ ref: refEl }, others, { type: 'text', readOnly: true, onClick: onClickInput })),
    _react2.default.createElement(_PickerDate2.default, _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
    }))
  );
});

exports.default = InputDate;
module.exports = exports['default'];