import { DrawerController, DrawerContainer } from '@ibiz/drawer-vue';
import { AppServiceBase } from '@ibizstudio-ex/runtime';
import { isArray, notNilEmpty } from 'qx-util';
import { Subject } from 'rxjs';
/**
 * 模态
 *
 * @export
 * @class AppPopup
 */
export class AppPopup {
    /**
     * Creates an instance of AppPopup.
     * @memberof AppPopup
     */
    constructor() {
        /**
         * 飘窗实例
         *
         * @protected
         * @type {DrawerItem[]}
         * @memberof AppPopup
         */
        this.drawerList = [];
        this.initData();
    }
    /**
     * 初始化基础数据
     *
     * @memberof AppPopup
     */
    initData() {
        if (!this.store) {
            const appService = AppServiceBase.getInstance();
            this.store = appService.getAppStore();
            this.i18n = appService.getI18n();
            this.router = appService.getRouter();
        }
    }
    /**
     * 添加飘窗实例
     *
     * @protected
     * @memberof AppPopup
     */
    addDrawerItem(item) {
        this.drawerList.push(item);
        this.store.commit('pushDrawerList', item);
        DrawerController.exp.setItems(this.drawerList);
    }
    /**
     * 移除指定飘窗实例
     *
     * @protected
     * @memberof AppPopup
     */
    removeDrawerItem(id) {
        const i = this.drawerList.findIndex(item => item.id === id);
        this.drawerList.splice(i, 1);
        this.store.commit('removeDrawerList', i);
        DrawerController.exp.setItems(this.drawerList);
    }
    /**
     * 顶部抽屉模式打开视图
     *
     * @param {{ viewname: string, title: string, width?: number, height?: number }} view 视图
     * @param {*} [viewParam={}] 视图参数
     * @param {any[]} deResParameters 关系实体参数对象
     * @param {any[]} parameters 当前应用视图参数对象
     * @param {any[]} args 多项数据
     * @param {*} [data={}] 行为参数
     * @returns {Observable<any>}
     * @memberof AppTopDrawerContainer
     */
    openDrawer(view, dynamicProps = {}, staticProps = {}) {
        this.initData();
        const subject = new Subject();
        this.open(view, dynamicProps, staticProps).then((data) => {
            subject.next(data);
            subject.complete();
            subject.unsubscribe();
        });
        return subject.asObservable();
    }
    /**
     * 打开上飘窗
     *
     * @protected
     * @param {{ viewname: string; title: string; width?: number; height?: number; placement?: string }} view
     * @param {*} [dynamicProps={}]
     * @param {*} [staticProps={}]
     * @return {*}  {Promise<any>}
     */
    async open(view, dynamicProps = {}, staticProps = {}) {
        const obj = dynamicProps.navdatas[0];
        const srfmajortext = obj ? obj.srfmajortext : '';
        const viewParamData = dynamicProps.viewparam ? JSON.parse(dynamicProps.viewparam) : undefined;
        const id = (viewParamData === null || viewParamData === void 0 ? void 0 : viewParamData.srfparentkey) ? viewParamData.srfparentkey : undefined;
        // 计算展示层级
        this.store.commit('upZIndex');
        const zIndex = this.store.getters.getZIndex();
        // 基本输入参数补充
        Object.assign(staticProps, { viewDefaultUsage: false, noViewCaption: true });
        const caption = srfmajortext ? srfmajortext : view.title;
        const ref = await DrawerController.present({
            id,
            caption,
            overlayIndex: zIndex,
            component: DrawerContainer,
            componentProps: { store: this.store, i18n: this.i18n, router: this.router, propsData: { dynamicProps, staticProps } },
        });
        // 缓存打卡界面信息
        const self = {
            id,
            ref,
            caption,
        };
        const i = this.drawerList.findIndex(item => item.id === id);
        if (i === -1) {
            this.addDrawerItem(self);
            // 侦听关闭行为
            const result = await ref.onDidDismiss();
            this.removeDrawerItem(self.id);
            this.store.commit('downZIndex');
            return { ret: notNilEmpty(result) ? 'OK' : 'NONE', datas: isArray(result) ? result : [result] };
        }
    }
}
// 模态服务控制器实例
export const appPopup = new AppPopup();
