import { Util } from '@ibizstudio/runtime';
import { ExpBarControlBase } from './expbar-control-base';
/**
 * 日历导航部件基类
 *
 *
 */
export class CalendarExpBarControlBase extends ExpBarControlBase {
    constructor() {
        super(...arguments);
        /**
         * 导航过滤项
         *
         * @type {*}
         * @memberof CalendarExpBarControlBase
         */
        this.navFilter = {};
        /**
         * 导航关系
         *
         * @type {*}
         * @memberof CalendarExpBarControlBase
         */
        this.navPSDer = {};
    }
    /**
     * 初始化日历导航部件实例
     *
     * @memberof CalendarExpBarControlBase
     */
    async ctrlModelInit() {
        await super.ctrlModelInit();
        this.initNavView();
    }
    /**
     * 初始化数据部件配置
     *
     * @memberof CalendarExpBarControlBase
     */
    async handleXDataCtrlOptions() {
        await super.handleXDataCtrlOptions();
    }
    /**
     * 初始化导航视图参数
     *
     * @memberof CalendarExpBarControlBase
     */
    initNavView() {
        var _a;
        const calendarItems = (_a = this.$xDataControl) === null || _a === void 0 ? void 0 : _a.getPSSysCalendarItems();
        let navViewName = {};
        let navParam = {};
        let navFilter = {};
        let navPSDer = {};
        if (calendarItems && calendarItems.length > 0) {
            calendarItems.forEach((item) => {
                var _a, _b;
                const viewName = {
                    [item.itemType]: item.getNavPSAppView() ? (_a = item.getNavPSAppView()) === null || _a === void 0 ? void 0 : _a.modelPath : "",
                };
                Object.assign(navViewName, viewName);
                const param = {
                    [item.itemType]: {
                        navigateContext: this.initNavParam(item.getPSNavigateContexts()),
                        navigateParams: this.initNavParam(item.getPSNavigateParams()),
                    }
                };
                Object.assign(navParam, param);
                const filter = {
                    [item.itemType]: item.navFilter ? item.navFilter : "",
                };
                Object.assign(navFilter, filter);
                const psDer = {
                    [item.itemType]: item.getNavPSDER() ? "n_" + ((_b = item.getNavPSDER().minorCodeName) === null || _b === void 0 ? void 0 : _b.toLowerCase()) + "_eq" : "",
                };
                Object.assign(navPSDer, psDer);
            });
        }
        this.navViewName = navViewName;
        this.navParam = navParam;
        this.navFilter = navFilter;
        this.navPSDer = navPSDer;
    }
    /**
     * split值变化事件
     *
     * @memberof CalendarExpBarControlBase
     */
    onSplitChange() {
        super.onSplitChange();
        if (this.$xDataControl) {
            const calendarContainer = this.$refs[this.xDataControlName].ctrl;
            if (calendarContainer.$refs[this.$xDataControl.codeName]) {
                const calendarApi = calendarContainer.$refs[this.$xDataControl.codeName].getApi();
                if (calendarApi) {
                    calendarApi.updateSize();
                    this.$forceUpdate();
                }
            }
        }
    }
    /**
     * 执行搜索
     *
     * @memberof CalendarExpBarControlBase
     */
    onSearch() {
        if (this.Environment && this.Environment.isPreviewMode) {
            return;
        }
        if (this.$xDataControl) {
            const calendarContainer = this.$refs[this.xDataControlName].ctrl;
            calendarContainer.searchEvents({ query: this.searchText });
        }
    }
    /**
     * 刷新
     *
     * @memberof CalendarExpBarControlBase
     */
    refresh() {
        if (this.$xDataControl) {
            const calendarContainer = this.$refs[this.xDataControlName].ctrl;
            calendarContainer.refresh();
        }
    }
    /**
     * 日历部件的选中数据事件
     *
     *
     * @param {any[]} args 选中数据
     * @return {*}  {void}
     * @memberof CalendarExpBarControlBase
     */
    onSelectionChange(args) {
        var _a, _b, _c, _d, _e, _f;
        let tempContext = {};
        let tempViewParam = {};
        if (args.length === 0) {
            this.calcToolbarItemState(true);
            return;
        }
        const arg = args[0];
        if (this.context) {
            Object.assign(tempContext, Util.deepCopy(this.context));
        }
        const calendarItem = (this.$xDataControl.getPSSysCalendarItems() || []).find((item) => {
            return item.itemType === arg.itemType;
        });
        const calendarItemEntity = calendarItem === null || calendarItem === void 0 ? void 0 : calendarItem.getPSAppDataEntity();
        if (calendarItem && calendarItemEntity) {
            Object.assign(tempContext, { [(_a = calendarItemEntity.codeName) === null || _a === void 0 ? void 0 : _a.toLowerCase()]: arg[(_b = calendarItemEntity.codeName) === null || _b === void 0 ? void 0 : _b.toLowerCase()] });
            Object.assign(tempContext, { srfparentdename: calendarItemEntity.codeName, srfparentdemapname: calendarItemEntity === null || calendarItemEntity === void 0 ? void 0 : calendarItemEntity.getPSDEName(), srfparentkey: arg[(_c = calendarItemEntity.codeName) === null || _c === void 0 ? void 0 : _c.toLowerCase()] });
            if (this.navFilter && this.navFilter[arg.itemType] && !Object.is(this.navFilter[arg.itemType], "")) {
                Object.assign(tempViewParam, { [this.navFilter[arg.itemType]]: arg[(_d = calendarItemEntity.codeName) === null || _d === void 0 ? void 0 : _d.toLowerCase()] });
            }
            if (this.navPSDer && this.navFilter[arg.itemType] && !Object.is(this.navPSDer[arg.itemType], "")) {
                Object.assign(tempViewParam, { [this.navPSDer[arg.itemType]]: arg[(_e = calendarItemEntity.codeName) === null || _e === void 0 ? void 0 : _e.toLowerCase()] });
            }
            if (this.navParam && this.navParam[arg.itemType] && this.navParam[arg.itemType].navigateContext && Object.keys(this.navParam[arg.itemType].navigateContext).length > 0) {
                let _context = Util.computedNavData(arg.curdata, tempContext, tempViewParam, this.navParam[arg.itemType].navigateContext);
                Object.assign(tempContext, _context);
            }
            if (this.navParam && this.navParam[arg.itemType] && this.navParam[arg.itemType].navigateParams && Object.keys(this.navParam[arg.itemType].navigateParams).length > 0) {
                let _params = Util.computedNavData(arg.curdata, tempContext, tempViewParam, this.navParam[arg.itemType].navigateParams);
                Object.assign(tempViewParam, _params);
            }
            if (calendarItem.getNavPSAppView()) {
                Object.assign(tempContext, {
                    viewpath: (_f = calendarItem.getNavPSAppView()) === null || _f === void 0 ? void 0 : _f.modelPath
                });
            }
        }
        this.selection = {};
        Object.assign(this.selection, { view: { viewname: 'app-view-shell' }, context: tempContext, viewparam: tempViewParam });
        this.calcToolbarItemState(false);
        this.$emit("ctrl-event", { controlname: this.controlInstance.name, action: "selectionchange", data: args });
    }
}
