"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FormulaControlRenderer = void 0;
var tslib_1 = require("tslib");
var react_1 = tslib_1.__importDefault(require("react"));
var Item_1 = require("./Item");
var tpl_1 = require("../../utils/tpl");
var helper_1 = require("../../utils/helper");
var FormulaControl = /** @class */ (function (_super) {
    tslib_1.__extends(FormulaControl, _super);
    function FormulaControl() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.inited = false;
        return _this;
    }
    FormulaControl.prototype.componentDidMount = function () {
        var _a = this.props, formInited = _a.formInited, initSet = _a.initSet;
        // 如果在表单中，还是等初始化数据过来才算
        if (formInited === false) {
            return;
        }
        this.inited = true;
        initSet === false || this.initSet();
    };
    FormulaControl.prototype.componentDidUpdate = function (prevProps) {
        var _a = this.props, formInited = _a.formInited, initSet = _a.initSet, autoSet = _a.autoSet;
        if (this.inited) {
            autoSet === false || this.autoSet(prevProps);
        }
        else if (formInited === true || typeof formInited === 'undefined') {
            this.inited = true;
            initSet === false || this.initSet();
        }
    };
    FormulaControl.prototype.initSet = function () {
        var _a = this.props, formula = _a.formula, data = _a.data, setPrinstineValue = _a.setPrinstineValue, initSet = _a.initSet, condition = _a.condition;
        if (!formula) {
            return;
        }
        else if (condition &&
            !~condition.indexOf('$') &&
            !~condition.indexOf('<%') &&
            !tpl_1.evalJS(condition, data)) {
            return;
        }
        var result = tpl_1.evalJS(formula, data);
        result !== null && setPrinstineValue(result);
    };
    FormulaControl.prototype.autoSet = function (prevProps) {
        var props = this.props;
        var formula = prevProps.formula, data = prevProps.data, onChange = prevProps.onChange, value = prevProps.value, condition = prevProps.condition;
        if (formula &&
            props.formula &&
            helper_1.isObjectShallowModified(data, props.data, false) &&
            value === props.value) {
            var nextResult = tpl_1.evalJS(props.formula, props.data);
            if (condition && props.condition) {
                if (!!~condition.indexOf('$') || !!~condition.indexOf('<%')) {
                    // 使用${xxx}，来监听某个变量的变化
                    if (tpl_1.filter(condition, data) !== tpl_1.filter(props.condition, props.data)) {
                        onChange(nextResult);
                    }
                }
                else if (tpl_1.evalJS(props.condition, props.data)) {
                    // 使用 data.xxx == 'a' 表达式形式来判断
                    onChange(nextResult);
                }
            }
            else {
                var prevResult = tpl_1.evalJS(formula, data);
                if (JSON.stringify(prevResult) !== JSON.stringify(nextResult)) {
                    onChange(nextResult !== null && nextResult !== void 0 ? nextResult : '');
                }
            }
        }
    };
    FormulaControl.prototype.doAction = function () {
        // 不细化具体是啥动作了，先重新计算，并把值运用上。
        var _a = this.props, formula = _a.formula, data = _a.data, onChange = _a.onChange, autoSet = _a.autoSet, value = _a.value;
        var result = tpl_1.evalJS(formula, data);
        onChange(result);
    };
    FormulaControl.prototype.render = function () {
        return null;
    };
    return FormulaControl;
}(react_1.default.Component));
exports.default = FormulaControl;
var FormulaControlRenderer = /** @class */ (function (_super) {
    tslib_1.__extends(FormulaControlRenderer, _super);
    function FormulaControlRenderer() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    FormulaControlRenderer = tslib_1.__decorate([
        Item_1.FormItem({
            type: 'formula',
            wrap: false,
            strictMode: false,
            sizeMutable: false
        })
    ], FormulaControlRenderer);
    return FormulaControlRenderer;
}(FormulaControl));
exports.FormulaControlRenderer = FormulaControlRenderer;
//# sourceMappingURL=./renderers/Form/Formula.js.map
