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

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

var option2value = function (item) { return item.type; };
var ConditionFunc = /** @class */ (function (_super) {
    __extends(ConditionFunc, _super);
    function ConditionFunc() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    ConditionFunc.prototype.handleFuncChange = function (type) {
        var value = __assign({}, this.props.value);
        value.func = type;
        this.props.onChange(value);
    };
    ConditionFunc.prototype.handleArgChange = function (arg, index) {
        var value = __assign({}, this.props.value);
        value.args = Array.isArray(value.args) ? value.args.concat() : [];
        value.args.splice(index, 1, arg);
        this.props.onChange(value);
    };
    ConditionFunc.prototype.renderFunc = function (func) {
        var _this = this;
        var _a = this.props, cx = _a.classnames, fields = _a.fields, value = _a.value, funcs = _a.funcs, config = _a.config, disabled = _a.disabled, testIdBuilder = _a.testIdBuilder;
        return (React__default.createElement("div", { className: cx('CBFunc-args') },
            React__default.createElement("span", null, "("),
            Array.isArray(func.args) && func.args.length ? (React__default.createElement("div", null, func.args.map(function (item, index) { return (React__default.createElement(Expression, { config: config, key: index, index: index, fields: fields, value: value === null || value === void 0 ? void 0 : value.args[index], valueField: { type: item.type }, onChange: _this.handleArgChange, funcs: funcs, disabled: disabled, testIdBuilder: testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild("exp-".concat(index)) })); }))) : null,
            React__default.createElement("span", null, ")")));
    };
    ConditionFunc.prototype.render = function () {
        var _this = this;
        var _a = this.props, value = _a.value, cx = _a.classnames, fieldClassName = _a.fieldClassName, funcs = _a.funcs, disabled = _a.disabled, testIdBuilder = _a.testIdBuilder, __ = _a.translate;
        var func = value
            ? findTree(funcs, function (item) { return item.type === value.func; })
            : null;
        return (React__default.createElement("div", { className: cx('CBFunc') },
            React__default.createElement(PopOverContainer, { popOverRender: function (_a) {
                    var onClose = _a.onClose;
                    return (React__default.createElement(GroupedSelection, { onClick: onClose, options: funcs, value: func === null || func === void 0 ? void 0 : func.type, option2value: option2value, onChange: _this.handleFuncChange, multiple: false, testIdBuilder: testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild('group') }));
                } }, function (_a) {
                var onClick = _a.onClick, ref = _a.ref, isOpened = _a.isOpened;
                return (React__default.createElement("div", { className: cx('CBFunc-select') },
                    React__default.createElement(ResultBox, { className: cx('CBGroup-fieldInput', fieldClassName, isOpened ? 'is-active' : ''), ref: ref, allowInput: false, result: func, onResultChange: noop, onResultClick: onClick, placeholder: __('Condition.field_placeholder'), disabled: disabled, testIdBuilder: testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild('resbox') },
                        React__default.createElement("span", { className: cx('CBGroup-fieldCaret') },
                            React__default.createElement(Icon, { icon: "right-arrow-bold", className: "icon" })))));
            }),
            func ? (this.renderFunc(func)) : (React__default.createElement("span", { className: cx('CBFunc-error') }, __('Condition.fun_error')))));
    };
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", [String]),
        __metadata("design:returntype", void 0)
    ], ConditionFunc.prototype, "handleFuncChange", null);
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", [Object, Number]),
        __metadata("design:returntype", void 0)
    ], ConditionFunc.prototype, "handleArgChange", null);
    return ConditionFunc;
}(React__default.Component));
var ConditionFunc$1 = themeable(localeable(ConditionFunc));

export { ConditionFunc, ConditionFunc$1 as default };
