import { Util } from '@ibizstudio/runtime';
import { TreeExpBarControlBase } from '../../../widgets/tree-exp-bar-control-base';
/**
 * 树视图导航栏部件基类
 *
 * @export
 * @class AppTreeExpBarBase
 * @extends {TreeExpBarControlBase}
 */
export class AppTreeExpBarBase extends TreeExpBarControlBase {
    constructor() {
        super(...arguments);
        /**
         * 快速搜索提示
         *
         * @memberof AppTreeExpBarBase
         */
        this.placeholder = '';
    }
    /**
     * 部件模型数据初始化实例
     *
     * @memberof ExpBarControlBase
     */
    async ctrlModelInit(args) {
        await super.ctrlModelInit(args);
        this.initQuickSearchPlaceholader();
    }
    /**
     * 填充节点实体
     *
     * @memberof ExpBarControlBase
     */
    async fillNodesEntity() {
        var _a;
        const allTreeNodes = this.$xDataControl.getPSDETreeNodes() || [];
        if ((allTreeNodes === null || allTreeNodes === void 0 ? void 0 : allTreeNodes.length) > 0) {
            for (let index = 0; index < allTreeNodes.length; index++) {
                const appDataEntity = allTreeNodes[index].getPSAppDataEntity();
                if (appDataEntity) {
                    await ((_a = appDataEntity.fill) === null || _a === void 0 ? void 0 : _a.call(appDataEntity));
                }
            }
        }
    }
    /**
     * 绘制导航视图
     *
     * @memberof AppTreeExpBarBase
     */
    renderNavView() {
        if (this.pickupViewPanelInstance) {
            return this.renderPickupViewPanel();
        }
        else {
            return this.renderDefaultNavView();
        }
    }
    /**
     * 绘制选择视图面板
     *
     * @memberof AppTreeExpBarBase
     */
    renderPickupViewPanel() {
        var _a, _b, _c, _d, _e, _f;
        let { targetCtrlName, targetCtrlParam, targetCtrlEvent } = this.computeTargetCtrlData(this.pickupViewPanelInstance);
        if (!this.cacheUUID) {
            this.cacheUUID = Util.createUUID();
        }
        Object.assign(targetCtrlParam.dynamicProps, {
            selectedData: (_a = this.dynamicProps) === null || _a === void 0 ? void 0 : _a.selectedData,
            context: (_b = this.selection) === null || _b === void 0 ? void 0 : _b.context,
            viewparams: (_c = this.selection) === null || _c === void 0 ? void 0 : _c.viewparam,
        });
        Object.assign(targetCtrlParam.staticProps, {
            isSingleSelect: (_d = this.staticProps) === null || _d === void 0 ? void 0 : _d.isSingleSelect,
            isShowButton: (_e = this.staticProps) === null || _e === void 0 ? void 0 : _e.isShowButton,
            viewMode: 1,
        });
        return this.$createElement(targetCtrlName, {
            key: this.cacheUUID,
            props: targetCtrlParam,
            ref: (_f = this.pickupViewPanelInstance) === null || _f === void 0 ? void 0 : _f.name,
            on: targetCtrlEvent,
        });
    }
    /**
     * 绘制默认导航视图
     *
     * @memberof AppTreeExpBarBase
     */
    renderDefaultNavView() {
        var _a, _b;
        if ((_b = (_a = this.selection) === null || _a === void 0 ? void 0 : _a.view) === null || _b === void 0 ? void 0 : _b.viewname) {
            // 如果不是在拖拽状态获取新的UUID,如果在拖拽状态则使用拖拽前的UUID，防止拖拽刷新
            if (!this.cacheUUID || this.cacheUUID.indexOf(this.selection.context.viewpath) == -1) {
                this.cacheUUID = this.selection.context.viewpath + Util.createUUID();
            }
            return this.$createElement(this.selection.view.viewname, {
                key: this.cacheUUID,
                props: {
                    staticProps: {
                        viewDefaultUsage: false,
                        viewUseByExpBar: true,
                    },
                    dynamicProps: {
                        viewdata: JSON.stringify(this.selection.context),
                        viewparam: JSON.stringify(this.selection.viewparam),
                    },
                },
                class: 'viewcontainer2',
                on: {
                    viewdataschange: this.onViewDatasChange.bind(this),
                    drdatasaved: this.onDrViewDatasChange.bind(this),
                    drdatasremove: this.onDrViewDatasChange.bind(this),
                    viewdatasactivated: this.viewDatasActivated.bind(this),
                    viewload: this.onViewLoad.bind(this),
                },
            });
        }
    }
    /**
     * 绘制标题栏
     *
     * @returns {*}
     * @memberof AppTreeExpBarBase
     */
    renderTitleBar() {
        var _a, _b;
        const title = this.$tl((_a = this.controlInstance.getTitlePSLanguageRes()) === null || _a === void 0 ? void 0 : _a.lanResTag, (_b = this.controlInstance) === null || _b === void 0 ? void 0 : _b.title);
        return (<div class='tree-exp-bar-header'>
        <div class='tree-exp-bar-title'>
          <icon type='ios-home-outline'/>
          <span>{title}</span>
        </div>
      </div>);
    }
    /**
     * 初始化快速搜索提示
     *
     * @memberof AppTreeExpBarBase
     */
    async initQuickSearchPlaceholader() {
        await this.fillNodesEntity();
        let placeholder = '';
        const allTreeNodes = this.$xDataControl.getPSDETreeNodes() || [];
        let placeholders = [];
        if ((allTreeNodes === null || allTreeNodes === void 0 ? void 0 : allTreeNodes.length) > 0) {
            allTreeNodes.forEach((node) => {
                var _a;
                if (Object.is(node.treeNodeType, 'DE') && node.enableQuickSearch) {
                    const quickSearchFields = ((_a = node.getPSAppDataEntity()) === null || _a === void 0 ? void 0 : _a.getQuickSearchPSAppDEFields()) || [];
                    if (quickSearchFields.length > 0) {
                        quickSearchFields.forEach((field) => {
                            var _a, _b;
                            const _field = (_a = node.getPSAppDataEntity()) === null || _a === void 0 ? void 0 : _a.findPSAppDEField(field.codeName);
                            if (_field) {
                                placeholders.push(this.$tl((_b = _field.getLNPSLanguageRes()) === null || _b === void 0 ? void 0 : _b.lanResTag, _field.logicName));
                            }
                        });
                    }
                }
            });
        }
        placeholders = [...new Set(placeholders)];
        placeholders.forEach((_placeholder, index) => {
            placeholder += _placeholder + (index == placeholders.length - 1 ? '' : '，');
        });
        this.placeholder = placeholder;
    }
    /**
     * 绘制快速搜索
     *
     * @memberof AppTreeExpBarBase
     */
    renderSearch() {
        return (<div class='search-container'>
        <i-input search={true} on-on-change={($event) => {
                this.searchText = $event.target.value.trim();
            }} placeholder={this.placeholder} on-on-search={() => this.onSearch()}></i-input>
      </div>);
    }
    /**
     * 绘制树视图导航栏
     *
     * @returns {*}
     * @memberof AppTreeExpBarBase
     */
    render() {
        var _a, _b, _c, _d, _e;
        if (!this.controlIsLoaded) {
            return null;
        }
        const showTitleBar = (_a = this.controlInstance) === null || _a === void 0 ? void 0 : _a.showTitleBar;
        const otherClassNames = {
            'tree-exp-content': showTitleBar ? true : false,
            'tree-exp-content2': !showTitleBar ? true : false,
            'treeview-exp-bar-content': false,
            'treeview-exp-bar-content2': false,
        };
        return (<div ref='exp-bar-container' style="width: 100%; height: 100%" class="exp-bar-container">
        <split ref={`${this.appDeCodeName}-${(_b = this.controlInstance.codeName) === null || _b === void 0 ? void 0 : _b.toLowerCase()}`} id={(_d = (_c = this.controlInstance) === null || _c === void 0 ? void 0 : _c.codeName) === null || _d === void 0 ? void 0 : _d.toLowerCase()} class={['expbarcontrol', `app-tree-exp-bar`, (_e = this.renderOptions) === null || _e === void 0 ? void 0 : _e.controlClassNames]} v-model={this.split} mode={this.sideBarlayout == 'LEFT' ? 'horizontal' : 'vertical'} on-on-move-end={this.onSplitChange.bind(this)}>
          {this.renderContent(otherClassNames)}
        </split>
      </div>);
    }
}
