"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var react_1 = tslib_1.__importDefault(require("react"));
var classnames_1 = tslib_1.__importDefault(require("classnames"));
var Tree_1 = tslib_1.__importDefault(require("../../components/Tree"));
var Options_1 = require("./Options");
var helper_1 = require("../../utils/helper");
var types_1 = require("../../types");
var api_1 = require("../../utils/api");
var tpl_1 = require("../../utils/tpl");
var Checkboxes_1 = require("../../components/Checkboxes");
var TreeControl = /** @class */ (function (_super) {
    tslib_1.__extends(TreeControl, _super);
    function TreeControl() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.state = {
            isAddModalOpened: false,
            isEditModalOpened: false,
            parent: null,
            prev: null,
            data: null
        };
        return _this;
    }
    TreeControl.prototype.reload = function () {
        var reload = this.props.reloadOptions;
        reload && reload();
    };
    TreeControl.prototype.handleAdd = function (values) {
        this.saveRemote(values, 'add');
    };
    TreeControl.prototype.handleAddModalConfirm = function (values, action, ctx, components) {
        this.saveRemote(tslib_1.__assign(tslib_1.__assign({}, values), { parent: this.state.parent }), 'add');
        this.closeAddDialog();
    };
    TreeControl.prototype.handleEdit = function (values) {
        this.saveRemote(values, 'edit');
    };
    TreeControl.prototype.handleEditModalConfirm = function (values, action, ctx, components) {
        this.saveRemote(tslib_1.__assign(tslib_1.__assign({}, values), { prev: this.state.prev }), 'edit');
        this.closeEditDialog();
    };
    TreeControl.prototype.saveRemote = function (item, type) {
        return tslib_1.__awaiter(this, void 0, void 0, function () {
            var _a, addApi, editApi, data, env, remote;
            return tslib_1.__generator(this, function (_b) {
                switch (_b.label) {
                    case 0:
                        _a = this.props, addApi = _a.addApi, editApi = _a.editApi, data = _a.data, env = _a.env;
                        remote = null;
                        if (!(type == 'add' && api_1.isEffectiveApi(addApi, helper_1.createObject(data, item)))) return [3 /*break*/, 2];
                        return [4 /*yield*/, env.fetcher(addApi, helper_1.createObject(data, item))];
                    case 1:
                        remote = _b.sent();
                        return [3 /*break*/, 4];
                    case 2:
                        if (!(type == 'edit' && api_1.isEffectiveApi(editApi, helper_1.createObject(data, item)))) return [3 /*break*/, 4];
                        return [4 /*yield*/, env.fetcher(editApi, helper_1.createObject(data, item))];
                    case 3:
                        remote = _b.sent();
                        _b.label = 4;
                    case 4:
                        if (remote && !remote.ok) {
                            env.notify('error', remote.msg || '保存失败');
                            return [2 /*return*/];
                        }
                        this.reload();
                        return [2 /*return*/];
                }
            });
        });
    };
    TreeControl.prototype.handleRemove = function (item) {
        return tslib_1.__awaiter(this, void 0, void 0, function () {
            var _a, deleteConfirmText, deleteApi, data, env, ctx, confirmed, result;
            return tslib_1.__generator(this, function (_b) {
                switch (_b.label) {
                    case 0:
                        _a = this.props, deleteConfirmText = _a.deleteConfirmText, deleteApi = _a.deleteApi, data = _a.data, env = _a.env;
                        ctx = helper_1.createObject(data, item);
                        if (!api_1.isEffectiveApi(deleteApi, ctx)) return [3 /*break*/, 3];
                        return [4 /*yield*/, env.confirm(deleteConfirmText ? tpl_1.filter(deleteConfirmText, ctx) : '确认要删除？')];
                    case 1:
                        confirmed = _b.sent();
                        if (!confirmed) {
                            return [2 /*return*/];
                        }
                        return [4 /*yield*/, env.fetcher(deleteApi, ctx)];
                    case 2:
                        result = _b.sent();
                        if (!result.ok) {
                            env.notify('error', result.msg || '删除失败');
                            return [2 /*return*/];
                        }
                        this.reload();
                        _b.label = 3;
                    case 3: return [2 /*return*/];
                }
            });
        });
    };
    TreeControl.prototype.openAddDialog = function (parent) {
        var data = this.props.data;
        this.setState({
            isAddModalOpened: true,
            data: helper_1.createObject(data, parent ? parent : {}),
            parent: parent
        });
    };
    TreeControl.prototype.closeAddDialog = function () {
        this.setState({
            isAddModalOpened: false,
            parent: null
        });
    };
    TreeControl.prototype.openEditDialog = function (prev) {
        var data = this.props.data;
        this.setState({
            isEditModalOpened: true,
            data: helper_1.createObject(data, prev),
            prev: prev
        });
    };
    TreeControl.prototype.closeEditDialog = function () {
        this.setState({
            isEditModalOpened: false,
            prev: null
        });
    };
    TreeControl.prototype.render = function () {
        var _a = this.props, className = _a.className, ns = _a.classPrefix, value = _a.value, onChange = _a.onChange, disabled = _a.disabled, joinValues = _a.joinValues, extractValue = _a.extractValue, delimiter = _a.delimiter, placeholder = _a.placeholder, options = _a.options, inline = _a.inline, multiple = _a.multiple, valueField = _a.valueField, initiallyOpen = _a.initiallyOpen, unfoldedLevel = _a.unfoldedLevel, withChildren = _a.withChildren, onlyChildren = _a.onlyChildren, loading = _a.loading, hideRoot = _a.hideRoot, rootLabel = _a.rootLabel, cascade = _a.cascade, rootValue = _a.rootValue, showIcon = _a.showIcon, showRadio = _a.showRadio, render = _a.render, addMode = _a.addMode, addApi = _a.addApi, addDialog = _a.addDialog, editMode = _a.editMode, editApi = _a.editApi, editDialog = _a.editDialog, deleteApi = _a.deleteApi;
        var data = this.state.data;
        return (react_1.default.createElement("div", { className: classnames_1.default(ns + "TreeControl", className) },
            loading ? (render('loading', {
                type: 'spinner'
            })) : (react_1.default.createElement(Tree_1.default, { classPrefix: ns, valueField: valueField, disabled: disabled, onChange: onChange, joinValues: joinValues, extractValue: extractValue, delimiter: delimiter, placeholder: placeholder, data: options, multiple: multiple, initiallyOpen: initiallyOpen, unfoldedLevel: unfoldedLevel, withChildren: withChildren, onlyChildren: onlyChildren, hideRoot: hideRoot, rootLabel: rootLabel, rootValue: rootValue, showIcon: showIcon, showRadio: showRadio, cascade: cascade, foldedField: "collapsed", value: value || '', nameField: "label", selfDisabledAffectChildren: false, addMode: addMode, addable: api_1.isEffectiveApi(addApi), onAdd: this.handleAdd, openAddDialog: this.openAddDialog, editMode: editMode, editable: api_1.isEffectiveApi(editApi), onEdit: this.handleEdit, openEditDialog: this.openEditDialog, onRemove: this.handleRemove, removable: api_1.isEffectiveApi(deleteApi) })),
            addMode && render('modal', tslib_1.__assign({ type: 'dialog' }, addDialog), {
                key: 'addModal',
                data: data,
                onConfirm: this.handleAddModalConfirm,
                onClose: this.closeAddDialog,
                show: this.state.isAddModalOpened
            }),
            editMode && render('modal', tslib_1.__assign({ type: 'dialog' }, editDialog), {
                key: 'editModal',
                data: data,
                onConfirm: this.handleEditModalConfirm,
                onClose: this.closeEditDialog,
                show: this.state.isEditModalOpened
            })));
    };
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
    TreeControl.defaultProps = {
        placeholder: '选项加载中...',
        multiple: false,
        hideRoot: false,
        rootLabel: '顶级',
        rootValue: '',
        showIcon: true
    };
    tslib_1.__decorate([
        helper_1.autobind,
        tslib_1.__metadata("design:type", Function),
        tslib_1.__metadata("design:paramtypes", [typeof (_a = typeof types_1.PlainObject !== "undefined" && types_1.PlainObject) === "function" ? _a : Object]),
        tslib_1.__metadata("design:returntype", void 0)
    ], TreeControl.prototype, "handleAdd", null);
    tslib_1.__decorate([
        helper_1.autobind,
        tslib_1.__metadata("design:type", Function),
        tslib_1.__metadata("design:paramtypes", [typeof (_b = typeof Array !== "undefined" && Array) === "function" ? _b : Object, typeof (_c = typeof types_1.Action !== "undefined" && types_1.Action) === "function" ? _c : Object, Object, typeof (_d = typeof Array !== "undefined" && Array) === "function" ? _d : Object]),
        tslib_1.__metadata("design:returntype", void 0)
    ], TreeControl.prototype, "handleAddModalConfirm", null);
    tslib_1.__decorate([
        helper_1.autobind,
        tslib_1.__metadata("design:type", Function),
        tslib_1.__metadata("design:paramtypes", [typeof (_e = typeof types_1.PlainObject !== "undefined" && types_1.PlainObject) === "function" ? _e : Object]),
        tslib_1.__metadata("design:returntype", void 0)
    ], TreeControl.prototype, "handleEdit", null);
    tslib_1.__decorate([
        helper_1.autobind,
        tslib_1.__metadata("design:type", Function),
        tslib_1.__metadata("design:paramtypes", [typeof (_f = typeof Array !== "undefined" && Array) === "function" ? _f : Object, typeof (_g = typeof types_1.Action !== "undefined" && types_1.Action) === "function" ? _g : Object, Object, typeof (_h = typeof Array !== "undefined" && Array) === "function" ? _h : Object]),
        tslib_1.__metadata("design:returntype", void 0)
    ], TreeControl.prototype, "handleEditModalConfirm", null);
    tslib_1.__decorate([
        helper_1.autobind,
        tslib_1.__metadata("design:type", Function),
        tslib_1.__metadata("design:paramtypes", [Object, String]),
        tslib_1.__metadata("design:returntype", Promise)
    ], TreeControl.prototype, "saveRemote", null);
    tslib_1.__decorate([
        helper_1.autobind,
        tslib_1.__metadata("design:type", Function),
        tslib_1.__metadata("design:paramtypes", [Object]),
        tslib_1.__metadata("design:returntype", Promise)
    ], TreeControl.prototype, "handleRemove", null);
    tslib_1.__decorate([
        helper_1.autobind,
        tslib_1.__metadata("design:type", Function),
        tslib_1.__metadata("design:paramtypes", [typeof (_j = typeof Checkboxes_1.Option !== "undefined" && Checkboxes_1.Option) === "function" ? _j : Object]),
        tslib_1.__metadata("design:returntype", void 0)
    ], TreeControl.prototype, "openAddDialog", null);
    tslib_1.__decorate([
        helper_1.autobind,
        tslib_1.__metadata("design:type", Function),
        tslib_1.__metadata("design:paramtypes", []),
        tslib_1.__metadata("design:returntype", void 0)
    ], TreeControl.prototype, "closeAddDialog", null);
    tslib_1.__decorate([
        helper_1.autobind,
        tslib_1.__metadata("design:type", Function),
        tslib_1.__metadata("design:paramtypes", [typeof (_k = typeof Checkboxes_1.Option !== "undefined" && Checkboxes_1.Option) === "function" ? _k : Object]),
        tslib_1.__metadata("design:returntype", void 0)
    ], TreeControl.prototype, "openEditDialog", null);
    tslib_1.__decorate([
        helper_1.autobind,
        tslib_1.__metadata("design:type", Function),
        tslib_1.__metadata("design:paramtypes", []),
        tslib_1.__metadata("design:returntype", void 0)
    ], TreeControl.prototype, "closeEditDialog", null);
    return TreeControl;
}(react_1.default.Component));
exports.default = TreeControl;
var TreeControlRenderer = /** @class */ (function (_super) {
    tslib_1.__extends(TreeControlRenderer, _super);
    function TreeControlRenderer() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    TreeControlRenderer = tslib_1.__decorate([
        Options_1.OptionsControl({
            type: 'tree'
        })
    ], TreeControlRenderer);
    return TreeControlRenderer;
}(TreeControl));
exports.TreeControlRenderer = TreeControlRenderer;
//# sourceMappingURL=./renderers/Form/Tree.js.map
