import _extends from "@babel/runtime/helpers/extends";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
import { createBEM } from '@zarm-design/bem';
import isEqual from 'lodash/isEqual';
import * as React from 'react';
import { ConfigContext } from '../config-provider';
import Picker from '../picker';
import { resolved } from '../picker-view/utils';
import { toArray } from '../utils';
var Select = /*#__PURE__*/React.forwardRef(function (props, ref) {
  var _locale$Select;

  var placeholder = props.placeholder,
      className = props.className,
      style = props.style,
      disabled = props.disabled,
      displayRender = props.displayRender,
      value = props.value,
      onChange = props.onChange,
      onCancel = props.onCancel,
      onConfirm = props.onConfirm,
      defaultValue = props.defaultValue,
      rest = _objectWithoutProperties(props, ["placeholder", "className", "style", "disabled", "displayRender", "value", "onChange", "onCancel", "onConfirm", "defaultValue"]);

  var _React$useContext = React.useContext(ConfigContext),
      prefixCls = _React$useContext.prefixCls,
      locale = _React$useContext.locale;

  var bem = createBEM('select', {
    prefixCls: prefixCls
  });

  var _React$useState = React.useState(toArray(value || defaultValue)),
      _React$useState2 = _slicedToArray(_React$useState, 2),
      innerValue = _React$useState2[0],
      setInnerValue = _React$useState2[1];

  var _React$useState3 = React.useState(false),
      _React$useState4 = _slicedToArray(_React$useState3, 2),
      visible = _React$useState4[0],
      setVisible = _React$useState4[1];

  React.useEffect(function () {
    if (props.value === undefined) return;
    if (isEqual(props.value, innerValue)) return;
    setInnerValue(resolved(props).value);
  }, [props.value]);
  var handleClick = React.useCallback(function () {
    if (disabled) return;
    setVisible(true);
  }, [disabled]);

  var handleonConfirm = function handleonConfirm(changedValue, dataSource) {
    setVisible(false);
    onConfirm === null || onConfirm === void 0 ? void 0 : onConfirm(changedValue, dataSource);
  };

  var handleOnCancel = function handleOnCancel() {
    setVisible(false);
    onCancel === null || onCancel === void 0 ? void 0 : onCancel();
  };

  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
    ref: ref,
    className: bem([{
      placeholder: !innerValue.length,
      disabled: disabled,
      visible: visible
    }, className]),
    style: style,
    onClick: handleClick
  }, /*#__PURE__*/React.createElement("div", {
    className: bem('input')
  }, /*#__PURE__*/React.createElement("div", {
    className: bem('value')
  }, innerValue.length && displayRender(resolved(props).items || []) || placeholder || (locale === null || locale === void 0 ? void 0 : (_locale$Select = locale.Select) === null || _locale$Select === void 0 ? void 0 : _locale$Select.placeholder))), /*#__PURE__*/React.createElement("div", {
    className: bem('arrow')
  })), /*#__PURE__*/React.createElement(Picker, _extends({}, rest, {
    visible: visible,
    value: innerValue,
    onConfirm: handleonConfirm,
    onChange: onChange,
    onCancel: handleOnCancel
  })));
});
Select.displayName = 'Select';
Select.defaultProps = {
  dataSource: [],
  cols: Infinity,
  maskClosable: true,
  displayRender: function displayRender(items) {
    return items === null || items === void 0 ? void 0 : items.map(function (item) {
      return item && item.label;
    });
  },
  onClick: function onClick() {}
};
export default Select;