import { AppViewLogicService } from '../app-service/logic-service/app-viewlogic-service';
import { MainControlBase } from "./main-control-base";
/**
 * 门户部件基类
 *
 * @export
 * @class ControlBase
 * @extends {PortletControlBase}
 */
export class PortletControlBase extends MainControlBase {
    constructor() {
        super(...arguments);
        /**
         * 操作栏模型对象
         *
         * @type {*}
         * @memberof PortletControlBase
         */
        this.actionBarModelData = {};
    }
    /**
     * 初始化工具栏数据
     *
     * @memberof PortletControlBase
     */
    initToolBar() {
        var _a, _b, _c;
        let toolbar = (_a = this.controlInstance.getPSControls()) === null || _a === void 0 ? void 0 : _a[0];
        let targetToolbarItems = [];
        if (toolbar && ((_b = toolbar.getPSDEToolbarItems()) === null || _b === void 0 ? void 0 : _b.length)) {
            (_c = toolbar.getPSDEToolbarItems()) === null || _c === void 0 ? void 0 : _c.forEach((item) => {
                var _a, _b, _c, _d, _e, _f;
                targetToolbarItems.push({
                    name: item.name,
                    showCaption: item.showCaption,
                    showIcon: item.showIcon,
                    tooltip: this.$tl((_a = item.getTooltipPSLanguageRes()) === null || _a === void 0 ? void 0 : _a.lanResTag, item.tooltip),
                    iconcls: (_c = (_b = item.getPSSysImage) === null || _b === void 0 ? void 0 : _b.call(item)) === null || _c === void 0 ? void 0 : _c.cssClass,
                    icon: (_e = (_d = item.getPSSysImage) === null || _d === void 0 ? void 0 : _d.call(item)) === null || _e === void 0 ? void 0 : _e.imagePath,
                    actiontarget: item.uIActionTarget,
                    caption: this.$tl((_f = item.getCapPSLanguageRes()) === null || _f === void 0 ? void 0 : _f.lanResTag, item.caption),
                    disabled: false,
                    itemType: item.itemType,
                    visabled: true,
                    noprivdisplaymode: item.noPrivDisplayMode,
                    dataaccaction: '',
                    uiaction: {}
                });
            });
        }
        this.toolbarModels = targetToolbarItems;
    }
    /**
     * 监听动态参数变化
     *
     * @param {*} newVal
     * @param {*} oldVal
     * @memberof PortletControlBase
     */
    onDynamicPropsChange(newVal, oldVal) {
        super.onDynamicPropsChange(newVal, oldVal);
        this.isAdaptiveSize = newVal.isAdaptiveSize;
    }
    /**
     * 初始化操作栏数据
     *
     * @memberof PortletControlBase
     */
    initActionBar() {
        var _a, _b, _c, _d;
        let groupDetails = (_d = (_c = (_b = (_a = this.controlInstance).getPSUIActionGroup) === null || _b === void 0 ? void 0 : _b.call(_a)) === null || _c === void 0 ? void 0 : _c.getPSUIActionGroupDetails) === null || _d === void 0 ? void 0 : _d.call(_c);
        if (groupDetails === null || groupDetails === void 0 ? void 0 : groupDetails.length) {
            groupDetails.forEach((item, index) => {
                var _a, _b, _c;
                let appUIAction = item.getPSUIAction();
                if (appUIAction) {
                    this.actionBarModelData[appUIAction.uIActionTag] = Object.assign(appUIAction, {
                        viewlogicname: item.name,
                        actionName: this.$tl((_a = appUIAction === null || appUIAction === void 0 ? void 0 : appUIAction.getCapPSLanguageRes()) === null || _a === void 0 ? void 0 : _a.lanResTag, appUIAction === null || appUIAction === void 0 ? void 0 : appUIAction.caption),
                        icon: (_c = (_b = appUIAction === null || appUIAction === void 0 ? void 0 : appUIAction.getPSSysImage) === null || _b === void 0 ? void 0 : _b.call(appUIAction)) === null || _c === void 0 ? void 0 : _c.cssClass,
                        // todo 计数器
                        // counterService: null,
                        // counterId: null, 
                        disabled: false,
                        visabled: (appUIAction === null || appUIAction === void 0 ? void 0 : appUIAction.dataAccessAction) && this.Environment.enablePermissionValid ? false : true,
                        getNoPrivDisplayMode: appUIAction.noPrivDisplayMode || 6
                    });
                }
            });
        }
    }
    /**
     * 触发界面行为
     *
     * @param {*} data 触发数据源
     * @param {*} $event 事件对象
     * @memberof PortletControlBase
     */
    handleItemClick(data, $event) {
        var _a, _b, _c, _d, _e;
        let tag = `${(_a = this.controlInstance.name) === null || _a === void 0 ? void 0 : _a.toLowerCase()}_${data.tag}_click`;
        if (((_b = this.controlInstance) === null || _b === void 0 ? void 0 : _b.portletType) == 'TOOLBAR') {
            // 工具栏要用工具栏的name
            tag = `${(_e = (_d = (_c = this.controlInstance.getPSControls()) === null || _c === void 0 ? void 0 : _c[0]) === null || _d === void 0 ? void 0 : _d.name) === null || _e === void 0 ? void 0 : _e.toLowerCase()}_${data.tag}_click`;
        }
        AppViewLogicService.getInstance().executeViewLogic(tag, $event ? $event : data.event, this, data.params ? data.params : data, this.controlInstance.getPSAppViewLogics());
    }
    /**
     * 初始化挂载状态集合
     *
     * @memberof PortletControlBase
     */
    initMountedMap() {
        var _a, _b;
        super.initMountedMap();
        let portletAppView = (_b = (_a = this.controlInstance) === null || _a === void 0 ? void 0 : _a.getPortletPSAppView) === null || _b === void 0 ? void 0 : _b.call(_a);
        if (portletAppView) {
            this.mountedMap.set(portletAppView.name, false);
        }
    }
    /**
     * 门户部件模型数据加载
     *
     * @memberof ViewBase
     */
    async ctrlModelLoad() {
        var _a, _b, _c, _d;
        switch (this.controlInstance.portletType) {
            case 'VIEW':
                await ((_b = (_a = this.controlInstance) === null || _a === void 0 ? void 0 : _a.getPortletPSAppView()) === null || _b === void 0 ? void 0 : _b.fill(true));
                break;
            case 'CUSTOM':
            case 'HTML':
            case 'RAWITEM':
                break;
            case 'APPMENU':
            case 'ACTIONBAR':
            case 'TOOLBAR':
            default:
                await ((_d = (_c = this.controlInstance.getPSControls()) === null || _c === void 0 ? void 0 : _c[0]) === null || _d === void 0 ? void 0 : _d.fill(true));
        }
    }
    /**
     * 初始化门户部件
     *
     * @memberof PortletControlBase
     */
    async ctrlModelInit(opts) {
        await super.ctrlModelInit();
        //初始化工具栏模型
        if (this.controlInstance.portletType == 'TOOLBAR') {
            this.initToolBar();
        }
        //初始化操作栏模型
        if (this.controlInstance.portletType == 'ACTIONBAR') {
            this.initActionBar();
        }
    }
    /**
     * 初始化部件的绘制参数
     *
     * @param {*} [opts]
     * @memberof PortletControlBase
     */
    initRenderOptions(opts) {
        super.initRenderOptions(opts);
        const { portletType } = this.controlInstance;
        Object.assign(this.renderOptions.controlClassNames, {
            [portletType.toLowerCase()]: true,
        });
    }
    /**
     * 部件初始化
     *
     * @param {*} [args]
     * @memberof PortletControlBase
     */
    ctrlInit(args) {
        super.ctrlInit(args);
    }
    viewStateAction(tag, action, data) {
        if (Object.is(tag, "all-portlet") && Object.is(action, 'loadmodel')) {
            this.calcUIActionAuthState(data);
        }
        if (tag == 'all-portlet' && action == 'refreshAll') {
            this.handleRefreshAll(data);
        }
        if (!Object.is(tag, this.name)) {
            return;
        }
        super.viewStateAction(tag, action, data);
        const refs = this.$refs;
        Object.keys(refs).forEach((_name) => {
            this.viewState.next({ tag: _name, action: action, data: data });
        });
    }
    /**
     * 刷新
     *
     * @memberof PortletControlBase
     */
    refresh(args) {
        var _a, _b;
        const { portletType } = this.controlInstance;
        let controls = (_a = this.controlInstance) === null || _a === void 0 ? void 0 : _a.getPSControls();
        if (portletType == "VIEW") {
            let portletAppView = (_b = this.controlInstance) === null || _b === void 0 ? void 0 : _b.getPortletPSAppView();
            this.viewState.next({ tag: portletAppView === null || portletAppView === void 0 ? void 0 : portletAppView.name, action: 'refresh', data: args });
        }
        if (portletType == "CHART" || portletType == "LIST") {
            this.viewState.next({ tag: controls === null || controls === void 0 ? void 0 : controls[0].name, action: 'refresh', data: args });
        }
        if (portletType == 'ACTIONBAR') {
            this.ctrlEvent({ controlname: this.controlInstance.name, action: 'refreshAll', data: args });
        }
    }
    /**
     * 处理dashboard的refreshAll通知
     *
     * @param {*} [args]
     * @memberof PortletControlBase
     */
    handleRefreshAll(args) {
        const { portletType } = this.controlInstance;
        if (portletType == "VIEW" || portletType == "CHART" || portletType == "LIST") {
            this.refresh();
        }
    }
    /**
     * 计算界面行为权限
     *
     * @param {*} [data={}]
     * @returns
     * @memberof PortletControlBase
     */
    calcUIActionAuthState(data = {}) {
        var _a;
        //  如果是操作栏，不计算权限
        if (((_a = this.controlInstance) === null || _a === void 0 ? void 0 : _a.portletType) == 'ACTIONBAR') {
            return;
        }
        let _this = this;
        // let uiservice: any = _this.appUIService ? _this.appUIService : new UIService(_this.$store);
        // if(_this.uiactionModel){
        //     ViewTool.calcActionItemAuthState(data,_this.uiactionModel,uiservice);
        // }
    }
}
