/**
 * amis-ui v6.12.0-beta.0
 * Copyright 2018-2025 fex
 */

import { __extends } from 'tslib';
import React__default from 'react';
import { findDOMNode } from 'react-dom';
import { BaseSelection } from './Selection.js';
import { PopOverContainer } from './PopOverContainer.js';
import GroupedSelection from './GroupedSelection.js';
import TreeSelection from './TreeSelection.js';
import ResultBox from './ResultBox.js';
import { filterTree, findTree, noop, themeable, localeable } from 'amis-core';
import { matchSorter } from 'match-sorter';
import { Icon } from './icons.js';
import SearchBox from './SearchBox.js';

var DropDownSelection = /** @class */ (function (_super) {
    __extends(DropDownSelection, _super);
    function DropDownSelection(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;
    }
    DropDownSelection.prototype.onSearch = function (text) {
        this.setState({ searchText: text });
    };
    DropDownSelection.prototype.filterOptions = function (options) {
        var _a = this.props, _b = _a.valueField, valueField = _b === void 0 ? 'value' : _b, labelField = _a.labelField;
        var text = this.state.searchText;
        if (!text) {
            return this.props.options;
        }
        return filterTree(options, function (option, key, level, paths) {
            return !!((Array.isArray(option.children) && option.children.length) ||
                !!matchSorter([option].concat(paths), text, {
                    keys: [labelField || 'label', valueField || 'value'],
                    threshold: matchSorter.rankings.CONTAINS
                }).length);
        }, 0, true);
    };
    // 选了值，还原options
    DropDownSelection.prototype.onPopClose = function (onClose) {
        this.setState({ searchText: '' });
        onClose();
    };
    DropDownSelection.prototype.render = function () {
        var _this = this;
        var _a = this.props, options = _a.options, onChange = _a.onChange, value = _a.value, cx = _a.classnames, disabled = _a.disabled, __ = _a.translate, searchable = _a.searchable, _b = _a.mode, mode = _b === void 0 ? 'list' : _b, _c = _a.valueField, valueField = _c === void 0 ? 'value' : _c, option2value = _a.option2value, loadingConfig = _a.loadingConfig, popOverContainer = _a.popOverContainer, testIdBuilder = _a.testIdBuilder, mobileUI = _a.mobileUI, ns = _a.classPrefix;
        return (React__default.createElement(PopOverContainer, { mobileUI: mobileUI, overlayWidthField: "width", popOverContainer: popOverContainer || (function () { return findDOMNode(_this); }), popOverRender: function (_a) {
                var onClose = _a.onClose;
                return (React__default.createElement("div", null,
                    searchable ? (React__default.createElement(SearchBox, { className: cx("".concat(ns, "DropDownSelection-searchbox")), mini: false, onSearch: _this.onSearch, testIdBuilder: testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild('searchbox') })) : null,
                    mode === 'list' ? (React__default.createElement(GroupedSelection, { multiple: false, onClick: function () { return _this.onPopClose(onClose); }, options: _this.filterOptions(_this.props.options), value: value, option2value: option2value, testIdBuilder: testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild('selection'), onChange: function (value) {
                            onChange(Array.isArray(value) ? value[0] : value);
                        } })) : (React__default.createElement(TreeSelection, { className: 'is-scrollable', multiple: false, options: _this.filterOptions(_this.props.options), value: value, loadingConfig: loadingConfig, testIdBuilder: testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild('selection'), onChange: function (value) {
                            _this.onPopClose(onClose);
                            onChange(value[valueField]);
                        } }))));
            } }, function (_a) {
            var onClick = _a.onClick, ref = _a.ref, isOpened = _a.isOpened;
            return (React__default.createElement("div", { className: cx('DropDownSelection') },
                React__default.createElement(ResultBox, { className: cx('DropDownSelection-input', isOpened ? 'is-active' : ''), ref: ref, allowInput: false, result: value
                        ? findTree(options, function (item) { return item[valueField] === value; })
                        : '', onResultChange: noop, onResultClick: onClick, placeholder: __('Condition.field_placeholder'), disabled: disabled, mobileUI: mobileUI, testIdBuilder: testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild('resultbox') }, !mobileUI ? (React__default.createElement("span", { className: cx('DropDownSelection-caret') },
                    React__default.createElement(Icon, { icon: "right-arrow-bold", className: "icon" }))) : null)));
        }));
    };
    return DropDownSelection;
}(BaseSelection));
var DropDownSelection$1 = themeable(localeable(DropDownSelection));

export { DropDownSelection$1 as default };
