import { ModelTool, Util } from '@ibizstudio/runtime';
import { ExpBarControlBase } from './expbar-control-base';
/**
 * 树视图导航栏部件基类
 *
 * @export
 * @class TreeExpBarControlBase
 * @extends {ExpBarControlBase}
 */
export class TreeExpBarControlBase extends ExpBarControlBase {
    constructor() {
        super(...arguments);
        /**
         * 刷新标识
         *
         * @public
         * @type {number}
         * @memberof TreeExpBarControlBase
         */
        this.counter = 0;
        /**
         * 分割宽度
         *
         * @public
         * @type {number}
         * @memberof TreeExpBarControlBase
         */
        this.split = 0.15;
    }
    /**
     * 监听部件静态参数变化
     *
     * @public
     * @type {number}
     * @memberof TreeExpBarControlBase
     */
    onStaticPropsChange(newVal, oldVal) {
        if (newVal.pickupviewpanel) {
            this.pickupViewPanelInstance = newVal.pickupviewpanel;
        }
        super.onStaticPropsChange(newVal, oldVal);
    }
    /**
    * 处理数据部件参数
    *
    * @memberof ExpBarControlBase
    */
    async handleXDataCtrlOptions() {
        await super.handleXDataCtrlOptions();
    }
    /**
     * 获取关系项视图
     *
     * @param {*} [arg={}] 额外参数
     * @returns {*}
     * @memberof TreeExpBarControlBase
     */
    async getExpItemView(arg = {}) {
        var _a, _b, _c, _d, _e, _f;
        let expmode = 'EXPITEM:' + arg.nodetype.toUpperCase();
        if ((_a = this.controlInstance) === null || _a === void 0 ? void 0 : _a.getPSAppViewRefs()) {
            for (let viewRef of ((_b = this.controlInstance) === null || _b === void 0 ? void 0 : _b.getPSAppViewRefs())) {
                if (Object.is(expmode, viewRef.name.toUpperCase())) {
                    let view = {
                        viewModelData: viewRef.getRefPSAppView(),
                        parentdata: viewRef.parentDataJO || {},
                        deKeyField: (_e = (_d = (_c = viewRef.getRefPSAppView()) === null || _c === void 0 ? void 0 : _c.getPSAppDataEntity()) === null || _d === void 0 ? void 0 : _d.codeName) === null || _e === void 0 ? void 0 : _e.toLowerCase(),
                    };
                    await ((_f = view.viewModelData) === null || _f === void 0 ? void 0 : _f.fill());
                    return view;
                }
            }
        }
        return null;
    }
    /**
     * 部件模型数据初始化实例
     *
     * @memberof ExpBarControlBase
     */
    async ctrlModelInit(args) {
        await super.ctrlModelInit(args);
        this.xDataControlName = this.controlInstance.xDataControlName;
        this.$xDataControl = ModelTool.findPSControlByName(this.xDataControlName, this.controlInstance.getPSControls());
    }
    /**
     * 树导航选中
     *
     * @param {any []} args 选中数据
     * @param {string} [tag]
     * @param {*} [$event2]
     * @returns {void}
     * @memberof TreeExpBarControlBase
     */
    async onSelectionChange(args, tag, $event2) {
        if (args.length === 0) {
            this.calcToolbarItemState(true);
            return;
        }
        const arg = args[0];
        if (!arg.id) {
            this.calcToolbarItemState(true);
            return;
        }
        const nodetype = arg.id.split(';')[0];
        const refview = await this.getExpItemView({ nodetype: nodetype });
        if (this.pickupViewPanelInstance) {
            this.onSelectionChangeByPickupViewPanel(arg);
        }
        else {
            if (!refview) {
                this.calcToolbarItemState(true);
                return;
            }
            let { tempContext, tempViewparam } = this.computeNavParams(arg);
            this.selection = {};
            Object.assign(tempContext, { viewpath: refview.viewModelData.modelPath });
            Object.assign(this.selection, { view: { viewname: 'app-view-shell' }, context: tempContext, viewparam: tempViewparam });
            this.calcToolbarItemState(false);
            this.$forceUpdate();
        }
    }
    /**
     * 树导航选中（选择视图面板处理逻辑）
     *
     * @param arg 选中数据
     * @memberof TreeExpBarControlBase
     */
    onSelectionChangeByPickupViewPanel(arg) {
        this.selection = {};
        let { tempContext, tempViewparam } = this.computeNavParams(arg);
        Object.assign(this.selection, { context: tempContext, viewparam: tempViewparam });
        this.calcToolbarItemState(false);
        this.$nextTick(() => {
            var _a;
            this.viewState.next({ tag: (_a = this.pickupViewPanelInstance) === null || _a === void 0 ? void 0 : _a.name, action: 'load', data: tempViewparam });
        });
    }
    /**
     * 计算导航参数
     *
     * @param arg  选中数据
     * @memberof TreeExpBarControlBase
     */
    computeNavParams(arg) {
        let tempContext = {};
        let tempViewparam = {};
        if (arg && arg.navfilter) {
            this.counter += 1;
            Object.defineProperty(tempViewparam, arg.navfilter, {
                value: arg.srfkey,
                writable: true,
                enumerable: true,
                configurable: true
            });
            Object.assign(tempContext, { srfcounter: this.counter });
        }
        Object.assign(tempContext, Util.deepCopy(this.context));
        if (arg.srfappctx) {
            Object.assign(tempContext, Util.deepCopy(arg.srfappctx));
        }
        if (arg.srfparentdename) {
            Object.assign(tempContext, { srfparentdename: arg.srfparentdename });
        }
        if (arg.srfparentdemapname) {
            Object.assign(tempContext, { srfparentdemapname: arg.srfparentdemapname });
        }
        if (arg.srfparentkey) {
            Object.assign(tempContext, { srfparentkey: arg.srfparentkey });
        }
        // 计算导航上下文
        if (arg && arg.navigateContext && Object.keys(arg.navigateContext).length > 0) {
            let tempData = arg.curData ? arg.curData : {};
            Object.assign(tempData, arg);
            let _context = this.$util.computedNavData(tempData, tempContext, tempViewparam, arg.navigateContext);
            Object.assign(tempContext, _context);
        }
        // 计算导航参数
        if (arg && arg.navigateParams && Object.keys(arg.navigateParams).length > 0) {
            let tempData = arg.curData ? arg.curData : {};
            Object.assign(tempData, arg);
            let _params = this.$util.computedNavData(tempData, tempContext, tempViewparam, arg.navigateParams);
            Object.assign(tempViewparam, _params);
            this.counter += 1;
            Object.assign(tempContext, { srfcounter: this.counter });
        }
        return { tempContext, tempViewparam };
    }
    /**
     * 计算目标部件所需参数
     *
     * @param {string} [controlInstance] 部件模型类型
     * @returns
     * @memberof TreeExpBarControlBase
     */
    computeTargetCtrlData(controlInstance) {
        const { targetCtrlName, targetCtrlParam, targetCtrlEvent } = super.computeTargetCtrlData(controlInstance);
        Object.assign(targetCtrlParam.staticProps, {
            isBranchAvailable: true
        });
        return { targetCtrlName: targetCtrlName, targetCtrlParam: targetCtrlParam, targetCtrlEvent: targetCtrlEvent };
    }
    /**
     * 执行搜索
     *
     * @memberof TreeExpBarControlBase
     */
    onSearch() {
        if (this.Environment && this.Environment.isPreviewMode) {
            return;
        }
        if (this.viewState && this.$xDataControl) {
            this.viewState.next({ tag: this.xDataControlName, action: 'filter', data: { srfnodefilter: this.searchText } });
        }
    }
    /**
     * 视图数据变化
     *
     * @param {*} $event
     * @memberof TreeExpBarControlBase
     */
    onViewDatasChange($event) {
        var _a;
        this.$emit("ctrl-event", { controlname: (_a = this.controlInstance) === null || _a === void 0 ? void 0 : _a.name, action: "selectionchange", data: $event });
    }
    /**
     * 视图数据变化
     *
     * @param {*} $event 事件数据
     * @memberof TreeExpBarControlBase
     */
    onDrViewDatasChange($event) {
        if (this.$xDataControl) {
            this.viewState.next({ tag: this.xDataControlName, action: 'refresh_parent' });
        }
    }
    /**
     * 视图数据被激活
     *
     * @param {*} $event 事件数据
     * @memberof TreeExpBarControlBase
     */
    viewDatasActivated($event) {
        this.$emit("ctrl-event", { controlname: this.controlInstance.name, action: "activated", data: $event });
    }
    /**
     * 视图数据加载完成
     *
     * @param {*} $event 事件数据
     * @memberof TreeExpBarControlBase
     */
    onViewLoad($event) {
        this.$emit("ctrl-event", { controlname: this.controlInstance.name, action: "load", data: $event });
    }
    /**
     * 部件事件处理
     *
     * @param {*} controlname 部件名称
     * @param {*} action 事件
     * @param {*} data 数据
     * @memberof TreeExpBarControlBase
     */
    onCtrlEvent(controlname, action, data) {
        //  选择视图面板逻辑特殊处理
        if (this.pickupViewPanelInstance && controlname == this.pickupViewPanelInstance.name) {
            this.ctrlEvent({ controlname, action, data });
        }
        else {
            super.onCtrlEvent(controlname, action, data);
        }
    }
}
