import { __decorate } from "tslib";
import { AuthServiceBase, LogUtil, Util } from '@ibizstudio/runtime';
import { Component } from 'vue-property-decorator';
import { AppFuncService, AppLayoutService, FooterItemsService } from '../../../app-service';
import { AppMenuService } from '../../../ctrl-service/app-menu-service';
import { VueLifeCycleProcessing } from '../../../decorators';
import { AppIndexViewBase } from '../app-common-view/app-indexview-base';
/**
 * 应用首页视图
 *
 * @export
 * @class AppStyle2IndexView
 * @extends {AppIndexViewBase}
 */
let AppStyle2IndexView = class AppStyle2IndexView extends AppIndexViewBase {
    constructor() {
        super(...arguments);
        /**
         * 建构权限服务对象
         *
         * @type {AuthService}
         * @memberof AppStyle2IndexView
         */
        this.authService = new AuthServiceBase({ $store: this.$store });
        /**
         * 底部项绘制服务
         *
         * @type {FooterItemsService}
         * @memberof AppStyle2IndexView
         */
        this.footerItemsService = new FooterItemsService();
        /**
         * 底部绘制
         *
         * @private
         * @memberof AppStyle2IndexView
         */
        this.footerRenders = [];
    }
    /**
     * 初始化应用首页视图实例
     *
     * @memberof AppStyle2IndexView
     */
    async viewModelInit() {
        await super.viewModelInit();
        this.service = new AppMenuService(this.menuInstance, this.context);
        await this.service.initServiceParam(this.context, this.menuInstance);
        this.appMenuModel = this.service.getAllMenuItems();
        this.left_exp = this.handleMenusResource('left_exp');
        this.bottom_exp = this.handleMenusResource('bottom_exp');
        this.top_menus = this.handleMenusResource('top_menus');
        this.user_menus = this.handleMenusResource('user_menus');
        this.registerFooterItems();
    }
    /**
     * 根据名称获取菜单组
     *
     * @param {string} name
     * @memberof AppStyle2IndexViewLayout
     */
    getMenuGroup(name) {
        return this.appMenuModel.find((item) => Object.is(item.name, name));
    }
    /**
     * 通过统一资源标识计算菜单
     *
     * @param {*} name 菜单标识
     * @memberof AppStyle2IndexViewLayout
     */
    handleMenusResource(name) {
        const inputMenus = this.getMenuGroup(name);
        if (inputMenus && inputMenus.getPSAppMenuItems) {
            this.computedEffectiveMenus(inputMenus.getPSAppMenuItems);
            // this.computeParentMenus(inputMenus.getPSAppMenuItems);
        }
        return inputMenus;
    }
    /**
     * 计算有效菜单项
     *
     * @param {*} inputMenus
     * @memberof AppStyle2IndexViewLayout
     */
    computedEffectiveMenus(inputMenus) {
        inputMenus.forEach((_item) => {
            var _a;
            if (!((_a = this.authService) === null || _a === void 0 ? void 0 : _a.getMenusPermission(_item))) {
                _item.hidden = true;
            }
            if (_item.getPSAppMenuItems && _item.getPSAppMenuItems.length > 0) {
                this.computedEffectiveMenus(_item.getPSAppMenuItems);
            }
        });
    }
    // /**
    //  * 计算父项菜单项是否隐藏
    //  *
    //  * @param {*} inputMenus
    //  * @memberof AppStyle2IndexViewLayout
    //  */
    // computeParentMenus(inputMenus: Array<any>) {
    //     if (inputMenus && inputMenus.length > 0) {
    //         inputMenus.forEach((item: any) => {
    //             if (item.hidden && item.getPSAppMenuItems && item.getPSAppMenuItems.length > 0) {
    //                 item.getPSAppMenuItems.map((singleItem: any) => {
    //                     if (!singleItem.hidden) {
    //                         item.hidden = false;
    //                     }
    //                     if (singleItem.getPSAppMenuItems && singleItem.getPSAppMenuItems.length > 0) {
    //                         this.computeParentMenus(singleItem.getPSAppMenuItems);
    //                     }
    //                 });
    //             }
    //         });
    //     }
    // }
    /**
     * 注册底部项
     *
     * @memberof AppStyle2IndexViewLayout
     */
    registerFooterItems() {
        const leftItems = this.getMenuGroup('footer_left');
        const centerItems = this.getMenuGroup('footer_center');
        const rightItems = this.getMenuGroup('footer_right');
        if (leftItems && leftItems.getPSAppMenuItems) {
            leftItems.getPSAppMenuItems.forEach((item) => {
                this.footerRenders.push(this.footerItemsService.registerLeftItem((h) => {
                    return (<div class='action-item' title={item.tooltip} on-click={() => this.click(item)}>
                                <menu-icon item={item}/>
                                {item.caption}
                                {/* todo 菜单多语言*/}
                                {/* this.$tl(item.captiontag,item.caption) */}
                            </div>);
                }));
            });
        }
        if (centerItems && centerItems.getPSAppMenuItems) {
            centerItems.getPSAppMenuItems.forEach((item) => {
                this.footerRenders.push(this.footerItemsService.registerCenterItem((h) => {
                    return (<div class='action-item' title={item.tooltip} on-click={() => this.click(item)}>
                                <menu-icon item={item}/>
                                {item.caption}
                                {/* todo 菜单多语言*/}
                                {/* this.$tl(item.captiontag,item.caption) */}
                            </div>);
                }));
            });
        }
        if (rightItems && rightItems.getPSAppMenuItems) {
            rightItems.getPSAppMenuItems.forEach((item) => {
                this.footerRenders.push(this.footerItemsService.registerRightItem((h) => {
                    return (<div class='action-item' title={item.tooltip} on-click={() => this.click(item)}>
                                <menu-icon item={item}/>
                                {item.caption}
                                {/* todo 菜单多语言*/}
                                {/* this.$tl(item.captiontag,item.caption) */}
                            </div>);
                }));
            });
        }
    }
    /**
     * 项点击触发界面行为
     *
     * @protected
     * @param {*} item
     * @memberof AppStyle2IndexViewLayout
     */
    click(item) {
        this.$store.commit('removeAllPage');
        this.navDataService.removeNavDataFrist();
        let tempContext = Util.deepCopy(this.context);
        if (item.getPSNavigateContexts) {
            const localContext = Util.formatNavParam(item.getPSNavigateContexts);
            Object.assign(tempContext, localContext);
        }
        if (item.getPSAppFunc) {
            const appFuncs = this.service.getAllFuncs();
            const appFunc = appFuncs.find((element) => {
                return element.appfunctag === item.getPSAppFunc.codeName;
            });
            if (appFunc) {
                AppFuncService.getInstance().executeApplication(appFunc, tempContext);
            }
        }
        else {
            LogUtil.warn('未指定应用功能');
        }
    }
    /**
     * 绘制左导航
     *
     * @memberof AppStyle2IndexView
     */
    renderLeftExp() {
        var _a, _b, _c, _d;
        return (((_a = this.left_exp) === null || _a === void 0 ? void 0 : _a.getPSAppMenuItems) ?
            <app-content-left-exp slot="leftExp" ref="leftExp" service={this.service} ctrlName={(_c = (_b = this.menuInstance) === null || _b === void 0 ? void 0 : _b.codeName) === null || _c === void 0 ? void 0 : _c.toLowerCase()} items={(_d = this.left_exp) === null || _d === void 0 ? void 0 : _d.getPSAppMenuItems} modelService={this.modelService} on-active-item-change={(activeItem) => { this.activeItem = activeItem; this.$forceUpdate(); }}/>
            : null);
    }
    /**
     * 绘制左导航菜单
     *
     * @memberof AppStyle2IndexView
     */
    renderLeftNavMenu() {
        var _a, _b, _c, _d;
        return (((_a = this.left_exp) === null || _a === void 0 ? void 0 : _a.getPSAppMenuItems) ?
            <app-content-left-nav-menu slot="leftNavMenu" ref="leftNavMenu" ctrlName={(_c = (_b = this.menuInstance) === null || _b === void 0 ? void 0 : _b.codeName) === null || _c === void 0 ? void 0 : _c.toLowerCase()} menus={(_d = this.left_exp) === null || _d === void 0 ? void 0 : _d.getPSAppMenuItems} modelService={this.modelService} on-menu-click={(item) => this.click(item)}/>
            : null);
    }
    /**
     * 绘制头部菜单
     *
     * @memberof AppStyle2IndexView
     */
    renderHeaderMenus() {
        var _a, _b, _c;
        return (<app-header-right-menus slot="headerMenus" ref="headerMenus" ctrlName={(_b = (_a = this.menuInstance) === null || _a === void 0 ? void 0 : _a.codeName) === null || _b === void 0 ? void 0 : _b.toLowerCase()} menus={(_c = this.top_menus) === null || _c === void 0 ? void 0 : _c.getPSAppMenuItems} modelService={this.modelService} on-menu-click={(item) => this.click(item)}/>);
    }
    /**
     * 绘制导航分页
     *
     * @memberof AppStyle2IndexView
     */
    renderTabPageExp() {
        return (<tab-page-exp-style2 slot="tabPageExp" ref="tabExp" modelService={this.modelService} activeItem={this.activeItem}></tab-page-exp-style2>);
    }
    /**
     * 绘制底部导航
     *
     * @memberof AppStyle2IndexView
     */
    renderBootomExp() {
        var _a, _b, _c, _d;
        return (((_a = this.bottom_exp) === null || _a === void 0 ? void 0 : _a.getPSAppMenuItems) ?
            <app-content-bottom-exp slot="bootomExp" ref="bootomExp" service={this.service} ctrlName={(_c = (_b = this.menuInstance) === null || _b === void 0 ? void 0 : _b.codeName) === null || _c === void 0 ? void 0 : _c.toLowerCase()} modelService={this.modelService} items={(_d = this.bottom_exp) === null || _d === void 0 ? void 0 : _d.getPSAppMenuItems}/>
            : null);
    }
    /**
     * 渲染视图主题内容(隐藏主体表单)
     *
     * @memberof IndexViewBase
     */
    renderMainContent() {
        var _a;
        let { targetCtrlName, targetCtrlParam, targetCtrlEvent } = this.computeTargetCtrlData(this.menuInstance);
        return this.$createElement(targetCtrlName, { props: targetCtrlParam, ref: (_a = this.menuInstance) === null || _a === void 0 ? void 0 : _a.name, on: targetCtrlEvent, style: { display: 'none' } });
    }
    /**
     * 应用首页视图渲染
     *
     * @memberof AppStyle2IndexView
     */
    render(h) {
        var _a, _b;
        if (!this.viewIsLoaded) {
            return null;
        }
        const targetViewLayoutComponent = AppLayoutService.getLayoutComponent(`${(_a = this.viewInstance) === null || _a === void 0 ? void 0 : _a.viewType}-${(_b = this.viewInstance) === null || _b === void 0 ? void 0 : _b.viewStyle}`);
        return h(targetViewLayoutComponent, {
            props: { viewInstance: this.viewInstance, model: this.model, modelService: this.modelService, viewparams: this.viewparams, context: this.context },
        }, [
            this.renderLeftExp(),
            this.renderLeftNavMenu(),
            this.renderHeaderMenus(),
            this.renderTabPageExp(),
            this.renderMainContent(),
            this.renderBootomExp(),
        ]);
    }
};
AppStyle2IndexView = __decorate([
    Component({}),
    VueLifeCycleProcessing()
], AppStyle2IndexView);
export { AppStyle2IndexView };
