"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));

var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

var _bem = require("@zarm-design/bem");

var _isEqual = _interopRequireDefault(require("lodash/isEqual"));

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

var _configProvider = require("../config-provider");

var _picker = _interopRequireDefault(require("../picker"));

var _utils = require("../picker-view/utils");

var _utils2 = require("../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 = (0, _objectWithoutProperties2.default)(props, ["placeholder", "className", "style", "disabled", "displayRender", "value", "onChange", "onCancel", "onConfirm", "defaultValue"]);

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

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

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

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

  React.useEffect(function () {
    if (props.value === undefined) return;
    if ((0, _isEqual.default)(props.value, innerValue)) return;
    setInnerValue((0, _utils.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((0, _utils.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.default, (0, _extends2.default)({}, 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() {}
};
var _default = Select;
exports.default = _default;