/**
 * 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 Field = require('./Field.js');
var amisCore = require('amis-core');
var Value = require('./Value.js');
var InputSwitch = require('./InputSwitch.js');
var Func = require('./Func.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 fieldMap = {
    value: '值',
    field: '字段',
    func: '函数'
};
var Expression = /** @class */ (function (_super) {
    tslib.__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 = amisCore.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 = tslib.__assign(tslib.__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 (_J$X_(React__default["default"].Fragment, null,
            inputType === 'value' ? (_J$X_(Value["default"], { 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' ? (_J$X_(Field["default"], { 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
                    ? amisCore.filterTree(fields, function (item) {
                        return item.children ||
                            item.type === valueField.type;
                    })
                    : fields })) : null,
            inputType === 'func' ? (_J$X_(Func["default"], { 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 ? (_J$X_(InputSwitch["default"], { 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));
    };
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", [String]),
        tslib.__metadata("design:returntype", void 0)
    ], Expression.prototype, "handleInputTypeChange", null);
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", [Object]),
        tslib.__metadata("design:returntype", void 0)
    ], Expression.prototype, "handleValueChange", null);
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", [String]),
        tslib.__metadata("design:returntype", void 0)
    ], Expression.prototype, "handleFieldChange", null);
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", [Object]),
        tslib.__metadata("design:returntype", void 0)
    ], Expression.prototype, "handleFuncChange", null);
    return Expression;
}(React__default["default"].Component));
var Expression$1 = amisCore.themeable(amisCore.localeable(Expression));

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