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

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

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

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


  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;
    // 确定按钮回调
    if (pickerProps && pickerProps.submitAttribute && pickerProps.submitAttribute.onClick) {
      pickerProps.submitAttribute.onClick(e, value, options);
      return;
    }
    // 赋值
    if (onChange) {
      onChange(e, value, options);
    }
    // 隐藏框
    setShow(false);
  }
  // 点击取消按钮
  function onClickCancel(e) {
    if (refEl.current) e.target = refEl.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 React.createElement(
    Fragment,
    null,
    React.createElement(InputText, _extends({ ref: refEl }, others, { readOnly: true, onClick: onClickInput })),
    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;