import { CommonViewEngine, throttle, ModelTool } from '@ibizstudio/runtime';
import { MainViewBase } from "./mainview-base";
/**
 * 自定义视图基类
 *
 * @export
 * @class CustomViewBase
 * @extends {MainViewBase}
 * @implements {CustomViewInterface}
 */
export class CustomViewBase extends MainViewBase {
    constructor() {
        super(...arguments);
        /**
         * 视图引擎
         *
         * @public
         * @type {CommonViewEngine}
         * @memberof CustomViewBase
         */
        this.engine = new CommonViewEngine();
    }
    /**
      * 初始化图表视图实例
      *
      * @param opts
      * @memberof CustomViewBase
      */
    async viewModelInit() {
        await super.viewModelInit();
    }
    /**
     * 初始化工具栏数据
     *
     * @memberof MainViewBase
     */
    initViewToolBar() {
        var _a;
        const targetViewToolbarItems = [];
        const viewToolBar = ModelTool.findPSControlByName('toolbar', this.viewInstance.getPSControls());
        if (viewToolBar && viewToolBar.getPSDEToolbarItems()) {
            (_a = viewToolBar.getPSDEToolbarItems()) === null || _a === void 0 ? void 0 : _a.forEach((toolbarItem) => {
                targetViewToolbarItems.push(this.initToolBarItems(toolbarItem));
            });
        }
        this.toolbarModels = targetViewToolbarItems;
    }
    /**
     * 绘制目标部件
     *
     * @memberof CustomViewBase
     */
    renderTargetControl(control) {
        var _a, _b, _c;
        if (Object.is(control.controlType, 'TOOLBAR')) {
            const viewToolBar = control;
            const targetViewToolbarItems = [];
            if (viewToolBar && viewToolBar.getPSDEToolbarItems()) {
                (_a = viewToolBar.getPSDEToolbarItems()) === null || _a === void 0 ? void 0 : _a.forEach((toolbarItem) => {
                    targetViewToolbarItems.push(this.initToolBarItems(toolbarItem));
                });
            }
            return (<view-toolbar slot={`layout-${control.name}`} mode={((_b = this.viewInstance) === null || _b === void 0 ? void 0 : _b.viewStyle) || 'DEFAULT'} counterServiceArray={this.counterServiceArray} isViewLoading={(_c = this.viewLoadingService) === null || _c === void 0 ? void 0 : _c.isLoading} toolbarModels={targetViewToolbarItems} on-item-click={(data, $event) => {
                    throttle(this.handleItemClick, [data, $event], this);
                }}></view-toolbar>);
        }
        else {
            let { targetCtrlName, targetCtrlParam, targetCtrlEvent } = this.computeTargetCtrlData(control);
            if (Object.is(control.controlType, 'SEARCHFORM') || Object.is(control.controlType, 'SEARCHBAR')) {
                Object.assign(targetCtrlParam.dynamicProps, { isExpandSearchForm: true });
            }
            return this.$createElement(targetCtrlName, { slot: `layout-${control.name}`, props: targetCtrlParam, ref: control === null || control === void 0 ? void 0 : control.name, on: targetCtrlEvent });
        }
    }
    /**
   * 引擎初始化
   *
   * @param {*} [opts={}] 引擎参数
   * @memberof ChartViewBase
   */
    engineInit(opts = {}) {
        var _a;
        if (this.Environment && this.Environment.isPreviewMode) {
            return;
        }
        const engineOptions = {
            view: this,
            parentContainer: this.$parent,
            keyPSDEField: ModelTool.getViewAppEntityCodeName(this.viewInstance).toLowerCase(),
            majorPSDEField: (_a = ModelTool.getAppEntityMajorField(this.viewInstance.getPSAppDataEntity())) === null || _a === void 0 ? void 0 : _a.codeName.toLowerCase()
        };
        if (this.viewInstance.getPSControls() && this.viewInstance.getPSControls().length > 0) {
            const ctrlArray = [];
            this.viewInstance.getPSControls().forEach((item) => {
                if (!Object.is(item.controlType, 'TOOLBAR')) {
                    ctrlArray.push({ name: item.name, ctrl: this.$refs[item.name].ctrl });
                }
            });
            Object.assign(engineOptions, { ctrl: ctrlArray });
        }
        if (this.viewInstance.getPSAppViewEngines() && this.viewInstance.getPSAppViewEngines().length > 0) {
            const engineArray = [];
            this.viewInstance.getPSAppViewEngines().forEach((item) => {
                if (Object.is(item.engineCat, 'CTRL')) {
                    engineArray.push(item.M);
                }
            });
            Object.assign(engineOptions, { engine: engineArray });
        }
        this.engine.init(engineOptions);
    }
}
