var _excluded = ["onClick", "onChange", "list", "selected", "pickerProps"];

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

import React, { forwardRef, useRef, useImperativeHandle, Fragment, useState } from 'react';
import InputText from './../InputText';
import Picker from './../Picker';
var InputPicker = forwardRef(function (_ref, ref) {
  var onClick = _ref.onClick,
      onChange = _ref.onChange,
      _ref$list = _ref.list,
      list = _ref$list === void 0 ? [] : _ref$list,
      selected = _ref.selected,
      _ref$pickerProps = _ref.pickerProps,
      pickerProps = _ref$pickerProps === void 0 ? {} : _ref$pickerProps,
      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 onClickInput() {
    if (onClick) onClick.apply(void 0, 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 (inputTextRef.current) e.target = inputTextRef.current; // 确定按钮回调

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


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


  function onClickCancel(e) {
    if (inputTextRef.current) e.target = inputTextRef.current;

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

    setShow(false);
  } // 过滤非法数据


  list = list.filter(function (item) {
    if (!item || !item.id && !item.name) return false;
    return true;
  });
  return /*#__PURE__*/React.createElement(Fragment, null, /*#__PURE__*/React.createElement(InputText, _extends({
    ref: inputTextRef
  }, others, {
    readOnly: true,
    onClick: onClickInput
  })), /*#__PURE__*/React.createElement(Picker, _extends({}, pickerProps, {
    maskAttribute: _extends({}, pickerProps.maskAttribute, {
      onClick: onClickMask
    }),
    submitAttribute: _extends({}, pickerProps.submitAttribute, {
      onClick: onClickSubmit
    }),
    cancelAttribute: _extends({}, pickerProps.cancelAttribute, {
      onClick: onClickCancel
    }),
    list: list,
    selected: selected,
    value: others.value,
    show: pickerProps.show === undefined ? show : pickerProps.show
  })));
});
export default InputPicker;