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

import { __extends, __decorate, __metadata } from 'tslib';
import React__default from 'react';
import debounce from 'lodash/debounce';
import { autobind, themeable, localeable } from 'amis-core';
import { Icon } from './icons.js';
import InputBox from './InputBox.js';

var TransferSearch = /** @class */ (function (_super) {
    __extends(TransferSearch, _super);
    function TransferSearch() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.state = {
            inputValue: ''
        };
        _this.lazySearch = debounce(function () {
            var inputValue = _this.state.inputValue;
            // 防止由于防抖导致空值问题
            if (!inputValue) {
                return;
            }
            var onSearch = _this.props.onSearch;
            onSearch(inputValue);
        }, 250, { trailing: true, leading: false });
        return _this;
    }
    TransferSearch.prototype.componentWillUnmount = function () {
        this.lazySearch.cancel();
    };
    TransferSearch.prototype.handleSearch = function (inputValue) {
        var _this = this;
        // text 有值的时候，走搜索否则直接走 handleSeachCancel ，等同于右侧的 clear 按钮
        this.setState({ inputValue: inputValue }, function () {
            if (inputValue) {
                // 如果有取消搜索，先取消掉。
                _this.cancelSearch && _this.cancelSearch();
                _this.lazySearch();
            }
            else {
                _this.handleSeachCancel();
            }
        });
    };
    TransferSearch.prototype.handleSearchKeyDown = function (e) {
        if (e.key === 'Enter') {
            e.preventDefault();
        }
    };
    TransferSearch.prototype.handleSeachCancel = function () {
        var _a, _b;
        (_b = (_a = this.props).onCancelSearch) === null || _b === void 0 ? void 0 : _b.call(_a);
        this.setState({
            inputValue: ''
        });
    };
    TransferSearch.prototype.clearInput = function () {
        this.setState({
            inputValue: ''
        });
    };
    TransferSearch.prototype.render = function () {
        var _a = this.props, cx = _a.classnames; _a.translate; var placeholder = _a.placeholder, mobileUI = _a.mobileUI;
        var inputValue = this.state.inputValue;
        return (React__default.createElement("div", { className: cx('Transfer-search') },
            React__default.createElement(InputBox, { value: inputValue, onChange: this.handleSearch, clearable: false, onKeyDown: this.handleSearchKeyDown, placeholder: placeholder, mobileUI: mobileUI }, !!inputValue ? (React__default.createElement("a", { onClick: this.handleSeachCancel },
                React__default.createElement(Icon, { icon: "close", className: "icon" }))) : (React__default.createElement(Icon, { icon: "search", className: "icon" })))));
    };
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", [String]),
        __metadata("design:returntype", void 0)
    ], TransferSearch.prototype, "handleSearch", null);
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", [Object]),
        __metadata("design:returntype", void 0)
    ], TransferSearch.prototype, "handleSearchKeyDown", null);
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", []),
        __metadata("design:returntype", void 0)
    ], TransferSearch.prototype, "handleSeachCancel", null);
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", []),
        __metadata("design:returntype", void 0)
    ], TransferSearch.prototype, "clearInput", null);
    return TransferSearch;
}(React__default.Component));
var TransferSearch$1 = themeable(localeable(TransferSearch));

export { TransferSearch, TransferSearch$1 as default };
