import { ModelTool, Util } from '@ibizstudio/runtime';
import { ExpBarControlBase } from './expbar-control-base';
export class ChartExpBarControlBase extends ExpBarControlBase {
    /**
     * 部件模型初始化
     *
     * @memberof ChartExpBarControlBase
     */
    async ctrlModelInit() {
        await super.ctrlModelInit();
        this.initNavView();
    }
    /**
     * 初始化导航视图
     *
     * @memberof ChartExpBarControlBase
     */
    initNavView() {
        if (!this.$xDataControl) {
            return;
        }
        let navViewName = {};
        let navParam = {};
        let navFilter = {};
        let navPSDer = {};
        const series = this.$xDataControl.getPSDEChartSerieses() || [];
        series.forEach((item) => {
            var _a, _b, _c, _d, _e;
            Object.assign(navViewName, {
                [item.name]: ((_b = (_a = item.getNavPSAppView) === null || _a === void 0 ? void 0 : _a.call(item)) === null || _b === void 0 ? void 0 : _b.modelPath) || ""
            });
            Object.assign(navParam, {
                [item.name.toLowerCase()]: {
                    navigateContext: this.initNavParam((_c = item.getPSNavigateContexts) === null || _c === void 0 ? void 0 : _c.call(item)),
                    navigateParams: this.initNavParam((_d = item.getPSNavigateParams) === null || _d === void 0 ? void 0 : _d.call(item))
                }
            });
            if (item.navFilter) {
                Object.assign(navFilter, {
                    [item.name]: item.navFilter || ""
                });
            }
            Object.assign(navPSDer, {
                [item.name]: item.getNavPSDER() ? "n_" + ((_e = item.getNavPSDER().minorCodeName) === null || _e === void 0 ? void 0 : _e.toLowerCase()) + "_eq" : ""
            });
        });
        this.navViewName = navViewName;
        this.navParam = navParam;
        this.navFilter = navFilter;
        this.navPSDer = navPSDer;
    }
    /**
     * 执行搜索
     *
     * @memberof ChartExpBarControlBase
     */
    onSearch() {
        var _a;
        if (this.Environment && this.Environment.isPreviewMode) {
            return;
        }
        let chart = (_a = this.$refs[`${this.xDataControlName}`]) === null || _a === void 0 ? void 0 : _a.ctrl;
        if (chart) {
            chart.load({ query: this.searchText });
        }
    }
    /**
     * 刷新
     *
     * @memberof ChartExpBarControlBase
     */
    refresh() {
        var _a;
        let chart = (_a = this.$refs[`${this.xDataControlName}`]) === null || _a === void 0 ? void 0 : _a.ctrl;
        if (chart) {
            chart.load({ query: this.searchText });
        }
    }
    /**
     * split值变化事件
     *
     * @memberof ChartExpBarControlBase
     */
    onSplitChange() {
        super.onSplitChange();
        let chart = this.$refs[`${this.xDataControlName}`].ctrl;
        if (chart && chart.myChart && chart.myChart.resize && chart.myChart.resize instanceof Function) {
            chart.myChart.resize();
        }
    }
    /**
     * 图表部件选中数据变化
     *
     * @memberof ChartExpBarControlBase
     */
    onSelectionChange(args) {
        var _a, _b, _c, _d, _e, _f, _g, _h;
        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 seriesItem = (this.$xDataControl.getPSDEChartSerieses() || []).find((item) => {
            return item.name.toLowerCase() === arg._chartName.toLowerCase();
        });
        const appDataEntity = (_a = this.$xDataControl) === null || _a === void 0 ? void 0 : _a.getPSAppDataEntity();
        if (seriesItem && appDataEntity) {
            Object.assign(tempContext, { [(_b = appDataEntity.codeName) === null || _b === void 0 ? void 0 : _b.toLowerCase()]: arg[(_d = (_c = ModelTool.getAppEntityKeyField(appDataEntity)) === null || _c === void 0 ? void 0 : _c.codeName) === null || _d === void 0 ? void 0 : _d.toLowerCase()] });
            Object.assign(tempContext, { srfparentdename: appDataEntity.codeName, srfparentdemapname: appDataEntity === null || appDataEntity === void 0 ? void 0 : appDataEntity.getPSDEName(), srfparentkey: arg[(_e = appDataEntity.codeName) === null || _e === void 0 ? void 0 : _e.toLowerCase()] });
            //  分类属性
            if (seriesItem.catalogField) {
                Object.assign(tempContext, { [seriesItem.catalogField]: arg[seriesItem.catalogField] });
                Object.assign(tempViewParam, { [seriesItem.catalogField]: arg[seriesItem.catalogField] });
            }
            //  数据属性
            if (seriesItem.valueField) {
                Object.assign(tempContext, { [seriesItem.valueField]: arg[seriesItem.valueField] });
                Object.assign(tempViewParam, { [seriesItem.valueField]: arg[seriesItem.valueField] });
            }
            if (this.navFilter && this.navFilter[arg._chartName] && !Object.is(this.navFilter[arg._chartName], "")) {
                Object.assign(tempViewParam, { [this.navFilter[arg._chartName]]: arg[(_f = appDataEntity.codeName) === null || _f === void 0 ? void 0 : _f.toLowerCase()] });
            }
            if (this.navPSDer && this.navFilter[arg._chartName] && !Object.is(this.navPSDer[arg._chartName], "")) {
                Object.assign(tempViewParam, { [this.navPSDer[arg._chartName]]: arg[(_g = appDataEntity.codeName) === null || _g === void 0 ? void 0 : _g.toLowerCase()] });
            }
            if (this.navParam && this.navParam[arg._chartName] && this.navParam[arg._chartName].navigateContext && Object.keys(this.navParam[arg._chartName].navigateContext).length > 0) {
                let _context = Util.computedNavData(arg, tempContext, tempViewParam, this.navParam[arg._chartName].navigateContext);
                Object.assign(tempContext, _context);
            }
            if (this.navParam && this.navParam[arg._chartName] && this.navParam[arg._chartName].navigateParams && Object.keys(this.navParam[arg._chartName].navigateParams).length > 0) {
                let _params = Util.computedNavData(arg, tempContext, tempViewParam, this.navParam[arg._chartName].navigateParams);
                Object.assign(tempViewParam, _params);
            }
            if (seriesItem.getNavPSAppView()) {
                Object.assign(tempContext, {
                    viewpath: (_h = seriesItem.getNavPSAppView()) === null || _h === void 0 ? void 0 : _h.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 });
    }
    /**
     * 部件事件
     * @param ctrl 部件
     * @param action  行为
     * @param data 数据
     *
     * @memberof ChartExpBarControlBase
     */
    onCtrlEvent(controlname, action, data) {
        if (action == 'selectionchange') {
            this.onSelectionChange(data.data);
        }
        else {
            super.onCtrlEvent(controlname, action, data);
        }
    }
}
