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

import { __extends, __assign, __decorate, __metadata } from 'tslib';
import React__default from 'react';
import { themeable, localeable, guid, autobind } from 'amis-core';
import Button from '../Button.js';
import GroupOrItem from './GroupOrItem.js';
import EnhancedSelect from '../Select.js';
import '../icons.js';
import SvgDownArrowBold from '../../icons/down-arrow-bold.svg.js';

var ConditionGroup = /** @class */ (function (_super) {
    __extends(ConditionGroup, _super);
    function ConditionGroup(props) {
        var _this = _super.call(this, props) || this;
        _this.state = {
            isCollapsed: false
        };
        return _this;
    }
    ConditionGroup.prototype.componentDidUpdate = function (prevProps) {
        // 上层折叠的时候，内层也折叠，主要是为了处理，子节点中，第一项也是Group的折叠场景
        if (prevProps.isCollapsed !== this.props.isCollapsed) {
            this.setState({
                isCollapsed: this.props.isCollapsed || false
            });
        }
    };
    ConditionGroup.prototype.getValue = function () {
        return __assign({ id: guid(), conjunction: 'and' }, this.props.value);
    };
    ConditionGroup.prototype.handleNotClick = function () {
        var onChange = this.props.onChange;
        var value = this.getValue();
        value.not = !value.not;
        onChange(value);
    };
    ConditionGroup.prototype.handleConjunctionChange = function (val) {
        var onChange = this.props.onChange;
        var value = this.getValue();
        value.conjunction = val.value;
        onChange(value);
    };
    ConditionGroup.prototype.handleAdd = function () {
        var onChange = this.props.onChange;
        var value = this.getValue();
        value.children = Array.isArray(value.children)
            ? value.children.concat()
            : [];
        value.children.push({
            id: guid()
        });
        onChange(value);
    };
    ConditionGroup.prototype.handleAddGroup = function () {
        var onChange = this.props.onChange;
        var value = this.getValue();
        value.children = Array.isArray(value.children)
            ? value.children.concat()
            : [];
        value.children.push({
            id: guid(),
            conjunction: 'and',
            children: [
                {
                    id: guid()
                }
            ]
        });
        onChange(value);
    };
    ConditionGroup.prototype.handleItemChange = function (item, index) {
        var onChange = this.props.onChange;
        var value = this.getValue();
        value.children = Array.isArray(value.children)
            ? value.children.concat()
            : [];
        value.children.splice(index, 1, item);
        onChange(value);
    };
    ConditionGroup.prototype.handleItemRemove = function (index) {
        var onChange = this.props.onChange;
        var value = this.getValue();
        value.children = Array.isArray(value.children)
            ? value.children.concat()
            : [];
        value.children.splice(index, 1);
        onChange(value);
    };
    ConditionGroup.prototype.toggleCollapse = function () {
        this.setState(function (state) {
            return {
                isCollapsed: !state.isCollapsed
            };
        });
    };
    ConditionGroup.prototype.render = function () {
        var _this = this;
        var _a, _b, _c;
        var _d = this.props, builderMode = _d.builderMode, cx = _d.classnames, fieldClassName = _d.fieldClassName, value = _d.value, data = _d.data, fields = _d.fields, funcs = _d.funcs, config = _d.config, removeable = _d.removeable, onRemove = _d.onRemove, onDragStart = _d.onDragStart, showNot = _d.showNot, _e = _d.showANDOR, showANDOR = _e === void 0 ? false : _e, disabled = _d.disabled, searchable = _d.searchable, __ = _d.translate, formula = _d.formula, popOverContainer = _d.popOverContainer, selectMode = _d.selectMode, renderEtrValue = _d.renderEtrValue, draggable = _d.draggable, depth = _d.depth, isAddBtnVisibleOn = _d.isAddBtnVisibleOn, isAddGroupBtnVisibleOn = _d.isAddGroupBtnVisibleOn, showIf = _d.showIf, formulaForIf = _d.formulaForIf, testIdBuilder = _d.testIdBuilder;
        var isCollapsed = this.state.isCollapsed;
        var body = Array.isArray(value === null || value === void 0 ? void 0 : value.children) && value.children.length
            ? isCollapsed
                ? value.children.slice(0, 1)
                : value.children
            : null;
        var param = { depth: depth, breadth: (_a = body === null || body === void 0 ? void 0 : body.length) !== null && _a !== void 0 ? _a : 0 };
        var addConditionVisibleBool = (_b = isAddBtnVisibleOn === null || isAddBtnVisibleOn === void 0 ? void 0 : isAddBtnVisibleOn(param)) !== null && _b !== void 0 ? _b : true;
        var addConditionGroupVisibleBool = (_c = isAddGroupBtnVisibleOn === null || isAddGroupBtnVisibleOn === void 0 ? void 0 : isAddGroupBtnVisibleOn(param)) !== null && _c !== void 0 ? _c : true;
        return (React__default.createElement("div", { className: cx('CBGroup'), "data-group-id": value === null || value === void 0 ? void 0 : value.id },
            builderMode === 'simple' && showANDOR === false ? null : (React__default.createElement("div", { className: cx('CBGroup-toolbarCondition'), draggable: draggable, onDragStart: onDragStart },
                Array.isArray(value === null || value === void 0 ? void 0 : value.children) && value.children.length > 1 ? (React__default.createElement("div", { className: cx('CBGroup-toolbarCondition-arrow', {
                        'is-collapse': isCollapsed
                    }), onClick: this.toggleCollapse },
                    React__default.createElement(SvgDownArrowBold, null))) : null,
                showNot ? (React__default.createElement(Button, { onClick: this.handleNotClick, className: "m-b-sm z-10", size: "xs", active: value === null || value === void 0 ? void 0 : value.not, disabled: disabled }, __('Condition.not'))) : null,
                React__default.createElement(EnhancedSelect, { options: [
                        {
                            label: __('Condition.and'),
                            value: 'and'
                        },
                        {
                            label: __('Condition.or'),
                            value: 'or'
                        }
                    ], testIdBuilder: testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild((value === null || value === void 0 ? void 0 : value.conjunction) || 'and'), value: (value === null || value === void 0 ? void 0 : value.conjunction) || 'and', disabled: disabled, onChange: this.handleConjunctionChange, clearable: false }))),
            React__default.createElement("div", { className: cx('CBGroup-body-wrapper') },
                React__default.createElement("div", { className: cx('CBGroup-body') },
                    body ? (body.map(function (item, index) { return (React__default.createElement(GroupOrItem, { draggable: draggable && value.children.length > 1, onDragStart: onDragStart, config: config, key: item.id, fields: fields, fieldClassName: fieldClassName, value: item, index: index, onChange: _this.handleItemChange, funcs: funcs, onRemove: _this.handleItemRemove, data: data, disabled: disabled, searchable: searchable, builderMode: builderMode, formula: formula, popOverContainer: popOverContainer, renderEtrValue: renderEtrValue, selectMode: selectMode, isCollapsed: isCollapsed, depth: depth, isAddBtnVisibleOn: isAddBtnVisibleOn, isAddGroupBtnVisibleOn: isAddGroupBtnVisibleOn, showIf: showIf, formulaForIf: formulaForIf, testIdBuilder: testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild("group-".concat(index)) })); })) : (React__default.createElement("div", { className: cx("CBGroup-placeholder ".concat(builderMode === 'simple' ? 'simple' : '')) }, __('Condition.blank'))),
                    isCollapsed ? (React__default.createElement("div", { className: cx('CBGroup-body-collapse') },
                        React__default.createElement("span", { onClick: this.toggleCollapse },
                            __('Condition.collapse'),
                            " ",
                            React__default.createElement(SvgDownArrowBold, null)))) : null),
                isCollapsed ? null : (React__default.createElement("div", { className: cx('CBGroup-toolbar'), draggable: draggable, onDragStart: onDragStart },
                    React__default.createElement("div", { className: cx("CBGroup-toolbarConditionAdd".concat(builderMode === 'simple' ? '-simple' : '')) },
                        React__default.createElement("div", { className: cx('ButtonGroup') },
                            addConditionVisibleBool ? (React__default.createElement(Button, { level: "link", onClick: this.handleAdd, size: "xs", disabled: disabled, testIdBuilder: testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild('add') }, __('Condition.add_cond'))) : null,
                            addConditionGroupVisibleBool && builderMode !== 'simple' ? (React__default.createElement(Button, { onClick: this.handleAddGroup, size: "xs", disabled: disabled, level: "link", testIdBuilder: testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild('add-group') }, __('Condition.add_cond_group'))) : null,
                            removeable ? (React__default.createElement(Button, { onClick: onRemove, size: "xs", disabled: disabled, level: "link", testIdBuilder: testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild('add-del') }, __('Condition.delete_cond_group'))) : null)))))));
    };
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", []),
        __metadata("design:returntype", void 0)
    ], ConditionGroup.prototype, "handleNotClick", null);
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", [Object]),
        __metadata("design:returntype", void 0)
    ], ConditionGroup.prototype, "handleConjunctionChange", null);
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", []),
        __metadata("design:returntype", void 0)
    ], ConditionGroup.prototype, "handleAdd", null);
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", []),
        __metadata("design:returntype", void 0)
    ], ConditionGroup.prototype, "handleAddGroup", null);
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", [Object, Number]),
        __metadata("design:returntype", void 0)
    ], ConditionGroup.prototype, "handleItemChange", null);
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", [Number]),
        __metadata("design:returntype", void 0)
    ], ConditionGroup.prototype, "handleItemRemove", null);
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", []),
        __metadata("design:returntype", void 0)
    ], ConditionGroup.prototype, "toggleCollapse", null);
    return ConditionGroup;
}(React__default.Component));
var ConditionGroup$1 = themeable(localeable(ConditionGroup));

export { ConditionGroup, ConditionGroup$1 as default };
