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

'use strict';

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

var tslib = require('tslib');
var amisCore = require('amis-core');
var React = require('react');
var omit = require('lodash/omit');
var icons = require('./icons.js');
var Input = require('./Input.js');
var isPlainObject = require('lodash/isPlainObject');
var TooltipWrapper = require('./TooltipWrapper.js');
var AutoFoldedList = require('./AutoFoldedList.js');

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

var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
var omit__default = /*#__PURE__*/_interopDefaultLegacy(omit);
var isPlainObject__default = /*#__PURE__*/_interopDefaultLegacy(isPlainObject);

var __react_jsx__ = require('react');
var _J$X_ = (__react_jsx__["default"] || __react_jsx__).createElement;
(__react_jsx__["default"] || __react_jsx__).Fragment;
var ResultBox = /** @class */ (function (_super) {
    tslib.__extends(ResultBox, _super);
    function ResultBox() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.state = {
            isFocused: false
        };
        _this.inputRef = React__default["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.renderMultipleTags = 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 (typeof maxTagCount === 'number' && maxTagCount > 0) {
            var tooltipProps = tslib.__assign({ offset: [0, -10], tooltipClassName: cx('ResultBox-overflow', overflowTagPopover === null || overflowTagPopover === void 0 ? void 0 : overflowTagPopover.tooltipClassName) }, omit__default["default"](overflowTagPopover, ['children', 'content', 'tooltipClassName']));
            return (_J$X_(AutoFoldedList["default"], { tooltipClassName: cx('Select-overflow-wrapper'), items: tags, popOverContainer: popOverContainer, tooltipOptions: tooltipProps, maxVisibleCount: maxTagCount, renderItem: function (item, index, folded) {
                    var itemTIB = testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild(item.value || index);
                    var isShowInvalid = showInvalidMatch && (item === null || item === void 0 ? void 0 : item.__unmatched);
                    var body = (_J$X_("div", tslib.__assign({ className: cx('ResultBox-value', {
                            'is-invalid': isShowInvalid
                        }), onClick: _this.handleItemClick }, itemTIB === null || itemTIB === void 0 ? void 0 : itemTIB.getTestId()),
                        _J$X_("span", tslib.__assign({ className: cx('ResultBox-valueLabel'), "data-index": index }, itemTIB === null || itemTIB === void 0 ? void 0 : itemTIB.getChild('click').getTestId()), itemRender(item)),
                        _J$X_("a", tslib.__assign({ "data-index": index, onClick: _this.removeItem }, itemTIB === null || itemTIB === void 0 ? void 0 : itemTIB.getChild('close').getTestId()),
                            _J$X_(icons.Icon, { icon: "close", className: "icon" }))));
                    return folded ? (body) : (_J$X_(TooltipWrapper["default"], { container: popOverContainer, placement: 'top', tooltip: item['label'], trigger: 'hover', key: index }, body));
                } }));
        }
        return tags.map(function (item, index) {
            var itemTIB = testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild(index);
            return (_J$X_(TooltipWrapper["default"], { container: popOverContainer, placement: 'top', tooltip: item['label'], trigger: 'hover', key: index },
                _J$X_("div", tslib.__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()),
                    _J$X_("span", tslib.__assign({ className: cx('ResultBox-valueLabel'), "data-index": index }, itemTIB === null || itemTIB === void 0 ? void 0 : itemTIB.getChild('click').getTestId()), itemRender(item)),
                    _J$X_("a", tslib.__assign({ "data-index": index, onClick: _this.removeItem }, itemTIB === null || itemTIB === void 0 ? void 0 : itemTIB.getChild('close').getTestId()),
                        _J$X_(icons.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 = tslib.__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 (_J$X_("div", tslib.__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(amisCore.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()),
            _J$X_("div", { className: cx('ResultBox-value-wrap') },
                Array.isArray(result) && result.length ? (this.renderMultipleTags(result)) : result && !Array.isArray(result) ? (_J$X_("span", { className: cx('ResultBox-singleValue') }, isPlainObject__default["default"](result) ? itemRender(result) : result)) : allowInput && !disabled ? null : (_J$X_("span", { className: cx('ResultBox-placeholder') }, __(placeholder || 'placeholder.noData'))),
                allowInput && !disabled ? (_J$X_(Input["default"], tslib.__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),
            _J$X_("div", { className: cx('ResultBox-actions') },
                clearable &&
                    !disabled &&
                    (Array.isArray(result) ? result.length : result) ? (_J$X_("a", tslib.__assign({ onClick: this.clearValue, className: cx('ResultBox-clear', {
                        'ResultBox-clear-with-arrow': hasDropDownArrow
                    }) }, testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild('clear').getTestId()),
                    _J$X_("div", { className: cx('ResultBox-clear-wrap') },
                        _J$X_(icons.Icon, { icon: "input-clear", className: "icon" })))) : null,
                actions,
                hasDropDownArrow && !mobileUI && (_J$X_("span", { className: cx('ResultBox-pc-arrow') },
                    _J$X_(icons.Icon, { icon: "right-arrow-bold", className: "icon" }))),
                !allowInput && mobileUI && showArrow ? (_J$X_("span", { className: cx('ResultBox-arrow') },
                    _J$X_(icons.Icon, { icon: "right-arrow-bold", className: "icon" }))) : null)));
    };
    ResultBox.defaultProps = {
        clearable: false,
        placeholder: 'placeholder.noData',
        inputPlaceholder: 'placeholder.enter',
        showArrow: true,
        itemRender: function (option) { return (_J$X_("span", null, "".concat(option.scopeLabel || '').concat(option.label))); }
    };
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", [Object]),
        tslib.__metadata("design:returntype", void 0)
    ], ResultBox.prototype, "clearValue", null);
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", [Object]),
        tslib.__metadata("design:returntype", void 0)
    ], ResultBox.prototype, "handleFocus", null);
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", [Object]),
        tslib.__metadata("design:returntype", void 0)
    ], ResultBox.prototype, "handleBlur", null);
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", [Object]),
        tslib.__metadata("design:returntype", void 0)
    ], ResultBox.prototype, "removeItem", null);
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", [Object]),
        tslib.__metadata("design:returntype", void 0)
    ], ResultBox.prototype, "handleItemClick", null);
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", [Object]),
        tslib.__metadata("design:returntype", void 0)
    ], ResultBox.prototype, "handleChange", null);
    return ResultBox;
}(React__default["default"].Component));
var ResultBox$1 = amisCore.themeable(amisCore.localeable(amisCore.uncontrollable(ResultBox, {
    value: 'onChange',
    result: 'onResultChange'
})));

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