var _excluded = ["value", "options", "label", "helperText", "error", "errorText", "required", "placeholder", "showSearch", "allowClear", "size", "className", "style", "onSearch", "filterOption", "onChange"];
function _extends() { _extends = Object.assign ? Object.assign.bind() : 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 _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
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; }
/**
 * PisellSingleSelect - EditView (编辑态视图)
 * 正常可编辑状态下的渲染
 */

import React, { forwardRef, useMemo, useCallback, useState, useRef } from 'react';
import { Select, Input } from 'antd';
import { CheckOutlined, SearchOutlined } from '@ant-design/icons';
import classNames from 'classnames';
import { useDebouncedSearch } from '@pisell/utils';
var EditView = /*#__PURE__*/forwardRef(function (props, ref) {
  var value = props.value,
    options = props.options,
    label = props.label,
    helperText = props.helperText,
    error = props.error,
    errorText = props.errorText,
    required = props.required,
    _props$placeholder = props.placeholder,
    placeholder = _props$placeholder === void 0 ? '请选择' : _props$placeholder,
    _props$showSearch = props.showSearch,
    showSearch = _props$showSearch === void 0 ? false : _props$showSearch,
    _props$allowClear = props.allowClear,
    allowClear = _props$allowClear === void 0 ? false : _props$allowClear,
    _props$size = props.size,
    size = _props$size === void 0 ? 'middle' : _props$size,
    className = props.className,
    style = props.style,
    onSearch = props.onSearch,
    filterOption = props.filterOption,
    onChange = props.onChange,
    restProps = _objectWithoutProperties(props, _excluded);

  // 转换为 Ant Design 需要的格式
  var antdOptions = useMemo(function () {
    return options.map(function (opt) {
      return {
        label: opt.label,
        value: opt.value,
        disabled: opt.disabled
      };
    });
  }, [options]);

  // 处理值变化
  var handleChange = function handleChange(newValue) {
    var selectedOption = options.find(function (opt) {
      return opt.value === newValue;
    });
    onChange === null || onChange === void 0 || onChange(newValue !== null && newValue !== void 0 ? newValue : null, selectedOption !== null && selectedOption !== void 0 ? selectedOption : null);
  };

  // 搜索关键词状态
  var _useState = useState(''),
    _useState2 = _slicedToArray(_useState, 2),
    searchKeyword = _useState2[0],
    setSearchKeyword = _useState2[1];

  // 处理搜索输入（防抖）
  var _useDebouncedSearch = useDebouncedSearch('', 300, function (text) {
      onSearch === null || onSearch === void 0 || onSearch(text);
    }),
    _useDebouncedSearch2 = _slicedToArray(_useDebouncedSearch, 2),
    setSearchInput = _useDebouncedSearch2[1];
  var handleSearchInput = function handleSearchInput(e) {
    var text = e.target.value;
    setSearchKeyword(text);
    setSearchInput(text);
  };

  // 过滤选项
  var filteredAntdOptions = useMemo(function () {
    if (!showSearch || !searchKeyword) return antdOptions;
    if (filterOption) {
      // 使用自定义过滤
      return antdOptions.filter(function (opt) {
        var selectOption = options.find(function (o) {
          return o.value === opt.value;
        });
        return selectOption ? filterOption(searchKeyword, selectOption) : false;
      });
    }

    // 默认按 label 过滤
    return antdOptions.filter(function (opt) {
      return opt.label.toLowerCase().includes(searchKeyword.toLowerCase());
    });
  }, [antdOptions, searchKeyword, showSearch, filterOption, options]);
  var wrapperRef = useRef(null);
  // 自定义选中图标
  var menuItemSelectedIcon = /*#__PURE__*/React.createElement(CheckOutlined, {
    style: {
      color: '#7F56D9',
      fontSize: 16
    }
  });

  // 自定义下拉框渲染（在顶部添加搜索框）
  var dropdownRender = useCallback(function (menu) {
    if (!showSearch) return menu;
    return /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement("div", {
      className: "pisell-select-search-wrapper"
    }, /*#__PURE__*/React.createElement(Input, {
      placeholder: "Search",
      prefix: /*#__PURE__*/React.createElement(SearchOutlined, {
        style: {
          color: '#98A2B3',
          fontSize: 16
        }
      }),
      value: searchKeyword,
      onChange: handleSearchInput,
      className: "pisell-select-search-input",
      bordered: false,
      autoFocus: true
    })), menu);
  }, [showSearch, searchKeyword, handleSearchInput]);
  return /*#__PURE__*/React.createElement("div", {
    ref: wrapperRef,
    className: classNames('pisell-single-select-wrapper', className),
    style: style
  }, label && /*#__PURE__*/React.createElement("label", {
    className: classNames('pisell-single-select-label', {
      'pisell-single-select-label-required': required
    })
  }, label), /*#__PURE__*/React.createElement(Select, _extends({}, restProps, {
    ref: ref,
    value: value !== null && value !== void 0 ? value : undefined,
    options: filteredAntdOptions,
    placeholder: placeholder,
    showSearch: false,
    allowClear: allowClear,
    menuItemSelectedIcon: menuItemSelectedIcon,
    dropdownRender: dropdownRender,
    onChange: handleChange,
    size: size,
    popupClassName: "pisell-single-select-dropdown",
    getPopupContainer: function getPopupContainer() {
      var _wrapperRef$current;
      return (_wrapperRef$current = wrapperRef.current) !== null && _wrapperRef$current !== void 0 ? _wrapperRef$current : document.body;
    },
    className: classNames('pisell-single-select', {
      'pisell-single-select-error': error
    })
  })), helperText && !error && /*#__PURE__*/React.createElement("div", {
    className: "pisell-single-select-helper-text"
  }, helperText), error && errorText && /*#__PURE__*/React.createElement("div", {
    className: "pisell-single-select-error-text"
  }, errorText));
});
EditView.displayName = 'PisellSingleSelectEditView';
export default EditView;