/**
 * amis-ui v6.14.0-beta.1
 * Copyright 2018-2025 fex
 */

import { __extends } from 'tslib';
import React__default from 'react';
import { findDOMNode } from 'react-dom';
import { themeable, localeable } from 'amis-core';
import { matchSorter } from 'match-sorter';
import { PopOverContainer } from './PopOverContainer.js';
import SearchBox from './SearchBox.js';
import GroupedSelection from './GroupedSelection.js';
import InputBox from './InputBox.js';
import { Icon } from './icons.js';

var option2value = function (item) { return item.value; };
var InputBoxWithSuggestion = /** @class */ (function (_super) {
    __extends(InputBoxWithSuggestion, _super);
    function InputBoxWithSuggestion(props) {
        var _this = _super.call(this, props) || this;
        _this.state = {
            searchText: ''
        };
        _this.onSearch = _this.onSearch.bind(_this);
        _this.filterOptions = _this.filterOptions.bind(_this);
        return _this;
    }
    InputBoxWithSuggestion.prototype.onSearch = function (text) {
        var txt = text.toLowerCase();
        this.setState({ searchText: txt });
    };
    InputBoxWithSuggestion.prototype.filterOptions = function (options) {
        var _this = this;
        return this.props.value
            ? matchSorter(options, this.props.value, {
                keys: ['label', 'value'],
                threshold: matchSorter.rankings.CONTAINS
            }).filter(function (item) { return item.value !== _this.props.value; })
            : options;
    };
    // 选了值，还原options
    InputBoxWithSuggestion.prototype.onPopClose = function (e, onClose) {
        this.setState({ searchText: '' });
        onClose();
    };
    InputBoxWithSuggestion.prototype.render = function () {
        var _this = this;
        var _a = this.props, placeholder = _a.placeholder, onChange = _a.onChange, value = _a.value, cx = _a.classnames, disabled = _a.disabled; _a.translate; var searchable = _a.searchable, popOverContainer = _a.popOverContainer, clearable = _a.clearable, hasError = _a.hasError, mobileUI = _a.mobileUI, className = _a.className;
        var options = this.filterOptions(Array.isArray(this.props.options) ? this.props.options : []);
        return (React__default.createElement(PopOverContainer, { show: !!options.length, popOverContainer: popOverContainer || (function () { return findDOMNode(_this); }), popOverRender: function (_a) {
                var onClose = _a.onClose;
                return (React__default.createElement(React__default.Fragment, null,
                    searchable ? (React__default.createElement(SearchBox, { mini: false, onSearch: _this.onSearch })) : null,
                    React__default.createElement(GroupedSelection, { multiple: false, onClick: function (e) { return _this.onPopClose(e, onClose); }, options: options, value: [value], option2value: option2value, onChange: function (value) {
                            onChange === null || onChange === void 0 ? void 0 : onChange(value);
                        } })));
            } }, function (_a) {
            var _b;
            var onClick = _a.onClick, ref = _a.ref, isOpened = _a.isOpened;
            return (React__default.createElement(InputBox, { className: cx('InputBox--sug', className, isOpened ? 'is-active' : ''), ref: ref, placeholder: placeholder, disabled: disabled, value: value, blurValue: (_b = (Array.isArray(_this.props.options)
                    ? _this.props.options
                    : []).find(function (o) { return o.value === value; })) === null || _b === void 0 ? void 0 : _b.label, onChange: onChange, clearable: clearable, onClick: onClick, hasError: hasError, mobileUI: mobileUI }, options.length ? (React__default.createElement("span", { className: cx('InputBox-caret') },
                React__default.createElement(Icon, { icon: "right-arrow-bold", className: "icon" }))) : null));
        }));
    };
    return InputBoxWithSuggestion;
}(React__default.Component));
var InputBoxWithSuggestion$1 = themeable(localeable(InputBoxWithSuggestion));

export { InputBoxWithSuggestion, InputBoxWithSuggestion$1 as default };
