"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
require("core-js/modules/es.array.iterator.js");
require("core-js/modules/web.dom-collections.iterator.js");
var _react = _interopRequireWildcard(require("react"));
var _icons = require("@wii/icons");
var _classnames = _interopRequireDefault(require("classnames"));
var _select = _interopRequireDefault(require("../select"));
var _input = _interopRequireDefault(require("../input"));
var _configProvider = _interopRequireDefault(require("../config-provider"));
var _wauFieldLabel = _interopRequireDefault(require("../wau-field-label"));
var _jsxRuntime = require("react/jsx-runtime");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (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; }
/**
 * 如果有 label 就优先使用 label
 *
 * @param valueMap
 * @param v
 */
const getValueOrLabel = (valueMap, v) => {
  if (typeof v !== 'object') {
    return valueMap[v] || v;
  }
  return valueMap[v === null || v === void 0 ? void 0 : v.value] || v.label;
};
const LightSelect = (props, ref) => {
  const {
    label,
    prefixCls: customizePrefixCls,
    onChange,
    value,
    mode,
    size,
    showSearch,
    disabled,
    style,
    className,
    bordered,
    options,
    onSearch,
    allowClear,
    labelInValue,
    ...restProps
  } = props;
  const {
    placeholder = label
  } = props;
  const {
    getPrefixCls
  } = (0, _react.useContext)(_configProvider.default.ConfigContext);
  const prefixCls = getPrefixCls('pro-field-select-light-select');
  const [open, setOpen] = (0, _react.useState)(false);
  const [keyword, setKeyword] = (0, _react.useState)('');
  const valueMap = (0, _react.useMemo)(() => {
    const values = {};
    options === null || options === void 0 ? void 0 : options.forEach(({
      label: aLabel,
      value: aValue
    }) => {
      values[aValue] = aLabel || aValue;
    });
    return values;
  }, [options]);
  const filterValue = Array.isArray(value) ? value.map(v => getValueOrLabel(valueMap, v)) : getValueOrLabel(valueMap, value);
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
    className: (0, _classnames.default)(prefixCls, {
      [`${prefixCls}-searchable`]: showSearch
    }, className),
    style: style,
    onClick: () => {
      if (!disabled) {
        setOpen(true);
      }
    },
    children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_select.default, {
      ...restProps,
      allowClear: allowClear,
      value: value,
      mode: mode,
      labelInValue: labelInValue,
      size: size,
      disabled: disabled,
      onChange: (v, option) => {
        if (onChange) {
          onChange(v, option);
        }
        if (mode !== 'multiple') {
          setTimeout(() => {
            setOpen(false);
          }, 0);
        }
      },
      bordered: bordered,
      showSearch: showSearch,
      onSearch: onSearch,
      style: style,
      dropdownRender: menuNode => {
        return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
          ref: ref,
          children: [showSearch && /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
            style: {
              margin: '4px 8px'
            },
            children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_input.default, {
              value: keyword,
              allowClear: allowClear,
              onChange: e => {
                setKeyword(e.target.value.toLowerCase());
                onSearch === null || onSearch === void 0 ? void 0 : onSearch(e.target.value);
              },
              onKeyDown: e => {
                // 避免按下删除键把选项也删除了
                e.stopPropagation();
              },
              style: {
                width: '100%'
              },
              prefix: /*#__PURE__*/(0, _jsxRuntime.jsx)(_icons.SearchOutlined, {})
            })
          }), menuNode]
        });
      },
      open: open,
      onDropdownVisibleChange: setOpen,
      prefixCls: customizePrefixCls,
      options: keyword ? options === null || options === void 0 ? void 0 : options.filter(o => {
        var _o$value;
        return String(o.label).toLowerCase().includes(keyword) || ((_o$value = o.value) === null || _o$value === void 0 ? void 0 : _o$value.toString().toLowerCase().includes(keyword));
      }) : options
    }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_wauFieldLabel.default, {
      ellipsis: true,
      size: size,
      label: label,
      placeholder: placeholder,
      disabled: disabled,
      expanded: open,
      bordered: bordered,
      allowClear: allowClear,
      value: filterValue || (value === null || value === void 0 ? void 0 : value.label) || value,
      onClear: () => {
        onChange === null || onChange === void 0 ? void 0 : onChange(undefined, undefined);
      }
    })]
  });
};
var _default = /*#__PURE__*/_react.default.forwardRef(LightSelect);
exports.default = _default;