import { TreeViewEngine, ModelTool } from '@ibizstudio/runtime';
import { MDViewBase } from './mdview-base';
/**
 * 实体树视图视图基类
 *
 * @export
 * @class TreeViewBase
 * @extends {MDViewBase}
 * @implements {TreeViewInterface}
 */
export class TreeViewBase extends MDViewBase {
    constructor() {
        super(...arguments);
        /**
         * 视图引擎
         *
         * @public
         * @type {Engine}
         * @memberof TreeViewBase
         */
        this.engine = new TreeViewEngine();
    }
    /**
     * 引擎初始化
     *
     * @param {*} [opts={}] 引擎参数
     * @memberof TreeViewBase
     */
    engineInit(opts = {}) {
        var _a, _b, _c;
        if (this.Environment && this.Environment.isPreviewMode) {
            return;
        }
        if (this.engine && this.treeInstance) {
            let engineOpts = Object.assign({
                view: this,
                parentContainer: this.$parent,
                p2k: '0',
                isLoadDefault: this.viewInstance.loadDefault,
                keyPSDEField: this.appDeCodeName.toLowerCase(),
                majorPSDEField: this.appDeMajorFieldName.toLowerCase(),
                opendata: (args, fullargs, params, $event, xData) => {
                    this.opendata(args, fullargs, params, $event, xData);
                },
                newdata: (args, fullargs, params, $event, xData) => {
                    this.newdata(args, fullargs, params, $event, xData);
                },
                tree: this.$refs[this.treeInstance.name].ctrl,
            }, opts);
            if (((_a = this.searchFormInstance) === null || _a === void 0 ? void 0 : _a.name) && this.$refs[this.searchFormInstance.name]) {
                engineOpts.searchform = (this.$refs[this.searchFormInstance.name].ctrl);
            }
            else if (((_b = this.quickSearchFormInstance) === null || _b === void 0 ? void 0 : _b.name) && this.$refs[this.quickSearchFormInstance.name]) {
                engineOpts.searchform = (this.$refs[this.quickSearchFormInstance.name].ctrl);
            }
            if (((_c = this.searchBarInstance) === null || _c === void 0 ? void 0 : _c.name) && this.$refs[this.searchBarInstance.name]) {
                engineOpts.searchbar = (this.$refs[this.searchBarInstance.name].ctrl);
            }
            this.engine.init(engineOpts);
        }
    }
    /**
     * 初始化列表视图实例
     *
     * @memberof AppDefaultTreeView
     */
    async viewModelInit() {
        this.viewInstance = (this.staticProps.modeldata);
        await super.viewModelInit();
        this.treeInstance = ModelTool.findPSControlByName('tree', this.viewInstance.getPSControls());
    }
    /**
     * 渲染视图主体内容区
     *
     * @memberof AppDefaultEditView
     */
    renderMainContent() {
        let { targetCtrlName, targetCtrlParam, targetCtrlEvent } = this.computeTargetCtrlData(this.treeInstance);
        return this.$createElement(targetCtrlName, { slot: 'default', props: targetCtrlParam, ref: this.treeInstance.name, on: targetCtrlEvent });
    }
    /**
     * 快速搜索
     *
     * @returns {void}
     * @memberof TreeViewBase
     */
    onSearch() {
        if (!this.treeInstance || !this.viewState) {
            return;
        }
        this.viewState.next({ tag: this.treeInstance.name, action: 'filter', data: { srfnodefilter: this.query } });
    }
}
