import { __decorate } from "tslib";
import { Provide } from 'vue-property-decorator';
import { isArray } from 'qx-util';
import { LogUtil, Util, ViewTool } from '@ibizstudio/runtime';
import { AppTreeService } from '../ctrl-service';
import { MDControlBase } from './md-control-base';
import { GlobalService, UIServiceRegister } from '@ibizstudio/runtime';
import { AppViewLogicService } from '../app-service';
/**
 * 树视图部件基类
 *
 * @export
 * @class TreeControlBase
 * @extends {MDControlBase}
 */
export class TreeControlBase extends MDControlBase {
    constructor() {
        super(...arguments);
        /**
         * 初始化完成
         *
         * @type {boolean}
         * @memberof TreeControlBase
         */
        this.inited = false;
        /**
         * 枝干节点是否可用（具有数据能力，可抛出）
         *
         * @type {boolean}
         * @memberof TreeControlBase
         */
        this.isBranchAvailable = true;
        /**
         * 是否开启树拖拽节点功能
         *
         * @type {boolean}
         * @memberof TreeControlBase
         */
        this.draggable = true;
        /**
         * 已选中数据集合
         *
         * @type {*}
         * @memberof TreeControlBase
         */
        this.selectedNodes = [];
        /**
         * 当前选中数据项
         *
         * @type {*}
         * @memberof TreeControlBase
         */
        this.currentselectedNode = {};
        /**
         * 缓存拖拽节点界面行为的结果集
         *
         * @type {*}
         * @memberof TreeControlBase
         */
        this.cacheDragNodeMap = new Map();
        /**
         * 数据展开主键
         *
         * @type {string[]}
         * @memberof TreeControlBase
         */
        this.expandedKeys = [];
        /**
         * 回显选中数据集合
         *
         * @type {*}
         * @memberof TreeControlBase
         */
        this.echoselectedNodes = [];
        /**
         * 过滤属性
         *
         * @type {string}
         * @memberof TreeControlBase
         */
        this.srfnodefilter = '';
        /**
         * 节点刷新模式 ['CURRENT'：当前节点，'PARENT'：父节点，'ALL'：全部]
         *
         * @type {'CURRENT' | 'PARENT' | 'ALL'}
         * @memberof TreeControlBase
         */
        this.refreshMode = 'CURRENT';
    }
    /**
     * 监听动态参数变化
     *
     * @param {*} newVal
     * @param {*} oldVal
     * @memberof TreeControlBase
     */
    onDynamicPropsChange(newVal, oldVal) {
        super.onDynamicPropsChange(newVal, oldVal);
        if ((newVal === null || newVal === void 0 ? void 0 : newVal.selectedData) && newVal.selectedData != (oldVal === null || oldVal === void 0 ? void 0 : oldVal.selectedData)) {
            this.selectedData = newVal.selectedData;
            this.onSelectedDataValueChange(newVal.selectedData);
        }
    }
    /**
     * 监听静态参数变化
     *
     * @param {*} newVal
     * @param {*} oldVal
     * @memberof TreeControlBase
     */
    onStaticPropsChange(newVal, oldVal) {
        this.isBranchAvailable = (newVal === null || newVal === void 0 ? void 0 : newVal.isBranchAvailable) !== false;
        this.isSingleSelect = newVal.isSingleSelect;
        super.onStaticPropsChange(newVal, oldVal);
    }
    /**
     * 部件模型数据初始化
     *
     * @memberof TreeControlBase
     */
    async ctrlModelInit(args) {
        await super.ctrlModelInit();
        this.service = new AppTreeService(this.controlInstance, this.context);
        this.initActionModel();
    }
    /**
     * 初始化树节点上下文菜单集合
     *
     * @memberof TreeControlBase
     */
    initActionModel() {
        const allTreeNodes = this.controlInstance.getPSDETreeNodes() || [];
        let tempModel = {};
        if ((allTreeNodes === null || allTreeNodes === void 0 ? void 0 : allTreeNodes.length) > 0) {
            allTreeNodes.forEach((item) => {
                var _a;
                if (item === null || item === void 0 ? void 0 : item.getPSDEContextMenu()) {
                    let toobarItems = (_a = item.getPSDEContextMenu()) === null || _a === void 0 ? void 0 : _a.getPSDEToolbarItems();
                    if (toobarItems.length > 0) {
                        toobarItems.forEach((toolbarItem) => {
                            this.initActionModelItem(toolbarItem, item, tempModel);
                        });
                    }
                }
            });
        }
        this.actionModel = {};
        Object.assign(this.actionModel, tempModel);
    }
    /**
     * 初始化上下菜单项
     *
     * @param toolbarItem 工具栏菜单模型
     * @param item 节点模型
     * @param tempModel 界面行为模型对象
     * @memberof TreeControlBase
     */
    initActionModelItem(toolbarItem, item, tempModel) {
        var _a, _b, _c;
        let tempItem = {
            name: toolbarItem.name,
            ctrlname: (_a = item.getPSDEContextMenu()) === null || _a === void 0 ? void 0 : _a.name,
            nodeOwner: item.nodeType
        };
        if (toolbarItem.itemType == 'DEUIACTION') {
            const uiAction = toolbarItem.getPSUIAction();
            if (uiAction) {
                tempItem.type = uiAction.uIActionType;
                tempItem.tag = uiAction.uIActionTag;
                tempItem.visabled = true;
                tempItem.disabled = false;
                if ((uiAction === null || uiAction === void 0 ? void 0 : uiAction.actionTarget) && (uiAction === null || uiAction === void 0 ? void 0 : uiAction.actionTarget) != "") {
                    tempItem.actiontarget = uiAction.actionTarget;
                }
                if (uiAction.noPrivDisplayMode) {
                    tempItem.noprivdisplaymode = uiAction.noPrivDisplayMode;
                }
                if (uiAction.dataAccessAction) {
                    tempItem.dataaccaction = uiAction.dataAccessAction;
                }
            }
        }
        tempItem.imgclass = toolbarItem.showIcon && toolbarItem.getPSSysImage() ? (_b = toolbarItem.getPSSysImage()) === null || _b === void 0 ? void 0 : _b.cssClass : '';
        tempItem.caption = toolbarItem.showCaption ? toolbarItem.caption : '';
        tempItem.title = toolbarItem.tooltip;
        tempModel[`${item.nodeType}_${toolbarItem.name}`] = tempItem;
        const toolbarItems = ((_c = toolbarItem === null || toolbarItem === void 0 ? void 0 : toolbarItem.getPSDEToolbarItems) === null || _c === void 0 ? void 0 : _c.call(toolbarItem)) || [];
        if ((toolbarItems === null || toolbarItems === void 0 ? void 0 : toolbarItems.length) > 0) {
            for (let toolBarChild of toolbarItems) {
                this.initActionModelItem(toolBarChild, item, tempModel);
            }
        }
    }
    /**
     * 树视图部件初始化
     *
     * @memberof TreeControlBase
     */
    ctrlInit() {
        super.ctrlInit();
    }
    viewStateAction(tag, action, data) {
        if (!Object.is(tag, this.name)) {
            return;
        }
        super.viewStateAction(tag, action, data);
        if (Object.is('load', action)) {
            this.inited = false;
            this.$nextTick(() => {
                this.inited = true;
            });
        }
        if (Object.is('filter', action)) {
            this.srfnodefilter = data.srfnodefilter;
            this.refresh_all();
        }
        if (Object.is('refresh_parent', action)) {
            this.refresh_parent();
        }
        if (Object.is('refresh_current', action)) {
            this.refresh_current();
        }
    }
    /**
     * 获取多项数据
     *
     * @returns {any[]}
     * @memberof TreeControlBase
     */
    getDatas() {
        return this.selectedNodes.map((item) => {
            return item.curData;
        });
    }
    /**
     * 获取单项数据
     *
     * @returns {*}
     * @memberof TreeControlBase
     */
    getData() {
        var _a, _b;
        return (_b = (_a = this.selectedNodes) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.curData;
    }
    /**
     * 数据加载
     *
     * @param {*} [node={}] 节点数据
     * @param {*} [resolve] 渲染树节点回调
     * @return {*}
     * @memberof TreeControlBase
     */
    load(node = {}, resolve) {
        var _a;
        if (node.data && node.data.children) {
            resolve(node.data.children);
            return;
        }
        let tempViewParams = Util.deepCopy(this.viewparams);
        let curNode = {};
        curNode = Util.deepObjectMerge(curNode, node);
        const params = {
            srfnodeid: node.data && node.data.id ? node.data.id : '#',
            srfnodefilter: this.srfnodefilter,
            parentData: (_a = curNode.data) === null || _a === void 0 ? void 0 : _a.curData
        };
        let tempContext = this.computecurNodeContext(curNode);
        if (curNode.data && curNode.data.srfparentdename) {
            Object.assign(tempContext, { srfparentdename: curNode.data.srfparentdename });
            Object.assign(tempViewParams, { srfparentdename: curNode.data.srfparentdename });
        }
        if (curNode.data && curNode.data.srfparentdemapname) {
            Object.assign(tempContext, { srfparentdemapname: curNode.data.srfparentdemapname });
            Object.assign(tempViewParams, { srfparentdemapname: curNode.data.srfparentdemapname });
        }
        if (curNode.data && curNode.data.srfparentkey) {
            Object.assign(tempContext, { srfparentkey: curNode.data.srfparentkey });
            Object.assign(tempViewParams, { srfparentkey: curNode.data.srfparentkey });
        }
        Object.assign(params, { viewparams: tempViewParams });
        this.onControlRequset('load', tempContext, params);
        this.service
            .getNodes(tempContext, params)
            .then((response) => {
            this.onControlResponse('load', response);
            if (!response || response.status !== 200) {
                this.$throw(response.info, 'load');
                resolve([]);
                return;
            }
            const _items = response.data;
            this.formatExpanded(_items);
            this.formatAppendCaption(_items);
            resolve([..._items]);
            let isRoot = Object.is(node.level, 0);
            let isSelectedAll = node.checked;
            this.setDefaultSelection(_items, isRoot, isSelectedAll);
            this.ctrlEvent({
                controlname: this.name,
                action: 'load',
                data: _items.filter((item) => item.enablecheck)
            });
        })
            .catch((response) => {
            this.onControlResponse('load', response);
            resolve([]);
            this.$throw(response, 'load');
        });
    }
    /**
     * 节点复选框选中事件
     *
     * @public
     * @param {*} data 当前节点对应传入对象
     * @param {*} checkedState 树目前选中状态对象
     * @memberof TreeControlBase
     */
    onCheck(data, checkedState) {
        // 处理多选数据
        if (!this.isSingleSelect) {
            // let leafNodes = checkedState.checkedNodes.filter((item: any) => item.leaf);
            const checkedNodes = Util.deepCopy(checkedState.checkedNodes);
            this.selectedNodes = checkedNodes.filter((checkedNode) => checkedNode.enablecheck);
            this.ctrlEvent({
                controlname: this.name,
                action: 'selectionchange',
                data: this.selectedNodes,
            });
        }
    }
    /**
     * 当前选中节点变更事件
     *
     * @public
     * @param {*} data 节点对应传入对象
     * @param {*} node 节点对应node对象
     * @memberof TreeControlBase
     */
    selectionChange(data, node) {
        // 禁用项处理
        if (data.disabled) {
            node.isCurrent = false;
            return;
        }
        // 只处理最底层子节点
        if (this.isBranchAvailable || data.leaf) {
            //  修改之前节点的选中状态值
            if (this.currentselectedNode && Object.keys(this.currentselectedNode).length > 0) {
                this.currentselectedNode.srfchecked = 0;
            }
            //  添加选中状态值
            data.srfchecked = 1;
            this.currentselectedNode = data;
            // 单选直接替换
            if (this.isSingleSelect) {
                this.selectedNodes = [this.currentselectedNode];
                this.ctrlEvent({
                    controlname: this.name,
                    action: 'selectionchange',
                    data: this.selectedNodes,
                });
            }
            // 多选用check方法
        }
    }
    /**
     * 刷新
     *
     * @param {*} [args] 额外参数
     * @memberof TreeControlBase
     */
    refresh(args) {
        if (args && isArray(args) && args.length > 0) {
            const refreshMode = args[0]['refreshMode'];
            switch (refreshMode) {
                case 1: //当前节点
                    this.refresh_current();
                    break;
                case 2: //父节点
                    this.refresh_parent();
                    break;
                case 3: //刷新全部
                    this.refresh_all();
                    break;
                default:
                    this.refresh_current();
                    break;
            }
        }
        else {
            this.refresh_current();
        }
    }
    /**
     * 刷新整个树
     *
     * @memberof TreeControlBase
     */
    refresh_all() {
        this.refreshMode = 'ALL';
        this.inited = false;
        this.$nextTick(() => {
            this.inited = true;
        });
    }
    /**
     * 刷新节点
     *
     * @public
     * @param {*} [curContext] 当前节点上下文
     * @param {*} [arg={}] 当前节点附加参数
     * @param {boolean} parentnode 是否是刷新父节点
     * @memberof TreeControlBase
     */
    refresh_node(curContext, arg = {}, parentnode) {
        const { srfnodeid: id } = arg;
        Object.assign(arg, { viewparams: this.viewparams });
        let tempContext = Util.deepCopy(curContext);
        this.onControlRequset('refresh_node', tempContext, arg);
        const get = this.service.getNodes(tempContext, arg);
        get.then((response) => {
            this.onControlResponse('refresh_node', response);
            if (!response || response.status !== 200) {
                this.$throw(response.info, 'refresh_node');
                return;
            }
            const _items = [...response.data];
            this.formatExpanded(_items);
            const tree = this.$refs[this.name];
            tree.updateKeyChildren(id, _items);
            if (parentnode) {
                this.currentselectedNode = {};
            }
            this.$forceUpdate();
            this.setDefaultSelection(_items);
        }).catch((response) => {
            this.onControlResponse('refresh_node', response);
            this.$throw(response, 'refresh_node');
        });
    }
    /**
     * 刷新当前节点
     *
     * @memberof TreeControlBase
     */
    refresh_current() {
        this.refreshMode = 'CURRENT';
        if (Object.keys(this.currentselectedNode).length === 0) {
            return;
        }
        const tree = this.$refs[this.name];
        const node = tree.getNode(this.currentselectedNode.id);
        if (!node || !node.parent) {
            return;
        }
        let curNode = {};
        curNode = Util.deepObjectMerge(curNode, node);
        let tempContext = {};
        if (curNode.data && curNode.data.srfappctx) {
            Object.assign(tempContext, curNode.data.srfappctx);
        }
        else {
            Object.assign(tempContext, this.context);
        }
        const id = node.key ? node.key : '#';
        const param = { srfnodeid: id };
        this.refresh_node(tempContext, param, false);
    }
    /**
     * 刷新当前节点的父节点
     *
     * @memberof TreeControlBase
     */
    refresh_parent() {
        this.refreshMode = 'PARENT';
        if (Object.keys(this.currentselectedNode).length === 0) {
            return;
        }
        const tree = this.$refs[this.name];
        const node = tree.getNode(this.currentselectedNode.id);
        if (!node || !node.parent) {
            return;
        }
        let curNode = {};
        const { parent: _parent } = node;
        curNode = Util.deepObjectMerge(curNode, _parent);
        let tempContext = {};
        if (curNode.data && curNode.data.srfappctx) {
            Object.assign(tempContext, curNode.data.srfappctx);
        }
        else {
            Object.assign(tempContext, this.context);
        }
        const id = _parent.key ? _parent.key : '#';
        const param = { srfnodeid: id };
        this.refresh_node(tempContext, param, true);
    }
    /**
     * 执行默认界面行为(树节点双击事件)
     *
     * @param {*} node 节点数据
     * @memberof TreeControlBase
     */
    doDefaultAction(node) {
        // todo 默认界面行为
        this.ctrlEvent({
            controlname: this.name,
            action: 'nodedblclick',
            data: this.selectedNodes,
        });
    }
    /**
     * 显示上下文菜单事件
     *
     * @param data 节点数据
     * @param event 事件源
     * @memberof TreeControlBase
     */
    showContext(data, event) {
        let _this = this;
        this.copyActionModel = {};
        const tags = data.id.split(';');
        Object.values(this.actionModel).forEach((item) => {
            if (Object.is(item.nodeOwner, tags[0])) {
                this.copyActionModel[item.name] = item;
            }
        });
        if (Object.keys(this.copyActionModel).length === 0) {
            return;
        }
        this.computeNodeState(data, data.nodeType, data.appEntityName).then((result) => {
            let flag = false;
            if (Object.values(result).length > 0) {
                flag = Object.values(result).some((item) => {
                    return item.visabled === true;
                });
            }
            if (flag) {
                _this.$refs[data.id].showContextMenu(event.clientX, event.clientY);
            }
        });
    }
    /**
     * 计算节点右键权限
     *
     * @param {*} node 节点数据
     * @param {*} nodeType 节点类型
     * @param {*} appEntityName 应用实体名称
     * @returns
     * @memberof TreeControlBase
     */
    async computeNodeState(node, nodeType, appEntityName) {
        if (Object.is(nodeType, 'STATIC')) {
            return this.copyActionModel;
        }
        let service = await new GlobalService().getService(appEntityName, this.context);
        if (this.copyActionModel && Object.keys(this.copyActionModel).length > 0) {
            if (service['Get'] && service['Get'] instanceof Function) {
                let tempContext = Util.deepCopy(this.context);
                tempContext[appEntityName.toLowerCase()] = node.srfkey;
                let targetData = await service.Get(tempContext, {}, false);
                const nodeUIService = await UIServiceRegister.getService(this.context, appEntityName.toLowerCase());
                if (nodeUIService) {
                    await nodeUIService.loaded();
                }
                ViewTool.calcTreeActionItemAuthState(targetData.data, this.copyActionModel, nodeUIService);
                return this.copyActionModel;
            }
            else {
                LogUtil.warn(this.$t('app.warn.geterror'));
                return this.copyActionModel;
            }
        }
    }
    /**
     * 部件事件
     * @param ctrl 部件
     * @param action  行为
     * @param data 数据
     *
     * @memberof TreeControlBase
     */
    onCtrlEvent(controlname, action, data, selectedNode) {
        var _a;
        if (action == 'contextMenuItemClick') {
            AppViewLogicService.getInstance().executeViewLogic(`${controlname}_${data}_click`, undefined, this, selectedNode.curData, ((_a = this.controlInstance) === null || _a === void 0 ? void 0 : _a.getPSAppViewLogics()) || []);
        }
        else {
            this.ctrlEvent({ controlname, action, data });
        }
    }
    /**
     * 自定义树节点筛选操作逻辑
     *
     * @param {*} value 过滤值
     * @param {*} data 节点值
     * @return {*}
     * @memberof TreeControlBase
     */
    filterNode(value, data) {
        if (!value)
            return true;
        return data.text.indexOf(value) !== -1;
    }
    /**
     * 计算当前节点的上下文
     *
     * @param {*} curNode 当前节点
     * @memberof TreeControlBase
     */
    computecurNodeContext(curNode) {
        let tempContext = {};
        if (curNode && curNode.data && curNode.data.srfappctx) {
            tempContext = Util.deepCopy(curNode.data.srfappctx);
        }
        else {
            tempContext = Util.deepCopy(this.context);
        }
        return tempContext;
    }
    /**
     * 设置默认展开节点
     *
     * @public
     * @param {any[]} items 节点集合
     * @returns {any[]}
     * @memberof TreeControlBase
     */
    formatExpanded(items) {
        const data = [];
        items.forEach(item => {
            if (item.expanded || (item.children && item.children.length > 0)) {
                this.expandedKeys.push(item.id);
            }
        });
        return data;
    }
    /**
     * 设置附加标题栏
     *
     * @public
     * @param {any[]} items 节点集合
     * @returns {any[]}
     * @memberof TreeControlBase
     */
    formatAppendCaption(items) {
        items.forEach(item => {
            if (item.appendCaption && item.textFormat) {
                item.text = item.textFormat + item.text;
            }
        });
    }
    /**
     * 设置默认选中,回显数项，选中所有子节点
     *
     * @param {any[]} items 当前节点所有子节点集合
     * @param {boolean} isRoot 是否是加载根节点
     * @param {boolean} isSelectedAll 是否选中所有子节点
     * @memberof TreeControlBase
     */
    setDefaultSelection(items, isRoot = false, isSelectedAll = false) {
        if (items.length == 0) {
            return;
        }
        let defaultData;
        //在导航视图中，如已有选中数据，则右侧展开已选中数据的视图，如无选中数据则默认选中第一条
        if (this.isSelectFirstDefault) {
            if (this.isSingleSelect) {
                let index = -1;
                if (this.selectedNodes && this.selectedNodes.length > 0) {
                    this.selectedNodes.forEach((select) => {
                        index = items.findIndex((item) => {
                            if (Util.isEmpty(item.srfkey)) {
                                return select.id == item.id;
                            }
                            else {
                                return select.srfkey == item.srfkey;
                            }
                        });
                    });
                }
                if (index === -1) {
                    if (isRoot) {
                        if (this.viewparams && this.viewparams.srfnavtag && items.length > 0) {
                            const activate = this.viewparams.srfnavtag;
                            index = items.findIndex((item) => {
                                return item.id && item.id.split(';') && (item.id.split(';')[0] == activate);
                            });
                            if (index === -1)
                                index = 0;
                        }
                        else {
                            index = 0;
                        }
                    }
                    else {
                        return;
                    }
                }
                defaultData = items[index];
                this.setTreeNodeHighLight(defaultData);
                this.currentselectedNode = Util.deepCopy(defaultData);
                if (this.isBranchAvailable || defaultData.leaf) {
                    this.selectedNodes = [this.currentselectedNode];
                    this.ctrlEvent({
                        controlname: this.name,
                        action: 'selectionchange',
                        data: this.selectedNodes,
                    });
                }
            }
        }
        // 已选数据的回显
        if (this.echoselectedNodes && this.echoselectedNodes.length > 0) {
            let checkedNodes = items.filter((item) => {
                return this.echoselectedNodes.some((val) => {
                    if (Object.is(item.srfkey, val.srfkey) && Object.is(item.srfmajortext, val.srfmajortext)) {
                        val.used = true;
                        this.selectedNodes.push(val);
                        this.ctrlEvent({
                            controlname: this.name,
                            action: 'selectionchange',
                            data: this.selectedNodes,
                        });
                        return true;
                    }
                });
            });
            if (checkedNodes.length > 0) {
                this.echoselectedNodes = this.echoselectedNodes.filter((item) => !item.used);
                // 父节点选中时，不需要执行这段，会选中所有子节点
                if (!isSelectedAll) {
                    if (this.isSingleSelect) {
                        this.setTreeNodeHighLight(checkedNodes[0]);
                        this.currentselectedNode = Util.deepCopy(checkedNodes[0]);
                        this.selectedNodes = [this.currentselectedNode];
                    }
                    else {
                        this.selectedNodes = this.selectedNodes.concat(checkedNodes);
                        const tree = this.$refs[this.name];
                        tree.setCheckedNodes(this.selectedNodes);
                    }
                }
            }
        }
        // 父节点选中时，选中所有子节点
        if (isSelectedAll) {
            let leafNodes = items.filter((item) => item.leaf);
            this.selectedNodes = this.selectedNodes.concat(leafNodes);
            this.ctrlEvent({
                controlname: this.name,
                action: 'selectionchange',
                data: this.selectedNodes,
            });
        }
    }
    /**
     * 设置选中高亮
     *
     * @param {*} data 节点数据
     * @memberof TreeControlBase
     */
    setTreeNodeHighLight(data) {
        const tree = this.$refs[this.name];
        tree.setCurrentKey(data.id);
    }
    /**
     * selectedData选中值变化
     *
     * @param {*} newVal
     * @memberof TreeControlBase
     */
    onSelectedDataValueChange(newVal) {
        this.echoselectedNodes = newVal ? this.isSingleSelect ? [JSON.parse(newVal)[0]] : JSON.parse(newVal) : [];
        this.selectedNodes = [];
        if (this.controlIsLoaded && this.echoselectedNodes.length > 0) {
            const { name } = this.controlInstance;
            let AllnodesObj = this.$refs[name].store.nodesMap;
            let AllnodesArray = [];
            for (const key in AllnodesObj) {
                if (AllnodesObj.hasOwnProperty(key)) {
                    AllnodesArray.push(AllnodesObj[key].data);
                }
            }
            this.setDefaultSelection(AllnodesArray);
        }
    }
    /**
     * 节点能否被拖拽
     *
     * @param node 拖拽节点
     * @returns 要拖拽的节点能否被拖拽
     */
    allowDrag(node) {
        return new Promise((resolve, reject) => {
            var _a, _b;
            if (((_a = node.data) === null || _a === void 0 ? void 0 : _a.allowDrag) || ((_b = node.data) === null || _b === void 0 ? void 0 : _b.allowOrder)) {
                this.draggingNode = Util.deepCopy(node);
                resolve(true);
            }
            else {
                resolve(false);
            }
        });
    }
    /**
     * 能否放入目标节点
     *
     * @param draggingNode 正在拖拽的节点
     * @param dropNode 目标节点
     * @param type 'prev'|'inner'|'next' 拖拽到相对目标节点的位置（前，插入，后）
     * @returns 拖拽的节点能否放置到目标节点
     * @memberof TreeControlBase
     */
    allowDrop(draggingNode, dropNode, type) {
        return new Promise((resolve, reject) => {
            var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
            if ((((_a = dropNode.data) === null || _a === void 0 ? void 0 : _a.allowDrop) || ((_b = dropNode.data) === null || _b === void 0 ? void 0 : _b.allowOrder)) && !Object.is('inner', type)) {
                if (this.ctrlTriggerLogicMap.get('calcnodeallowdrop')) {
                    const draggingNodeData = draggingNode.data;
                    const dropNodeData = dropNode.data;
                    if (this.cacheDragNodeMap && this.cacheDragNodeMap.get(((_c = draggingNode.data) === null || _c === void 0 ? void 0 : _c.id) + ((_d = dropNode.data) === null || _d === void 0 ? void 0 : _d.id))) {
                        const isAllowDrop = this.cacheDragNodeMap.get(draggingNode.data.id + dropNode.data.id) == 'true' ? true : false;
                        return resolve(isAllowDrop);
                    }
                    let arg = { draggingNode: draggingNodeData.curData, dropNode: dropNodeData.curData };
                    this.ctrlTriggerLogicMap.get('calcnodeallowdrop').executeUILogic({
                        context: dropNodeData.srfappctx,
                        viewparams: this.viewparams,
                        utils: this.viewCtx,
                        data: [arg],
                        event: null,
                        xData: this,
                        actionContext: this,
                        srfParentDeName: dropNodeData.srfparentdename,
                        arg: Object.assign(arg, { data: [arg] })
                    });
                    if (arg && arg.srfret) {
                        if ((_e = dropNode.data) === null || _e === void 0 ? void 0 : _e.allowDrop) {
                            this.cacheDragNodeMap.set(draggingNode.data.id + dropNode.data.id, 'true');
                            resolve(true);
                        }
                        else if (((_f = dropNode.data) === null || _f === void 0 ? void 0 : _f.allowOrder) && Object.is((_g = this.draggingNode.parent) === null || _g === void 0 ? void 0 : _g.id, (_h = dropNode.parent) === null || _h === void 0 ? void 0 : _h.id)) {
                            this.cacheDragNodeMap.set(draggingNode.data.id + dropNode.data.id, 'true');
                            resolve(true);
                        }
                        else {
                            this.cacheDragNodeMap.set(draggingNode.data.id + dropNode.data.id, 'false');
                            resolve(false);
                        }
                    }
                    else {
                        this.cacheDragNodeMap.set(draggingNode.data.id + dropNode.data.id, 'false');
                        resolve(false);
                    }
                }
                else {
                    if ((_j = dropNode.data) === null || _j === void 0 ? void 0 : _j.allowDrop) {
                        resolve(true);
                    }
                    else if (((_k = dropNode.data) === null || _k === void 0 ? void 0 : _k.allowOrder) && Object.is((_l = this.draggingNode.parent) === null || _l === void 0 ? void 0 : _l.id, (_m = dropNode.parent) === null || _m === void 0 ? void 0 : _m.id)) {
                        resolve(true);
                    }
                    else {
                        resolve(false);
                    }
                }
            }
            else {
                resolve(false);
            }
        });
    }
    /**
     * 树节点值变化
     *
     * @param value 变化值
     * @param node 节点数据
     * @param event 额外参数
     * @memberof TreeControlBase
     */
    nodeValueChange(value, node, event) {
        this.$set(node.data, 'text', value);
        this.$set(node.data, 'srfmajortext', value);
        if (node.data.curData && node.data.nodeTextField) {
            this.$set(node.data.curData, node.data.nodeTextField, value);
        }
    }
    /**
     * 保存并刷新
     *
     * @param node 节点
     * @param event 额外参数
     * @memberof TreeControlBase
     */
    async saveAndRefresh(node, event) {
        const nodeData = node.data;
        if (Object.is(nodeData.nodeType, 'STATIC')) {
            return;
        }
        let tempContext = this.computecurNodeContext(node);
        let service = await new GlobalService().getService(nodeData.appEntityName, this.context);
        let viewparams = Util.deepCopy(this.viewparams);
        this.onControlRequset('saveAndRefresh', tempContext, viewparams);
        this.service.update('Update', tempContext, nodeData.curData, service).then((response) => {
            this.onControlResponse('saveAndRefresh', response);
            if (!response.status || response.status !== 200) {
                this.$throw(response, 'update');
            }
            else {
                this.$success((nodeData.srfmajortext ? nodeData.srfmajortext : '') + '变更' + this.$t('app.commonwords.success'), 'update');
            }
            this.refreshEditNodeParent(node);
        }).catch((response) => {
            this.onControlResponse('saveAndRefresh', response);
            this.$throw(response, 'update');
            this.refreshEditNodeParent(node);
        });
    }
    /**
     * 刷新编辑节点的父节点
     *
     * @param editNode 编辑节点
     * @memberof TreeControlBase
     */
    refreshEditNodeParent(editNode) {
        let curNode = {};
        const { parent: _parent } = editNode;
        curNode = Util.deepObjectMerge(curNode, _parent);
        let tempContext = {};
        if (curNode.data && curNode.data.srfappctx) {
            Object.assign(tempContext, curNode.data.srfappctx);
        }
        else {
            Object.assign(tempContext, this.context);
        }
        const id = _parent.key ? _parent.key : '#';
        const param = { srfnodeid: id };
        this.refresh_node(tempContext, param, false);
    }
    /**
     * 开始加载
     *
     * @memberof TreeControlBase
     */
    ctrlBeginLoading() {
        if (Object.keys(this.currentselectedNode).length === 0) {
            super.ctrlBeginLoading();
        }
        else {
            const tree = this.$refs[this.name];
            if (tree) {
                const node = tree.getNode(this.currentselectedNode.id);
                if (!node || !node.parent) {
                    super.ctrlBeginLoading();
                }
                else {
                    this.ctrlLoadingService.beginLoading2(`.tree-node-id-${this.refreshMode == 'PARENT' ? node.parent.id : node.id}`);
                }
            }
            else {
                super.ctrlBeginLoading();
            }
        }
    }
}
__decorate([
    Provide()
], TreeControlBase.prototype, "expandedKeys", void 0);
