/**
 * 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 ReactDOM = require('react-dom');
var amisCore = require('amis-core');
var icons = require('../icons.js');
var Expression = require('./Expression.js');
var config = require('./config.js');
var PopOverContainer = require('../PopOverContainer.js');
var GroupedSelection = require('../GroupedSelection.js');
var ResultBox = require('../ResultBox.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 option2value = function (item) { return item.value; };
var ConditionItem = /** @class */ (function (_super) {
    tslib.__extends(ConditionItem, _super);
    function ConditionItem() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    ConditionItem.prototype.handleLeftFieldSelect = function (field) {
        var value = tslib.__assign({}, this.props.value);
        var onChange = this.props.onChange;
        value.left = field;
        onChange(value, this.props.index);
    };
    ConditionItem.prototype.handleLeftInputTypeChange = function (type) {
        var value = tslib.__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 = amisCore.findTree(fields, function (f) { return f.name === leftValue.field; });
        var value = tslib.__assign(tslib.__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 = amisCore.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 = tslib.__assign(tslib.__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 = tslib.__assign(tslib.__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 = tslib.__assign(tslib.__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 (_J$X_(Expression["default"], { 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$1 = _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 = amisCore.findTree(funcs, function (i) { return i.type === left.func; });
            if (func) {
                operators = (_a = config$1.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 = amisCore.findTree(fields, function (i) { return i.name === left.field; });
            if (field) {
                operators = field.operators || ((_b = config$1.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(config.OperationMap[operator]),
                        value: operator
                    };
                }
                else {
                    return operator;
                }
            });
            return (_J$X_(PopOverContainer.PopOverContainer, { mobileUI: mobileUI, disabled: !!((value === null || value === void 0 ? void 0 : value.op) && operators.length < 2), popOverContainer: popOverContainer || (function () { return ReactDOM.findDOMNode(_this); }), popOverRender: function (_a) {
                    var onClose = _a.onClose;
                    return (_J$X_(GroupedSelection["default"], { 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 (_J$X_("div", { className: cx('CBGroup-operator') },
                    _J$X_(ResultBox["default"], { 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(config.OperationMap[value === null || value === void 0 ? void 0 : value.op]), onResultChange: amisCore.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 ? (_J$X_("span", { className: cx('CBGroup-operatorCaret') },
                        _J$X_(icons.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 = amisCore.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 = amisCore.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 = tslib.__assign(tslib.__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 = amisCore.findTree(fields, function (i) { return i.name === (value === null || value === void 0 ? void 0 : value.left).field; });
            if (leftField) {
                field = tslib.__assign(tslib.__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 (_J$X_(React__default["default"].Fragment, null,
                _J$X_(Expression["default"], { 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")) }),
                _J$X_("span", { className: cx('CBSeprator') }, "~"),
                _J$X_(Expression["default"], { 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 (_J$X_("span", { key: i },
                    _J$X_(Expression["default"], { key: "".concat(field.name, "-").concat(i), config: config, op: op, funcs: funcs, valueField: tslib.__assign(tslib.__assign({}, field), { value: schema }), value: amisCore.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 (_J$X_(Expression["default"], { 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 (_J$X_("div", { className: cx('CBItem') },
            this.renderLeft(),
            this.renderOperator(),
            this.renderRight()));
    };
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", [Object]),
        tslib.__metadata("design:returntype", void 0)
    ], ConditionItem.prototype, "handleLeftFieldSelect", null);
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", [String]),
        tslib.__metadata("design:returntype", void 0)
    ], ConditionItem.prototype, "handleLeftInputTypeChange", null);
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", [Object]),
        tslib.__metadata("design:returntype", void 0)
    ], ConditionItem.prototype, "handleLeftChange", null);
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", [Object]),
        tslib.__metadata("design:returntype", void 0)
    ], ConditionItem.prototype, "handleOperatorChange", null);
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", [Object]),
        tslib.__metadata("design:returntype", void 0)
    ], ConditionItem.prototype, "handleRightChange", null);
    return ConditionItem;
}(React__default["default"].Component));
var ConditionItem$1 = amisCore.themeable(amisCore.localeable(ConditionItem));

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