/**
 * amis-ui v6.11.0-beta.7
 * Copyright 2018-2025 fex
 */

import { __extends, __assign, __decorate, __metadata } from 'tslib';
import React__default from 'react';
import ConditionField from './Field.js';
import { themeable, localeable, findTree, filterTree, autobind } from 'amis-core';
import Value from './Value.js';
import InputSwitch from './InputSwitch.js';
import ConditionFunc from './Func.js';

var fieldMap = {
    value: '值',
    field: '字段',
    func: '函数'
};
var Expression = /** @class */ (function (_super) {
    __extends(Expression, _super);
    function Expression() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    Expression.prototype.handleInputTypeChange = function (type) {
        var _a;
        var value = this.props.value;
        var onChange = this.props.onChange;
        if (type === 'value') {
            value = '';
        }
        else if (type === 'func') {
            value = {
                type: 'func',
                func: (_a = findTree(this.props.funcs, function (item) { return item.type; })) === null || _a === void 0 ? void 0 : _a.type,
                args: []
            };
        }
        else if (type === 'field') {
            value = {
                type: 'field',
                field: ''
            };
        }
        onChange(value, this.props.index);
    };
    Expression.prototype.handleValueChange = function (data) {
        this.props.onChange(data, this.props.index);
    };
    Expression.prototype.handleFieldChange = function (field) {
        var value = this.props.value;
        var onChange = this.props.onChange;
        value = {
            type: 'field',
            field: field
        };
        onChange(value, this.props.index);
    };
    Expression.prototype.handleFuncChange = function (func) {
        var value = this.props.value;
        var onChange = this.props.onChange;
        value = __assign(__assign({}, func), { type: 'func' });
        onChange(value, this.props.index);
    };
    Expression.prototype.render = function () {
        var _a = this.props, value = _a.value, valueField = _a.valueField, allowedTypes = _a.allowedTypes, funcs = _a.funcs, fields = _a.fields, op = _a.op; _a.classnames; var fieldClassName = _a.fieldClassName, config = _a.config, data = _a.data, disabled = _a.disabled, searchable = _a.searchable, formula = _a.formula, popOverContainer = _a.popOverContainer, selectMode = _a.selectMode, renderEtrValue = _a.renderEtrValue, testIdBuilder = _a.testIdBuilder;
        var inputType = ((value === null || value === void 0 ? void 0 : value.type) === 'field'
            ? 'field'
            : (value === null || value === void 0 ? void 0 : value.type) === 'func'
                ? 'func'
                : value !== undefined
                    ? 'value'
                    : undefined) ||
            (allowedTypes === null || allowedTypes === void 0 ? void 0 : allowedTypes[0]) ||
            'value';
        var types = allowedTypes || ['value', 'field', 'func'];
        if ((!Array.isArray(funcs) || !funcs.length) && ~types.indexOf('func')) {
            types.splice(types.indexOf('func'), 1);
        }
        return (React__default.createElement(React__default.Fragment, null,
            inputType === 'value' ? (React__default.createElement(Value, { field: valueField, value: value, onChange: this.handleValueChange, op: op, data: data, disabled: disabled, formula: formula, popOverContainer: popOverContainer, renderEtrValue: renderEtrValue, testIdBuilder: testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild('tValue') })) : null,
            inputType === 'field' ? (React__default.createElement(ConditionField, { value: value === null || value === void 0 ? void 0 : value.field, onChange: this.handleFieldChange, fieldClassName: fieldClassName, disabled: disabled, searchable: searchable, popOverContainer: popOverContainer, selectMode: selectMode, testIdBuilder: testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild('tField'), options: valueField
                    ? filterTree(fields, function (item) {
                        return item.children ||
                            item.type === valueField.type;
                    })
                    : fields })) : null,
            inputType === 'func' ? (React__default.createElement(ConditionFunc, { config: config, value: value, onChange: this.handleFuncChange, fieldClassName: fieldClassName, funcs: funcs, fields: fields, allowedTypes: allowedTypes, disabled: disabled, testIdBuilder: testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild('tFunc') })) : null,
            types.length > 1 ? (React__default.createElement(InputSwitch, { disabled: disabled, value: inputType, popOverContainer: popOverContainer, onChange: this.handleInputTypeChange, testIdBuilder: testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild('tSwitch'), options: types.map(function (item) { return ({
                    label: fieldMap[item],
                    value: item
                }); }) })) : null));
    };
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", [String]),
        __metadata("design:returntype", void 0)
    ], Expression.prototype, "handleInputTypeChange", null);
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", [Object]),
        __metadata("design:returntype", void 0)
    ], Expression.prototype, "handleValueChange", null);
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", [String]),
        __metadata("design:returntype", void 0)
    ], Expression.prototype, "handleFieldChange", null);
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", [Object]),
        __metadata("design:returntype", void 0)
    ], Expression.prototype, "handleFuncChange", null);
    return Expression;
}(React__default.Component));
var Expression$1 = themeable(localeable(Expression));

export { Expression, Expression$1 as default };
