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

import { __extends, __assign, __spreadArray, __read, __rest, __decorate, __metadata } from 'tslib';
import { ucFirst, autobind, themeable, localeable, uncontrollable } from 'amis-core';
import React__default from 'react';
import omit from 'lodash/omit';
import isInteger from 'lodash/isInteger';
import { Icon } from './icons.js';
import Input from './Input.js';
import isPlainObject from 'lodash/isPlainObject';
import TooltipWrapper from './TooltipWrapper.js';

var ResultBox = /** @class */ (function (_super) {
    __extends(ResultBox, _super);
    function ResultBox() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.state = {
            isFocused: false
        };
        _this.inputRef = React__default.createRef();
        return _this;
    }
    ResultBox.prototype.focus = function () {
        var _a;
        (_a = this.inputRef.current) === null || _a === void 0 ? void 0 : _a.focus();
    };
    ResultBox.prototype.blur = function () {
        var _a;
        (_a = this.inputRef.current) === null || _a === void 0 ? void 0 : _a.blur();
    };
    ResultBox.prototype.clearValue = function (e) {
        e.preventDefault();
        e.stopPropagation();
        this.props.onClear && this.props.onClear(e);
        this.props.onResultChange && this.props.onResultChange([]);
    };
    ResultBox.prototype.handleFocus = function (e) {
        var onFocus = this.props.onFocus;
        onFocus && onFocus(e);
        this.setState({
            isFocused: true
        });
    };
    ResultBox.prototype.handleBlur = function (e) {
        var onBlur = this.props.onBlur;
        onBlur && onBlur(e);
        this.setState({
            isFocused: false
        });
    };
    ResultBox.prototype.removeItem = function (e) {
        e.stopPropagation();
        e.preventDefault();
        var _a = this.props, result = _a.result, onResultChange = _a.onResultChange;
        var index = parseInt(e.currentTarget.getAttribute('data-index'), 10);
        var newResult = Array.isArray(result) ? result.concat() : [];
        newResult.splice(index, 1);
        onResultChange && onResultChange(newResult);
    };
    ResultBox.prototype.handleItemClick = function (e) {
        e.stopPropagation();
        e.preventDefault();
        var _a = this.props, result = _a.result, onItemClick = _a.onItemClick;
        var index = parseInt(e.currentTarget.getAttribute('data-index'), 10);
        var newResult = Array.isArray(result) ? result.concat() : [];
        onItemClick && onItemClick(newResult[index] || {});
    };
    ResultBox.prototype.handleChange = function (e) {
        var onChange = this.props.onChange;
        onChange === null || onChange === void 0 ? void 0 : onChange(e.currentTarget.value);
    };
    ResultBox.prototype.renderMultipeTags = function (tags) {
        var _this = this;
        var _a = this.props, maxTagCount = _a.maxTagCount, overflowTagPopover = _a.overflowTagPopover, itemRender = _a.itemRender, cx = _a.classnames, showInvalidMatch = _a.showInvalidMatch, popOverContainer = _a.popOverContainer, testIdBuilder = _a.testIdBuilder;
        if (maxTagCount != null &&
            isInteger(Math.floor(maxTagCount)) &&
            Math.floor(maxTagCount) >= 0 &&
            Math.floor(maxTagCount) < tags.length) {
            var maxVisibleCount_1 = Math.floor(maxTagCount);
            var tooltipProps_1 = __assign({ placement: 'top', trigger: 'hover', showArrow: false, offset: [0, -10], tooltipClassName: cx('ResultBox-overflow', overflowTagPopover === null || overflowTagPopover === void 0 ? void 0 : overflowTagPopover.tooltipClassName) }, omit(overflowTagPopover, ['children', 'content', 'tooltipClassName']));
            return __spreadArray(__spreadArray([], __read(tags.slice(0, maxVisibleCount_1)), false), [
                { label: "+ ".concat(tags.length - maxVisibleCount_1, " ...") }
            ], false).map(function (item, index) {
                var isShowInvalid = showInvalidMatch && (item === null || item === void 0 ? void 0 : item.__unmatched);
                var itemTIB = testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild(item.value || index);
                return index === maxVisibleCount_1 ? (React__default.createElement(TooltipWrapper, { key: tags.length, container: popOverContainer, tooltip: __assign(__assign({}, tooltipProps_1), { children: function () { return (React__default.createElement("div", { className: cx('ResultBox-overflow-wrapper') }, tags
                            .slice(maxVisibleCount_1, tags.length)
                            .map(function (item, index) {
                            var itemIndex = index + maxVisibleCount_1;
                            return (React__default.createElement("div", __assign({ className: cx('ResultBox-value', {
                                    'is-invalid': showInvalidMatch && (item === null || item === void 0 ? void 0 : item.__unmatched)
                                }), key: itemIndex, onClick: _this.handleItemClick }, itemTIB === null || itemTIB === void 0 ? void 0 : itemTIB.getTestId()),
                                React__default.createElement("span", __assign({ className: cx('ResultBox-valueLabel'), "data-index": index }, itemTIB === null || itemTIB === void 0 ? void 0 : itemTIB.getChild('click').getTestId()), itemRender(item)),
                                React__default.createElement("a", __assign({ "data-index": itemIndex, onClick: _this.removeItem }, itemTIB === null || itemTIB === void 0 ? void 0 : itemTIB.getChild('close').getTestId()),
                                    React__default.createElement(Icon, { icon: "close", className: "icon" }))));
                        }))); } }) },
                    React__default.createElement("div", { className: cx('ResultBox-value', {
                            'is-invalid': isShowInvalid
                        }), key: index },
                        React__default.createElement("span", { className: cx('ResultBox-valueLabel') }, item.label)))) : (React__default.createElement(TooltipWrapper, { container: popOverContainer, placement: 'top', tooltip: item['label'], trigger: 'hover', key: index },
                    React__default.createElement("div", __assign({ className: cx('ResultBox-value', {
                            'is-invalid': isShowInvalid
                        }), onClick: _this.handleItemClick }, itemTIB === null || itemTIB === void 0 ? void 0 : itemTIB.getTestId()),
                        React__default.createElement("span", __assign({ className: cx('ResultBox-valueLabel'), "data-index": index }, itemTIB === null || itemTIB === void 0 ? void 0 : itemTIB.getChild('click').getTestId()), itemRender(item)),
                        React__default.createElement("a", __assign({ "data-index": index, onClick: _this.removeItem }, itemTIB === null || itemTIB === void 0 ? void 0 : itemTIB.getChild('close').getTestId()),
                            React__default.createElement(Icon, { icon: "close", className: "icon" })))));
            });
        }
        return tags.map(function (item, index) {
            var itemTIB = testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild(index);
            return (React__default.createElement(TooltipWrapper, { container: popOverContainer, placement: 'top', tooltip: item['label'], trigger: 'hover', key: index },
                React__default.createElement("div", __assign({ className: cx('ResultBox-value', {
                        'is-invalid': showInvalidMatch && (item === null || item === void 0 ? void 0 : item.__unmatched)
                    }), onClick: _this.handleItemClick }, itemTIB === null || itemTIB === void 0 ? void 0 : itemTIB.getTestId()),
                    React__default.createElement("span", __assign({ className: cx('ResultBox-valueLabel'), "data-index": index }, itemTIB === null || itemTIB === void 0 ? void 0 : itemTIB.getChild('click').getTestId()), itemRender(item)),
                    React__default.createElement("a", __assign({ "data-index": index, onClick: _this.removeItem }, itemTIB === null || itemTIB === void 0 ? void 0 : itemTIB.getChild('close').getTestId()),
                        React__default.createElement(Icon, { icon: "close", className: "icon" })))));
        });
    };
    ResultBox.prototype.render = function () {
        var _a;
        /** 不需要透传给Input的属性要解构出来 */
        var _b = this.props, className = _b.className, cx = _b.classnames; _b.classPrefix; var clearable = _b.clearable, disabled = _b.disabled, hasError = _b.hasError, result = _b.result, value = _b.value, placeholder = _b.placeholder, children = _b.children, itemRender = _b.itemRender, allowInput = _b.allowInput, inputPlaceholder = _b.inputPlaceholder; _b.onResultChange; _b.onChange; var onResultClick = _b.onResultClick, __ = _b.translate; _b.locale; var onKeyPress = _b.onKeyPress, onFocus = _b.onFocus, onBlur = _b.onBlur, borderMode = _b.borderMode, mobileUI = _b.mobileUI, hasDropDownArrow = _b.hasDropDownArrow, actions = _b.actions; _b.onClear; _b.maxTagCount; _b.overflowTagPopover; var showArrow = _b.showArrow; _b.popOverContainer; var testIdBuilder = _b.testIdBuilder, rest = __rest(_b, ["className", "classnames", "classPrefix", "clearable", "disabled", "hasError", "result", "value", "placeholder", "children", "itemRender", "allowInput", "inputPlaceholder", "onResultChange", "onChange", "onResultClick", "translate", "locale", "onKeyPress", "onFocus", "onBlur", "borderMode", "mobileUI", "hasDropDownArrow", "actions", "onClear", "maxTagCount", "overflowTagPopover", "showArrow", "popOverContainer", "testIdBuilder"]);
        var isFocused = this.state.isFocused;
        return (React__default.createElement("div", __assign({ className: cx('ResultBox', className, (_a = {
                    'is-focused': isFocused,
                    'is-disabled': disabled,
                    'is-error': hasError,
                    'is-clickable': onResultClick,
                    'is-clearable': clearable,
                    'is-mobile': mobileUI,
                    'is-group': Array.isArray(result)
                },
                _a["ResultBox--border".concat(ucFirst(borderMode))] = borderMode,
                _a)), onClick: onResultClick, tabIndex: !allowInput && !disabled && onFocus ? 0 : -1, onKeyPress: allowInput ? undefined : onKeyPress, onFocus: allowInput ? undefined : onFocus, onBlur: allowInput ? undefined : onBlur }, testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getTestId()),
            React__default.createElement("div", { className: cx('ResultBox-value-wrap') },
                Array.isArray(result) && result.length ? (this.renderMultipeTags(result)) : result && !Array.isArray(result) ? (React__default.createElement("span", { className: cx('ResultBox-singleValue') }, isPlainObject(result) ? itemRender(result) : result)) : allowInput && !disabled ? null : (React__default.createElement("span", { className: cx('ResultBox-placeholder') }, __(placeholder || 'placeholder.noData'))),
                allowInput && !disabled ? (React__default.createElement(Input, __assign({}, rest, { className: cx('ResultBox-value-input'), onKeyPress: onKeyPress, ref: this.inputRef, value: value || '', onChange: this.handleChange, placeholder: __(
                    /** 数组模式下输入内容后将不再展示placeholder */
                    Array.isArray(result)
                        ? result.length > 0
                            ? inputPlaceholder
                            : placeholder
                        : result
                            ? ''
                            : placeholder), onFocus: this.handleFocus, onBlur: this.handleBlur, testIdBuilder: testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild('input') }))) : null,
                children),
            React__default.createElement("div", { className: cx('ResultBox-actions') },
                clearable &&
                    !disabled &&
                    (Array.isArray(result) ? result.length : result) ? (React__default.createElement("a", __assign({ onClick: this.clearValue, className: cx('ResultBox-clear', {
                        'ResultBox-clear-with-arrow': hasDropDownArrow
                    }) }, testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild('clear').getTestId()),
                    React__default.createElement("div", { className: cx('ResultBox-clear-wrap') },
                        React__default.createElement(Icon, { icon: "input-clear", className: "icon" })))) : null,
                actions,
                hasDropDownArrow && !mobileUI && (React__default.createElement("span", { className: cx('ResultBox-pc-arrow') },
                    React__default.createElement(Icon, { icon: "right-arrow-bold", className: "icon" }))),
                !allowInput && mobileUI && showArrow ? (React__default.createElement("span", { className: cx('ResultBox-arrow') },
                    React__default.createElement(Icon, { icon: "right-arrow-bold", className: "icon" }))) : null)));
    };
    ResultBox.defaultProps = {
        clearable: false,
        placeholder: 'placeholder.noData',
        inputPlaceholder: 'placeholder.enter',
        showArrow: true,
        itemRender: function (option) { return (React__default.createElement("span", null, "".concat(option.scopeLabel || '').concat(option.label))); }
    };
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", [Object]),
        __metadata("design:returntype", void 0)
    ], ResultBox.prototype, "clearValue", null);
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", [Object]),
        __metadata("design:returntype", void 0)
    ], ResultBox.prototype, "handleFocus", null);
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", [Object]),
        __metadata("design:returntype", void 0)
    ], ResultBox.prototype, "handleBlur", null);
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", [Object]),
        __metadata("design:returntype", void 0)
    ], ResultBox.prototype, "removeItem", null);
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", [Object]),
        __metadata("design:returntype", void 0)
    ], ResultBox.prototype, "handleItemClick", null);
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", [Object]),
        __metadata("design:returntype", void 0)
    ], ResultBox.prototype, "handleChange", null);
    return ResultBox;
}(React__default.Component));
var ResultBox$1 = themeable(localeable(uncontrollable(ResultBox, {
    value: 'onChange',
    result: 'onResultChange'
})));

export { ResultBox, ResultBox$1 as default };
