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

import { __extends, __assign, __decorate, __metadata } from 'tslib';
import { autobind, themeable } from 'amis-core';
import React__default from 'react';
import { Icon } from '../icons.js';
import ConditionGroup from './Group.js';
import ConditionItem from './Item.js';
import FormulaPicker from '../formula/Picker.js';
import Button from '../Button.js';
import TooltipWrapper from '../TooltipWrapper.js';

var CBGroupOrItem = /** @class */ (function (_super) {
    __extends(CBGroupOrItem, _super);
    function CBGroupOrItem() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.state = {
            hover: false
        };
        return _this;
    }
    CBGroupOrItem.prototype.handleItemChange = function (value) {
        this.props.onChange(value, this.props.index);
    };
    CBGroupOrItem.prototype.handleItemRemove = function () {
        var _a, _b;
        (_b = (_a = this.props).onRemove) === null || _b === void 0 ? void 0 : _b.call(_a, this.props.index);
    };
    CBGroupOrItem.prototype.handlerHoverIn = function (e) {
        if (this.props.mobileUI) {
            return;
        }
        e.stopPropagation();
        this.setState({
            hover: true
        });
    };
    CBGroupOrItem.prototype.handlerHoverOut = function (e) {
        this.setState({
            hover: false
        });
    };
    CBGroupOrItem.prototype.handleIfChange = function (condition) {
        var value = __assign(__assign({}, this.props.value), { if: condition });
        this.props.onChange(value, this.props.index);
    };
    CBGroupOrItem.prototype.render = function () {
        var _a = this.props, builderMode = _a.builderMode, cx = _a.classnames, fieldClassName = _a.fieldClassName, value = _a.value, config = _a.config, fields = _a.fields, funcs = _a.funcs, draggable = _a.draggable, data = _a.data, disabled = _a.disabled, searchable = _a.searchable, onDragStart = _a.onDragStart, formula = _a.formula, popOverContainer = _a.popOverContainer, selectMode = _a.selectMode, renderEtrValue = _a.renderEtrValue, isCollapsed = _a.isCollapsed, depth = _a.depth, isAddBtnVisibleOn = _a.isAddBtnVisibleOn, isAddGroupBtnVisibleOn = _a.isAddGroupBtnVisibleOn, showIf = _a.showIf, formulaForIf = _a.formulaForIf, testIdBuilder = _a.testIdBuilder, mobileUI = _a.mobileUI;
        return (React__default.createElement("div", { className: cx("CBGroupOrItem".concat(builderMode === 'simple' ? '-simple' : ''), { 'is-mobile': mobileUI }), "data-id": value === null || value === void 0 ? void 0 : value.id },
            React__default.createElement("div", { className: cx('CBGroupOrItem-body') }, (value === null || value === void 0 ? void 0 : value.conjunction) ? (React__default.createElement("div", { className: cx('CBGroupOrItem-body-group', {
                    'is-hover': this.state.hover || mobileUI
                }), onMouseOver: this.handlerHoverIn, onMouseOut: this.handlerHoverOut },
                draggable && !disabled ? (React__default.createElement("a", { draggable: true, onDragStart: onDragStart, className: cx('CBGroupOrItem-dragbar') },
                    React__default.createElement(Icon, { icon: "drag-bar", className: "icon" }))) : null,
                React__default.createElement(ConditionGroup, { isCollapsed: isCollapsed, draggable: draggable, disabled: disabled, searchable: searchable, selectMode: selectMode, onDragStart: onDragStart, config: config, fields: fields, formula: formula, value: value, onChange: this.handleItemChange, fieldClassName: fieldClassName, funcs: funcs, removeable: true, onRemove: this.handleItemRemove, data: data, renderEtrValue: renderEtrValue, depth: depth + 1, isAddBtnVisibleOn: isAddBtnVisibleOn, isAddGroupBtnVisibleOn: isAddGroupBtnVisibleOn, showIf: showIf, formulaForIf: formulaForIf, testIdBuilder: testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild("sub-".concat(depth)) }))) : (React__default.createElement("div", { className: cx('CBGroupOrItem-body-item') },
                draggable && !disabled ? (React__default.createElement("a", { draggable: true, onDragStart: onDragStart, className: cx('CBGroupOrItem-dragbar') },
                    React__default.createElement(Icon, { icon: "drag-bar", className: "icon" }))) : null,
                React__default.createElement(ConditionItem, { disabled: disabled, searchable: searchable, config: config, fields: fields, value: value, onChange: this.handleItemChange, fieldClassName: fieldClassName, funcs: funcs, data: data, formula: formula, popOverContainer: popOverContainer, renderEtrValue: renderEtrValue, selectMode: selectMode, testIdBuilder: testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild("cItem") }),
                showIf ? (React__default.createElement(FormulaPicker, __assign({}, formulaForIf, { evalMode: true, mixedMode: false, header: "\u8BBE\u7F6E\u6761\u4EF6", value: (value === null || value === void 0 ? void 0 : value.if) || '', onChange: this.handleIfChange }), function (_a) {
                    var onClick = _a.onClick;
                    return (React__default.createElement(TooltipWrapper, { tooltip: '配置启动条件，当前规则只有在此条件成立时才会生效', tooltipTheme: "dark", container: popOverContainer },
                        React__default.createElement("a", { className: cx('CBIf', (value === null || value === void 0 ? void 0 : value.if) ? 'is-active' : ''), onClick: onClick },
                            React__default.createElement(Icon, { icon: "if", className: "icon" }))));
                })) : null,
                React__default.createElement(Button, { className: cx('CBDelete'), onClick: this.handleItemRemove, disabled: disabled, level: "link", testIdBuilder: testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild("delete") },
                    React__default.createElement(Icon, { icon: "remove", className: "icon" })))))));
    };
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", [Object]),
        __metadata("design:returntype", void 0)
    ], CBGroupOrItem.prototype, "handleItemChange", null);
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", []),
        __metadata("design:returntype", void 0)
    ], CBGroupOrItem.prototype, "handleItemRemove", null);
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", [Object]),
        __metadata("design:returntype", void 0)
    ], CBGroupOrItem.prototype, "handlerHoverIn", null);
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", [Object]),
        __metadata("design:returntype", void 0)
    ], CBGroupOrItem.prototype, "handlerHoverOut", null);
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", [String]),
        __metadata("design:returntype", void 0)
    ], CBGroupOrItem.prototype, "handleIfChange", null);
    return CBGroupOrItem;
}(React__default.Component));
var GroupOrItem = themeable(CBGroupOrItem);

export { CBGroupOrItem, GroupOrItem as default };
