"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var _ = require("lodash");
/**
 * 保存内部联动的对象
 */
var default_1 = /** @class */ (function () {
    function default_1(columns, form, context) {
        this.orginColumns = [];
        this.linked = {};
        this.values = {};
        this.AllColumns = [];
        this.form = null;
        this.context = null;
        this.orginColumns = columns;
        this.AllColumns = _.flatten(columns);
        this.form = form;
        this.context = context;
    }
    default_1.prototype.getLink = function (column, params) {
        var _this = this;
        if (!_.has(column, 'link')) {
            return {};
        }
        var _a = this.form, setFieldsValue = _a.setFieldsValue, getFieldValue = _a.getFieldValue;
        var init = function (link) {
            var val = getFieldValue(column.field);
            if ((!_.has(_this.linked, link.field)) && val) {
                _this.linked[link.field] = true;
                _this.onChange(column, val);
            }
        };
        var linkArr = [];
        if (_.isArray(column.link)) {
            linkArr = column.link;
        }
        else {
            linkArr = [column.link];
        }
        window.setTimeout(function () {
            linkArr.map(function (link) {
                var _a;
                var val = getFieldValue(column.field);
                if (val) {
                    _this.values[column.field] = true;
                    init(link);
                }
                else if (_this.values[column.field] === undefined && (column.type == 'select' || column.type == 'radio') && column.options && column.options.length > 0) {
                    _this.values[column.field] = true;
                    setFieldsValue((_a = {}, _a[column.field] = column.options[0].value, _a));
                    init(link);
                }
            });
        }, 20);
        if (column.type === undefined || column.type == 'text') {
            params.onBlur = function (e) { return _this.onChange(column, e.target.value); };
        }
        else if (column.type == 'select') {
            params.onChange = (function (txt) { return _this.onChange(column, txt); });
        }
        else if (column.type == 'radio') {
            params.onChange = (function (evt) { return _this.onChange(column, evt.target.value); });
        }
        return params;
    };
    default_1.prototype.setOption = function (link, arr) {
        var nameField = link.name || 'name';
        var valField = link.value || 'value';
        var newOptions = [];
        arr.map(function (item) {
            newOptions.push({
                name: item[nameField],
                value: item[valField]
            });
        });
        var tmpArr = _.slice(this.orginColumns);
        var find = false;
        for (var i = 0, _i = tmpArr.length; i < _i; i++) {
            if (find)
                break;
            var group = tmpArr[i];
            for (var j = 0, _j = group.length; j < _j; j++) {
                if (group[j].field == link.field) {
                    group[j].options = newOptions;
                    find = true;
                    break;
                }
            }
        }
        this.orginColumns = tmpArr;
        this.context.setState({ columns: tmpArr });
    };
    default_1.prototype.onChange = function (column, txt) {
        var _this = this;
        var childRenLink = [];
        var linkArr = [];
        if (_.isArray(column.link)) {
            linkArr = column.link;
        }
        else {
            linkArr = [column.link];
        }
        var _a = this.form, getFieldValue = _a.getFieldValue, setFieldsValue = _a.setFieldsValue;
        linkArr.map(function (link) {
            var _a, _b, _c, _d, _e;
            var child = _.find(_this.AllColumns, function (item) { return item.field == link.field; });
            if (child && child.link) {
                childRenLink.push(child);
            }
            var resp = link.call(txt);
            // 返回的是一个值
            if (_.isString(resp) || _.isNumber(resp)) {
                setFieldsValue((_a = {}, _a[link.field] = resp + '', _a));
                // 返回的是一个数组
            }
            else if (_.isArray(resp)) {
                var val = getFieldValue(link.field);
                var valueField = link.value || 'value';
                if (resp.length > 0) {
                    if (val == '' || _.find(resp, (_b = {}, _b[valueField] = val, _b)) == null) {
                        setFieldsValue((_c = {}, _c[link.field] = resp[0][link.value || 'value'], _c));
                    }
                }
                else {
                    setFieldsValue((_d = {}, _d[link.field] = '', _d));
                }
                _this.setOption(link, resp);
                //返回的是一个纯对象
            }
            else if (_.isPlainObject(resp)) {
                var rows = _.concat([], _this.orginColumns);
                rows.map(function (row) {
                    row.map(function (column) {
                        if (column.field == link.field) {
                            _.extend(column, resp);
                        }
                    });
                });
                _this.orginColumns = rows;
                _this.context.setState({ columns: rows });
                if (_.has(resp, '_value')) {
                    setFieldsValue((_e = {}, _e[link.field] = resp['_value'], _e));
                }
                //返回的是一个纯Promise对象
            }
            else if (_.isObject(resp)) {
                resp.then(function (json) {
                    var _a, _b, _c;
                    var list = json.list || json.data || [];
                    var val = getFieldValue(link.field);
                    var valueField = link.value || 'value';
                    if (list.length > 0) {
                        if (val == '' || _.find(list, (_a = {}, _a[valueField] = val, _a)) == null) {
                            setFieldsValue((_b = {}, _b[link.field] = list[0][link.value || 'value'], _b));
                        }
                    }
                    else {
                        setFieldsValue((_c = {}, _c[link.field] = '', _c));
                    }
                    _this.setOption(link, list);
                });
            }
        });
        if (childRenLink.length > 0) {
            childRenLink.map(function (item) {
                var val = getFieldValue(item.field);
                _this.onChange(item, val);
            });
        }
    };
    return default_1;
}());
exports.default = default_1;
