import { ChartViewEngine, ModelTool } from '@ibizstudio/runtime';
import { MDViewBase } from './mdview-base';
/**
 * 图表视图基类
 *
 * @export
 * @class ChartViewBase
 * @extends {MDViewBase}
 * @implements {ChartViewInterface}
 */
export class ChartViewBase extends MDViewBase {
    constructor() {
        super(...arguments);
        /**
         * 视图引擎
         *
         * @public
         * @type {ChartViewEngine}
         * @memberof ChartViewBase
         */
        this.engine = new ChartViewEngine();
    }
    /**
     * 引擎初始化
     *
     * @param {*} [opts={}] 引擎参数
     * @memberof ChartViewBase
     */
    engineInit(opts = {}) {
        var _a, _b, _c, _d;
        if (this.Environment && this.Environment.isPreviewMode) {
            return;
        }
        if (this.engine && this.chartInstance) {
            let engineOpts = Object.assign({
                view: this,
                parentContainer: this.$parent,
                p2k: '0',
                // todo loadDefault 返回undefined
                isLoadDefault: this.viewInstance.loadDefault !== false ? true : false,
                keyPSDEField: ModelTool.getViewAppEntityCodeName(this.viewInstance).toLowerCase(),
                majorPSDEField: (_a = ModelTool.getAppEntityMajorField(this.viewInstance.getPSAppDataEntity())) === null || _a === void 0 ? void 0 : _a.codeName.toLowerCase(),
                opendata: (args, fullargs, params, $event, xData) => {
                    this.opendata(args, fullargs, params, $event, xData);
                },
                newdata: (args, fullargs, params, $event, xData) => {
                    this.newdata(args, fullargs, params, $event, xData);
                },
                chart: this.$refs[this.chartInstance.name].ctrl,
            }, opts);
            if (((_b = this.searchFormInstance) === null || _b === void 0 ? void 0 : _b.name) && this.$refs[this.searchFormInstance.name]) {
                engineOpts.searchform = (this.$refs[this.searchFormInstance.name].ctrl);
            }
            else if (((_c = this.quickSearchFormInstance) === null || _c === void 0 ? void 0 : _c.name) && this.$refs[this.quickSearchFormInstance.name]) {
                engineOpts.searchform = (this.$refs[this.quickSearchFormInstance.name].ctrl);
            }
            if (((_d = this.searchBarInstance) === null || _d === void 0 ? void 0 : _d.name) && this.$refs[this.searchBarInstance.name]) {
                engineOpts.searchbar = (this.$refs[this.searchBarInstance.name].ctrl);
            }
            this.engine.init(engineOpts);
        }
    }
    /**
      * 初始化图表视图实例
      *
      * @param opts
      * @memberof ChartViewBase
      */
    async viewModelInit() {
        var _a;
        // 填充图表视图实体
        await ((_a = this.viewInstance.getPSAppDataEntity()) === null || _a === void 0 ? void 0 : _a.fill());
        await super.viewModelInit();
        this.chartInstance = ModelTool.findPSControlByName('chart', this.viewInstance.getPSControls());
    }
    /**
     * 渲染视图主体内容区
     *
     * @returns
     * @memberof ChartViewBase
     */
    renderMainContent() {
        var _a;
        let { targetCtrlName, targetCtrlParam, targetCtrlEvent } = this.computeTargetCtrlData(this.chartInstance);
        return this.$createElement(targetCtrlName, { props: targetCtrlParam, ref: (_a = this.chartInstance) === null || _a === void 0 ? void 0 : _a.name, on: targetCtrlEvent });
    }
    /**
     * 快速搜索
     *
     * @param {*} $event
     * @memberof ChartViewBase
     */
    onSearch($event) {
        var _a, _b;
        const refs = this.$refs[(_a = this.chartInstance) === null || _a === void 0 ? void 0 : _a.name];
        if (refs && ((_b = refs.$refs) === null || _b === void 0 ? void 0 : _b.ctrl)) {
            refs.$refs.ctrl.refresh({});
        }
    }
}
