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

'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var tslib = require('tslib');
require('react');
var ReactDOM = require('react-dom');
var Selection = require('./Selection.js');
var PopOverContainer = require('./PopOverContainer.js');
var GroupedSelection = require('./GroupedSelection.js');
var TreeSelection = require('./TreeSelection.js');
var ResultBox = require('./ResultBox.js');
var amisCore = require('amis-core');
var matchSorter = require('match-sorter');
var icons = require('./icons.js');
var SearchBox = require('./SearchBox.js');

var __react_jsx__ = require('react');
var _J$X_ = (__react_jsx__["default"] || __react_jsx__).createElement;
(__react_jsx__["default"] || __react_jsx__).Fragment;
var DropDownSelection = /** @class */ (function (_super) {
    tslib.__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 amisCore.filterTree(options, function (option, key, level, paths) {
            return !!((Array.isArray(option.children) && option.children.length) ||
                !!matchSorter.matchSorter([option].concat(paths), text, {
                    keys: [labelField || 'label', valueField || 'value'],
                    threshold: matchSorter.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 (_J$X_(PopOverContainer.PopOverContainer, { mobileUI: mobileUI, overlayWidthField: "width", popOverContainer: popOverContainer || (function () { return ReactDOM.findDOMNode(_this); }), popOverRender: function (_a) {
                var onClose = _a.onClose;
                return (_J$X_("div", null,
                    searchable ? (_J$X_(SearchBox["default"], { 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' ? (_J$X_(GroupedSelection["default"], { 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);
                        } })) : (_J$X_(TreeSelection["default"], { 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 (_J$X_("div", { className: cx('DropDownSelection') },
                _J$X_(ResultBox["default"], { className: cx('DropDownSelection-input', isOpened ? 'is-active' : ''), ref: ref, allowInput: false, result: value
                        ? amisCore.findTree(options, function (item) { return item[valueField] === value; })
                        : '', onResultChange: amisCore.noop, onResultClick: onClick, placeholder: __('Condition.field_placeholder'), disabled: disabled, mobileUI: mobileUI, testIdBuilder: testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild('resultbox') }, !mobileUI ? (_J$X_("span", { className: cx('DropDownSelection-caret') },
                    _J$X_(icons.Icon, { icon: "right-arrow-bold", className: "icon" }))) : null)));
        }));
    };
    return DropDownSelection;
}(Selection.BaseSelection));
var DropDownSelection$1 = amisCore.themeable(amisCore.localeable(DropDownSelection));

exports["default"] = DropDownSelection$1;
