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

'use strict';

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

var tslib = require('tslib');
var React = require('react');
var debounce = require('lodash/debounce');
var amisCore = require('amis-core');
var icons = require('./icons.js');
var InputBox = require('./InputBox.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
var debounce__default = /*#__PURE__*/_interopDefaultLegacy(debounce);

var __react_jsx__ = require('react');
var _J$X_ = (__react_jsx__["default"] || __react_jsx__).createElement;
(__react_jsx__["default"] || __react_jsx__).Fragment;
var TransferSearch = /** @class */ (function (_super) {
    tslib.__extends(TransferSearch, _super);
    function TransferSearch() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.state = {
            inputValue: ''
        };
        _this.lazySearch = debounce__default["default"](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 (_J$X_("div", { className: cx('Transfer-search') },
            _J$X_(InputBox["default"], { value: inputValue, onChange: this.handleSearch, clearable: false, onKeyDown: this.handleSearchKeyDown, placeholder: placeholder, mobileUI: mobileUI }, !!inputValue ? (_J$X_("a", { onClick: this.handleSeachCancel },
                _J$X_(icons.Icon, { icon: "close", className: "icon" }))) : (_J$X_(icons.Icon, { icon: "search", className: "icon" })))));
    };
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", [String]),
        tslib.__metadata("design:returntype", void 0)
    ], TransferSearch.prototype, "handleSearch", null);
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", [Object]),
        tslib.__metadata("design:returntype", void 0)
    ], TransferSearch.prototype, "handleSearchKeyDown", null);
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", []),
        tslib.__metadata("design:returntype", void 0)
    ], TransferSearch.prototype, "handleSeachCancel", null);
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", []),
        tslib.__metadata("design:returntype", void 0)
    ], TransferSearch.prototype, "clearInput", null);
    return TransferSearch;
}(React__default["default"].Component));
var TransferSearch$1 = amisCore.themeable(amisCore.localeable(TransferSearch));

exports.TransferSearch = TransferSearch;
exports["default"] = TransferSearch$1;
