"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var Options_1 = require("./Options");
var react_1 = tslib_1.__importDefault(require("react"));
var Transfer_1 = tslib_1.__importDefault(require("../../components/Transfer"));
var Options_2 = require("./Options");
var helper_1 = require("../../utils/helper");
var Spinner_1 = tslib_1.__importDefault(require("../../components/Spinner"));
var find_1 = tslib_1.__importDefault(require("lodash/find"));
var Select_1 = require("../../components/Select");
var TransferRenderer = /** @class */ (function (_super) {
    tslib_1.__extends(TransferRenderer, _super);
    function TransferRenderer() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    TransferRenderer.prototype.handleChange = function (value) {
        var _a = this.props, onChange = _a.onChange, joinValues = _a.joinValues, delimiter = _a.delimiter, valueField = _a.valueField, extractValue = _a.extractValue, options = _a.options, setOptions = _a.setOptions;
        var newValue = value;
        var newOptions = options.concat();
        if (Array.isArray(value)) {
            if (joinValues || extractValue) {
                newValue = value.map(function (item) {
                    var resolved = find_1.default(options, Select_1.optionValueCompare(item[valueField || 'value'], valueField || 'value'));
                    if (!resolved) {
                        newOptions.push(item);
                    }
                    return item[valueField || 'value'];
                });
            }
            if (joinValues) {
                newValue = newValue.join(delimiter || ',');
            }
        }
        newOptions.length > options.length && setOptions(newOptions, true);
        onChange(newValue);
    };
    TransferRenderer.prototype.option2value = function (option) {
        return option;
    };
    TransferRenderer.prototype.handleSearch = function (term, cancelExecutor) {
        return tslib_1.__awaiter(this, void 0, void 0, function () {
            var _a, searchApi, options, labelField, valueField, env, data, payload, result, e_1, regexp_1;
            return tslib_1.__generator(this, function (_b) {
                switch (_b.label) {
                    case 0:
                        _a = this.props, searchApi = _a.searchApi, options = _a.options, labelField = _a.labelField, valueField = _a.valueField, env = _a.env, data = _a.data;
                        if (!searchApi) return [3 /*break*/, 5];
                        _b.label = 1;
                    case 1:
                        _b.trys.push([1, 3, , 4]);
                        return [4 /*yield*/, env.fetcher(searchApi, helper_1.createObject(data, { term: term }), {
                                cancelExecutor: cancelExecutor
                            })];
                    case 2:
                        payload = _b.sent();
                        if (!payload.ok) {
                            throw new Error(payload.msg || '搜索请求异常');
                        }
                        result = payload.data.options || payload.data.items || payload.data;
                        if (!Array.isArray(result)) {
                            throw new Error('期望接口返回数组信息');
                        }
                        return [2 /*return*/, result.map(function (item) {
                                var resolved = null;
                                if (Array.isArray(options)) {
                                    resolved = find_1.default(options, Select_1.optionValueCompare(item[valueField || 'value'], valueField));
                                }
                                return resolved || item;
                            })];
                    case 3:
                        e_1 = _b.sent();
                        if (!env.isCancel(e_1)) {
                            env.notify('error', e_1.message);
                        }
                        return [2 /*return*/, []];
                    case 4: return [3 /*break*/, 6];
                    case 5:
                        if (term) {
                            regexp_1 = helper_1.string2regExp(term);
                            return [2 /*return*/, helper_1.filterTree(options, function (option) {
                                    return !!((Array.isArray(option.children) && option.children.length) ||
                                        regexp_1.test(option[labelField || 'label']) ||
                                        regexp_1.test(option[valueField || 'value']));
                                }, 0, true)];
                        }
                        else {
                            return [2 /*return*/, options];
                        }
                        _b.label = 6;
                    case 6: return [2 /*return*/];
                }
            });
        });
    };
    TransferRenderer.prototype.render = function () {
        var _a = this.props, className = _a.className, cx = _a.classnames, options = _a.options, selectedOptions = _a.selectedOptions, sortable = _a.sortable, selectMode = _a.selectMode, columns = _a.columns, loading = _a.loading, searchable = _a.searchable, searchResultMode = _a.searchResultMode;
        return (react_1.default.createElement("div", { className: cx('TransferControl', className) },
            react_1.default.createElement(Transfer_1.default, { value: selectedOptions, options: options, onChange: this.handleChange, option2value: this.option2value, sortable: sortable, selectMode: selectMode, searchResultMode: searchResultMode, columns: columns, onSearch: searchable ? this.handleSearch : undefined }),
            react_1.default.createElement(Spinner_1.default, { overlay: true, key: "info", show: loading })));
    };
    var _a, _b, _c;
    tslib_1.__decorate([
        helper_1.autobind,
        tslib_1.__metadata("design:type", Function),
        tslib_1.__metadata("design:paramtypes", [typeof (_a = typeof Array !== "undefined" && Array) === "function" ? _a : Object]),
        tslib_1.__metadata("design:returntype", void 0)
    ], TransferRenderer.prototype, "handleChange", null);
    tslib_1.__decorate([
        helper_1.autobind,
        tslib_1.__metadata("design:type", Function),
        tslib_1.__metadata("design:paramtypes", [typeof (_b = typeof Options_2.Option !== "undefined" && Options_2.Option) === "function" ? _b : Object]),
        tslib_1.__metadata("design:returntype", void 0)
    ], TransferRenderer.prototype, "option2value", null);
    tslib_1.__decorate([
        helper_1.autobind,
        tslib_1.__metadata("design:type", Function),
        tslib_1.__metadata("design:paramtypes", [String, typeof (_c = typeof Function !== "undefined" && Function) === "function" ? _c : Object]),
        tslib_1.__metadata("design:returntype", Promise)
    ], TransferRenderer.prototype, "handleSearch", null);
    TransferRenderer = tslib_1.__decorate([
        Options_1.OptionsControl({
            type: 'transfer'
        })
    ], TransferRenderer);
    return TransferRenderer;
}(react_1.default.Component));
exports.TransferRenderer = TransferRenderer;
//# sourceMappingURL=./renderers/Form/Transfer.js.map
