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

import { __extends, __assign, __decorate, __metadata } from 'tslib';
import React__default from 'react';
import { findDOMNode } from 'react-dom';
import { findTree, noop, getVariable, autobind, themeable, localeable } from 'amis-core';
import { Icon } from '../icons.js';
import Expression from './Expression.js';
import { OperationMap } from './config.js';
import { PopOverContainer } from '../PopOverContainer.js';
import GroupedSelection from '../GroupedSelection.js';
import ResultBox from '../ResultBox.js';

var option2value = function (item) { return item.value; };
var ConditionItem = /** @class */ (function (_super) {
    __extends(ConditionItem, _super);
    function ConditionItem() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    ConditionItem.prototype.handleLeftFieldSelect = function (field) {
        var value = __assign({}, this.props.value);
        var onChange = this.props.onChange;
        value.left = field;
        onChange(value, this.props.index);
    };
    ConditionItem.prototype.handleLeftInputTypeChange = function (type) {
        var value = __assign({}, this.props.value);
        var onChange = this.props.onChange;
        if (type === 'func') {
            value.left = { type: 'func' };
        }
        else {
            value.left = '';
        }
        onChange(value, this.props.index);
    };
    ConditionItem.prototype.handleLeftChange = function (leftValue) {
        var _a, _b, _c, _d;
        var _e = this.props, fields = _e.fields, config = _e.config;
        // 获取默认Op
        var field = findTree(fields, function (f) { return f.name === leftValue.field; });
        var value = __assign(__assign({}, this.props.value), { left: leftValue, op: (_d = (_b = (_a = field === null || field === void 0 ? void 0 : field.defaultOp) !== null && _a !== void 0 ? _a : (Array.isArray(field === null || field === void 0 ? void 0 : field.operators) && field.operators.length === 1
                ? field.operators[0].value || field.operators[0]
                : undefined)) !== null && _b !== void 0 ? _b : (_c = config.types[field === null || field === void 0 ? void 0 : field.type]) === null || _c === void 0 ? void 0 : _c.defaultOp) !== null && _d !== void 0 ? _d : undefined, right: undefined });
        var onChange = this.props.onChange;
        onChange(value, this.props.index);
    };
    ConditionItem.prototype.handleOperatorChange = function (op) {
        var _a;
        var _b = this.props, fields = _b.fields, value = _b.value, index = _b.index, onChange = _b.onChange, formula = _b.formula;
        var useFormulaInput = (formula === null || formula === void 0 ? void 0 : formula.mode) === 'input-group' && (formula === null || formula === void 0 ? void 0 : formula.inputSettings);
        var leftFieldSchema = findTree(fields, function (i) { var _a; return i.name === ((_a = value === null || value === void 0 ? void 0 : value.left) === null || _a === void 0 ? void 0 : _a.field); });
        var result = __assign(__assign({}, value), { op: op, 
            /** 使用公式编辑器模式时，因为不同条件下值格式不一致（比如select类型下包含和等于对应的multiple会变化），所以变化条件时需要清空right值 */
            right: useFormulaInput
                ? leftFieldSchema === null || leftFieldSchema === void 0 ? void 0 : leftFieldSchema.defaultValue
                : (_a = value.right) !== null && _a !== void 0 ? _a : leftFieldSchema === null || leftFieldSchema === void 0 ? void 0 : leftFieldSchema.defaultValue });
        onChange(result, index);
    };
    ConditionItem.prototype.handleRightChange = function (rightValue) {
        var value = __assign(__assign({}, this.props.value), { right: rightValue });
        var onChange = this.props.onChange;
        onChange(value, this.props.index);
    };
    ConditionItem.prototype.handleRightSubChange = function (index, rightValue) {
        var _a, _b;
        var origin;
        if (typeof index === 'string') {
            origin = Object.assign({}, (_a = this.props.value) === null || _a === void 0 ? void 0 : _a.right);
            origin[index] = rightValue;
        }
        else {
            origin = Array.isArray((_b = this.props.value) === null || _b === void 0 ? void 0 : _b.right)
                ? this.props.value.right.concat()
                : [];
            origin[index] = rightValue;
        }
        var value = __assign(__assign({}, this.props.value), { right: origin });
        var onChange = this.props.onChange;
        onChange(value, this.props.index);
    };
    ConditionItem.prototype.renderLeft = function () {
        var _a = this.props, value = _a.value, fields = _a.fields, funcs = _a.funcs, config = _a.config, disabled = _a.disabled, fieldClassName = _a.fieldClassName, searchable = _a.searchable, popOverContainer = _a.popOverContainer, selectMode = _a.selectMode, testIdBuilder = _a.testIdBuilder;
        return (React__default.createElement(Expression, { config: config, funcs: funcs, value: value.left, fieldClassName: fieldClassName, onChange: this.handleLeftChange, fields: fields, disabled: disabled, searchable: searchable, popOverContainer: popOverContainer, selectMode: selectMode, testIdBuilder: testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild('left'), allowedTypes: ['field', 'func'].filter(function (type) { return type === 'field' || type === 'func'; }) }));
    };
    ConditionItem.prototype.renderOperator = function () {
        var _this = this;
        var _a, _b;
        var _c = this.props, funcs = _c.funcs, config = _c.config, fields = _c.fields, value = _c.value, cx = _c.classnames, disabled = _c.disabled, popOverContainer = _c.popOverContainer, testIdBuilder = _c.testIdBuilder, mobileUI = _c.mobileUI;
        var left = value === null || value === void 0 ? void 0 : value.left;
        var operators = [];
        if ((left === null || left === void 0 ? void 0 : left.type) === 'func') {
            var func = findTree(funcs, function (i) { return i.type === left.func; });
            if (func) {
                operators = (_a = config.types[func.returnType]) === null || _a === void 0 ? void 0 : _a.operators;
            }
        }
        else if ((left === null || left === void 0 ? void 0 : left.type) === 'field') {
            var field = findTree(fields, function (i) { return i.name === left.field; });
            if (field) {
                operators = field.operators || ((_b = config.types[field.type]) === null || _b === void 0 ? void 0 : _b.operators);
            }
        }
        if (Array.isArray(operators) && operators.length) {
            var __1 = this.props.translate;
            var options_1 = operators.map(function (operator) {
                if (typeof operator === 'string') {
                    return {
                        label: __1(OperationMap[operator]),
                        value: operator
                    };
                }
                else {
                    return operator;
                }
            });
            return (React__default.createElement(PopOverContainer, { mobileUI: mobileUI, disabled: !!((value === null || value === void 0 ? void 0 : value.op) && operators.length < 2), popOverContainer: popOverContainer || (function () { return findDOMNode(_this); }), popOverRender: function (_a) {
                    var onClose = _a.onClose;
                    return (React__default.createElement(GroupedSelection, { onClick: onClose, option2value: option2value, onChange: _this.handleOperatorChange, options: options_1, value: value.op, multiple: false, testIdBuilder: testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild('operator-group') }));
                } }, function (_a) {
                var _b;
                var onClick = _a.onClick, isOpened = _a.isOpened, ref = _a.ref; _a.disabled;
                return (React__default.createElement("div", { className: cx('CBGroup-operator') },
                    React__default.createElement(ResultBox, { className: cx('CBGroup-operatorInput', isOpened ? 'is-active' : ''), ref: ref, allowInput: false, result: ((_b = options_1.find(function (option) { return option.value === value.op; })) === null || _b === void 0 ? void 0 : _b.label) ||
                            __1(OperationMap[value === null || value === void 0 ? void 0 : value.op]), onResultChange: noop, onResultClick: onClick, disabled: disabled, placeholder: __1('Condition.cond_placeholder'), mobileUI: mobileUI, testIdBuilder: testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild('operator-resbox') }, !mobileUI ? (React__default.createElement("span", { className: cx('CBGroup-operatorCaret') },
                        React__default.createElement(Icon, { icon: "right-arrow-bold", className: "icon" }))) : null)));
            }));
        }
        return null;
    };
    ConditionItem.prototype.renderRight = function () {
        var _a = this.props, value = _a.value, funcs = _a.funcs, fields = _a.fields;
        if (!(value === null || value === void 0 ? void 0 : value.op)) {
            return null;
        }
        var left = value === null || value === void 0 ? void 0 : value.left;
        var leftType = '';
        if ((left === null || left === void 0 ? void 0 : left.type) === 'func') {
            var func = findTree(funcs, function (i) { return i.type === left.func; });
            if (func) {
                leftType = func.returnType;
            }
        }
        else if ((left === null || left === void 0 ? void 0 : left.type) === 'field') {
            var field = findTree(fields, function (i) { return i.name === left.field; });
            if (field) {
                leftType = field.type;
            }
        }
        if (leftType) {
            return this.renderRightWidgets(leftType, value.op);
        }
        return null;
    };
    ConditionItem.prototype.renderRightWidgets = function (type, op) {
        var _this = this;
        var _a, _b, _c, _d;
        var _e = this.props, funcs = _e.funcs, value = _e.value, data = _e.data, fields = _e.fields, config = _e.config, cx = _e.classnames, disabled = _e.disabled, formula = _e.formula, popOverContainer = _e.popOverContainer, renderEtrValue = _e.renderEtrValue, testIdBuilder = _e.testIdBuilder;
        var field = __assign(__assign({}, config.types[type]), { type: type });
        var option;
        if (((_a = value === null || value === void 0 ? void 0 : value.left) === null || _a === void 0 ? void 0 : _a.type) === 'field') {
            var leftField = findTree(fields, function (i) { return i.name === (value === null || value === void 0 ? void 0 : value.left).field; });
            if (leftField) {
                field = __assign(__assign({}, field), leftField);
                option = (_b = field.operators) === null || _b === void 0 ? void 0 : _b.find(function (option) { return typeof option !== 'string' && (option === null || option === void 0 ? void 0 : option.value) === op; });
            }
        }
        if (op === 'is_empty' || op === 'is_not_empty') {
            return null;
        }
        else if (op === 'between' || op === 'not_between') {
            return (React__default.createElement(React__default.Fragment, null,
                React__default.createElement(Expression, { key: "".concat(field.name, "-0"), config: config, funcs: funcs, valueField: field, value: (_c = value.right) === null || _c === void 0 ? void 0 : _c[0], data: data, onChange: this.handleRightSubChange.bind(this, 0), fields: fields, allowedTypes: (field === null || field === void 0 ? void 0 : field.valueTypes) ||
                        config.valueTypes || ['value', 'field', 'func'], disabled: disabled, formula: formula, popOverContainer: popOverContainer, renderEtrValue: renderEtrValue, testIdBuilder: testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild("right-".concat(op, "-").concat(field.name, "-exp-0")) }),
                React__default.createElement("span", { className: cx('CBSeprator') }, "~"),
                React__default.createElement(Expression, { key: "".concat(field.name, "-1"), config: config, funcs: funcs, valueField: field, value: (_d = value.right) === null || _d === void 0 ? void 0 : _d[1], data: data, onChange: this.handleRightSubChange.bind(this, 1), fields: fields, allowedTypes: (field === null || field === void 0 ? void 0 : field.valueTypes) ||
                        config.valueTypes || ['value', 'field', 'func'], disabled: disabled, formula: formula, popOverContainer: popOverContainer, renderEtrValue: renderEtrValue, testIdBuilder: testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild("right-".concat(op, "-").concat(field.name, "-exp-1")) })));
        }
        else if (option && typeof option !== 'string' && option.values) {
            return option.values.map(function (schema, i) {
                return (React__default.createElement("span", { key: i },
                    React__default.createElement(Expression, { key: "".concat(field.name, "-").concat(i), config: config, op: op, funcs: funcs, valueField: __assign(__assign({}, field), { value: schema }), value: getVariable(value.right, schema.name), data: data, onChange: _this.handleRightSubChange.bind(_this, schema.name), fields: fields, allowedTypes: (field === null || field === void 0 ? void 0 : field.valueTypes) ||
                            config.valueTypes || ['value', 'field', 'func'], disabled: disabled, formula: formula, popOverContainer: popOverContainer, renderEtrValue: renderEtrValue, testIdBuilder: testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild("right-".concat(op, "-").concat(field.name, "-exp-").concat(i)) })));
            });
        }
        return (React__default.createElement(Expression, { key: "".concat(field.name, "-0"), config: config, op: op, funcs: funcs, valueField: field, value: value.right, data: data, onChange: this.handleRightChange, fields: fields, allowedTypes: (field === null || field === void 0 ? void 0 : field.valueTypes) || config.valueTypes || ['value', 'field', 'func'], disabled: disabled, formula: formula, popOverContainer: popOverContainer, renderEtrValue: renderEtrValue, testIdBuilder: testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild("right-".concat(op, "-").concat(field.name, "-exp")) }));
    };
    ConditionItem.prototype.render = function () {
        var cx = this.props.classnames;
        return (React__default.createElement("div", { className: cx('CBItem') },
            this.renderLeft(),
            this.renderOperator(),
            this.renderRight()));
    };
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", [Object]),
        __metadata("design:returntype", void 0)
    ], ConditionItem.prototype, "handleLeftFieldSelect", null);
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", [String]),
        __metadata("design:returntype", void 0)
    ], ConditionItem.prototype, "handleLeftInputTypeChange", null);
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", [Object]),
        __metadata("design:returntype", void 0)
    ], ConditionItem.prototype, "handleLeftChange", null);
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", [Object]),
        __metadata("design:returntype", void 0)
    ], ConditionItem.prototype, "handleOperatorChange", null);
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", [Object]),
        __metadata("design:returntype", void 0)
    ], ConditionItem.prototype, "handleRightChange", null);
    return ConditionItem;
}(React__default.Component));
var ConditionItem$1 = themeable(localeable(ConditionItem));

export { ConditionItem, ConditionItem$1 as default };
