/**
 * 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 amisCore = require('amis-core');
var PopOverContainer = require('../PopOverContainer.js');
var GroupedSelection = require('../GroupedSelection.js');
var ResultBox = require('../ResultBox.js');
var icons = require('../icons.js');
var Expression = require('./Expression.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.type; };
var ConditionFunc = /** @class */ (function (_super) {
    tslib.__extends(ConditionFunc, _super);
    function ConditionFunc() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    ConditionFunc.prototype.handleFuncChange = function (type) {
        var value = tslib.__assign({}, this.props.value);
        value.func = type;
        this.props.onChange(value);
    };
    ConditionFunc.prototype.handleArgChange = function (arg, index) {
        var value = tslib.__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 (_J$X_("div", { className: cx('CBFunc-args') },
            _J$X_("span", null, "("),
            Array.isArray(func.args) && func.args.length ? (_J$X_("div", null, func.args.map(function (item, index) { return (_J$X_(Expression["default"], { 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,
            _J$X_("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
            ? amisCore.findTree(funcs, function (item) { return item.type === value.func; })
            : null;
        return (_J$X_("div", { className: cx('CBFunc') },
            _J$X_(PopOverContainer.PopOverContainer, { popOverRender: function (_a) {
                    var onClose = _a.onClose;
                    return (_J$X_(GroupedSelection["default"], { 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 (_J$X_("div", { className: cx('CBFunc-select') },
                    _J$X_(ResultBox["default"], { className: cx('CBGroup-fieldInput', fieldClassName, isOpened ? 'is-active' : ''), ref: ref, allowInput: false, result: func, onResultChange: amisCore.noop, onResultClick: onClick, placeholder: __('Condition.field_placeholder'), disabled: disabled, testIdBuilder: testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild('resbox') },
                        _J$X_("span", { className: cx('CBGroup-fieldCaret') },
                            _J$X_(icons.Icon, { icon: "right-arrow-bold", className: "icon" })))));
            }),
            func ? (this.renderFunc(func)) : (_J$X_("span", { className: cx('CBFunc-error') }, __('Condition.fun_error')))));
    };
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", [String]),
        tslib.__metadata("design:returntype", void 0)
    ], ConditionFunc.prototype, "handleFuncChange", null);
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", [Object, Number]),
        tslib.__metadata("design:returntype", void 0)
    ], ConditionFunc.prototype, "handleArgChange", null);
    return ConditionFunc;
}(React__default["default"].Component));
var ConditionFunc$1 = amisCore.themeable(amisCore.localeable(ConditionFunc));

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