"use strict";

exports.__esModule = true;
exports["default"] = void 0;

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

var _InputText = _interopRequireDefault(require("./../InputText"));

var _PickerDate = _interopRequireDefault(require("./../PickerDate"));

var _validateDate = _interopRequireDefault(require("./validateDate"));

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

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

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

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

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

  var _useState = (0, _react.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 = (0, _validateDate["default"])(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["default"].createElement(_react.Fragment, null, children && /*#__PURE__*/_react["default"].createElement("div", _extends({
    ref: inputTextRef
  }, others, {
    onClick: handleClickInput
  }), children), !children && /*#__PURE__*/_react["default"].createElement(_InputText["default"], _extends({
    ref: inputTextRef
  }, others, {
    type: "text",
    readOnly: true,
    onClick: handleClickInput
  })), /*#__PURE__*/_react["default"].createElement(_PickerDate["default"], _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
  })));
});
var _default = InputDate;
exports["default"] = _default;
module.exports = exports.default;