/**
 * 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 amisCore = require('amis-core');
var Button = require('../Button.js');
var GroupOrItem = require('./GroupOrItem.js');
var Select = require('../Select.js');
require('../icons.js');
var downArrowBold = require('../../icons/down-arrow-bold.svg.js');

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

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

var __react_jsx__ = require('react');
var _J$X_ = (__react_jsx__["default"] || __react_jsx__).createElement;
(__react_jsx__["default"] || __react_jsx__).Fragment;
var ConditionGroup = /** @class */ (function (_super) {
    tslib.__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 tslib.__assign({ id: amisCore.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: amisCore.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: amisCore.guid(),
            conjunction: 'and',
            children: [
                {
                    id: amisCore.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 anyFieldAvailable = amisCore.someTree(fields, function (field) { return field.disabled !== true; });
        var addConditionVisibleBool = (_b = isAddBtnVisibleOn === null || isAddBtnVisibleOn === void 0 ? void 0 : isAddBtnVisibleOn(param)) !== null && _b !== void 0 ? _b : anyFieldAvailable;
        var addConditionGroupVisibleBool = (_c = isAddGroupBtnVisibleOn === null || isAddGroupBtnVisibleOn === void 0 ? void 0 : isAddGroupBtnVisibleOn(param)) !== null && _c !== void 0 ? _c : anyFieldAvailable;
        return (_J$X_("div", { className: cx('CBGroup'), "data-group-id": value === null || value === void 0 ? void 0 : value.id },
            builderMode === 'simple' && showANDOR === false ? null : (_J$X_("div", { className: cx('CBGroup-toolbarCondition'), draggable: draggable, onDragStart: onDragStart },
                Array.isArray(value === null || value === void 0 ? void 0 : value.children) && value.children.length > 1 ? (_J$X_("div", { className: cx('CBGroup-toolbarCondition-arrow', {
                        'is-collapse': isCollapsed
                    }), onClick: this.toggleCollapse },
                    _J$X_(downArrowBold["default"], null))) : null,
                showNot ? (_J$X_(Button["default"], { 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,
                _J$X_(Select["default"], { 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 }))),
            _J$X_("div", { className: cx('CBGroup-body-wrapper') },
                _J$X_("div", { className: cx('CBGroup-body') },
                    body ? (body.map(function (item, index) { return (_J$X_(GroupOrItem["default"], { 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)) })); })) : (_J$X_("div", { className: cx("CBGroup-placeholder ".concat(builderMode === 'simple' ? 'simple' : '')) }, __('Condition.blank'))),
                    isCollapsed ? (_J$X_("div", { className: cx('CBGroup-body-collapse') },
                        _J$X_("span", { onClick: this.toggleCollapse },
                            __('Condition.collapse'),
                            " ",
                            _J$X_(downArrowBold["default"], null)))) : null),
                isCollapsed ? null : (_J$X_("div", { className: cx('CBGroup-toolbar'), draggable: draggable, onDragStart: onDragStart },
                    _J$X_("div", { className: cx("CBGroup-toolbarConditionAdd".concat(builderMode === 'simple' ? '-simple' : '')) },
                        _J$X_("div", { className: cx('ButtonGroup') },
                            addConditionVisibleBool ? (_J$X_(Button["default"], { 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' ? (_J$X_(Button["default"], { 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 ? (_J$X_(Button["default"], { 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)))))));
    };
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", []),
        tslib.__metadata("design:returntype", void 0)
    ], ConditionGroup.prototype, "handleNotClick", null);
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", [Object]),
        tslib.__metadata("design:returntype", void 0)
    ], ConditionGroup.prototype, "handleConjunctionChange", null);
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", []),
        tslib.__metadata("design:returntype", void 0)
    ], ConditionGroup.prototype, "handleAdd", null);
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", []),
        tslib.__metadata("design:returntype", void 0)
    ], ConditionGroup.prototype, "handleAddGroup", null);
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", [Object, Number]),
        tslib.__metadata("design:returntype", void 0)
    ], ConditionGroup.prototype, "handleItemChange", null);
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", [Number]),
        tslib.__metadata("design:returntype", void 0)
    ], ConditionGroup.prototype, "handleItemRemove", null);
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", []),
        tslib.__metadata("design:returntype", void 0)
    ], ConditionGroup.prototype, "toggleCollapse", null);
    return ConditionGroup;
}(React__default["default"].Component));
var ConditionGroup$1 = amisCore.themeable(amisCore.localeable(ConditionGroup));

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