import { ModelTool, Util } from '@ibizstudio/runtime';
import { MainControlBase } from "./main-control-base";
/**
 * 数据关系栏部件基类
 *
 * @export
 * @class DrbarControlBase
 * @extends {MainControlBase}
 */
export class DrbarControlBase extends MainControlBase {
    constructor() {
        super(...arguments);
        /**
         * 表单名称
         *
         * @type {string}
         * @memberof DrbarControlBase
         */
        this.formName = '';
        /**
         * 数据选中项
         *
         * @type {*}
         * @memberof DrbarControlBase
         */
        this.selection = {};
        /**
         * 关系栏数据项
         *
         * @type {any[]}
         * @memberof DrbarControlBase
         */
        this.items = [];
        /**
         * 默认打开项
         *
         * @type {string[]}
         * @memberof DrbarControlBase
         */
        this.defaultOpeneds = [];
        /**
         * 父数据
         *
         * @type {*}
         * @memberof DrbarControlBase
         */
        this.parentData = {};
        /**
         * 宽度
         *
         * @type {number}
         * @memberof DrbarControlBase
         */
        this.width = 240;
        /**
         * @description 菜单方向
         * @type {('horizontal' | 'vertical')}
         * @memberof DrbarControlBase
         */
        this.menuDir = 'vertical';
        /**
         * @description 显示模式（DEFAULT：默认模式，INDEXMODE：嵌入实体首页视图中）
         * @type {('DEFAULT' | 'INDEXMODE')}
         * @memberof DrbarControlBase
         */
        this.showMode = 'DEFAULT';
        /**
         * @description 菜单项数据
         * @type {any[]}
         * @memberof DrbarControlBase
         */
        this.menuItems = [];
    }
    /**
     * @description 静态参数变化
     * @param {*} newVal 新值
     * @param {*} oldVal 旧值
     * @memberof DrbarControlBase
     */
    onStaticPropsChange(newVal, oldVal) {
        super.onStaticPropsChange(newVal, oldVal);
        this.showMode = newVal.showMode == 'INDEXMODE' ? 'INDEXMODE' : 'DEFAULT';
        this.menuDir = newVal.showMode == 'INDEXMODE' ? 'horizontal' : newVal.menuDir == 'horizontal' ? 'horizontal' : 'vertical';
    }
    /**
     * 部件模型初始化
     *
     * @memberof DrbarControlBase
     */
    async ctrlModelInit() {
        await super.ctrlModelInit();
        this.width = this.controlInstance.width >= 240 ? this.controlInstance.width : 240;
        this.initDrbarBasicData();
    }
    /**
     * 部件初始化
     *
     * @memberof DrbarControlBase
     */
    ctrlInit() {
        super.ctrlInit();
        this.initDefaultSelection();
    }
    viewStateAction(tag, action, data) {
        if (!Object.is(tag, this.name)) {
            return;
        }
        super.viewStateAction(tag, action, data);
        if (Object.is('state', action)) {
            this.handleFormChange(data);
        }
    }
    /**
     * @description 初始化默认选中
     * @memberof DrbarControlBase
     */
    initDefaultSelection() {
        if (this.items.length > 0) {
            if (this.showMode == 'INDEXMODE') {
                this.selection = this.items.length > 1 ? this.items[1] : this.items[0];
            }
            else {
                this.selection = this.items[0];
            }
        }
    }
    /**
     * 初始化关系栏数据项
     *
     * @memberof DrbarControlBase
     */
    initDrbarBasicData() {
        var _a, _b, _c, _d, _e, _f, _g;
        const formInstance = ModelTool.findPSControlByType('FORM', ((_d = (_c = (_b = (_a = this.controlInstance).getParentPSModelObject) === null || _b === void 0 ? void 0 : _b.call(_a)).getPSControls) === null || _d === void 0 ? void 0 : _d.call(_c)) || []);
        if (formInstance) {
            this.formName = (_e = formInstance.name) === null || _e === void 0 ? void 0 : _e.toLowerCase();
            const editItemCaption = (_f = this.controlInstance.M) === null || _f === void 0 ? void 0 : _f.editItemCaption;
            this.items.push({
                text: editItemCaption ? editItemCaption : formInstance.logicName,
                disabled: false,
                id: this.formName
            });
        }
        const ctrlItems = this.controlInstance.getPSDEDRCtrlItems() || [];
        const counterRef = this.controlInstance.getPSAppCounterRef();
        let counterService = undefined;
        if (counterRef) {
            counterService = (_g = Util.findElementByField(this.counterServiceArray, 'id', counterRef.id)) === null || _g === void 0 ? void 0 : _g.service;
        }
        ctrlItems.forEach((item) => {
            var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
            let _item = {
                text: this.$tl((_a = item.getCapPSLanguageRes()) === null || _a === void 0 ? void 0 : _a.lanResTag, item.caption),
                disabled: true,
                id: (_b = item.name) === null || _b === void 0 ? void 0 : _b.toLowerCase(),
                iconcls: (_e = (_d = (_c = item).getPSSysImage) === null || _d === void 0 ? void 0 : _d.call(_c)) === null || _e === void 0 ? void 0 : _e.cssClass,
                icon: (_h = (_g = (_f = item).getPSSysImage) === null || _g === void 0 ? void 0 : _g.call(_f)) === null || _h === void 0 ? void 0 : _h.imagePath,
                view: item,
                // TODO 待接口修复后调整。
                groupCodeName: ((_k = (_j = item === null || item === void 0 ? void 0 : item.M) === null || _j === void 0 ? void 0 : _j.getPSDEDRBarGroup) === null || _k === void 0 ? void 0 : _k.id) || '',
            };
            if (item.counterId && counterService) {
                Object.assign(_item, {
                    counter: {
                        id: item.counterId,
                        count: (_l = counterService === null || counterService === void 0 ? void 0 : counterService.counterData) === null || _l === void 0 ? void 0 : _l[item.counterId.toLowerCase()],
                        offset: _item.groupCodeName ? [10, 19] : [-16, 19],
                        showZero: 1
                    }
                });
            }
            this.items.push(_item);
        });
        this.initMenuItems();
    }
    /**
     * @description 初始化菜单项集合
     * @memberof DrbarControlBase
     */
    initMenuItems() {
        const groups = this.controlInstance.getPSDEDRBarGroups() || [];
        const menuItems = [];
        groups.forEach((group) => {
            var _a;
            const items = this.items.filter((item) => { return Object.is(item.groupCodeName, group.id); });
            if (items.length == 1) {
                menuItems.push(items[0]);
            }
            else {
                menuItems.push({
                    text: this.$tl((_a = group.getCapPSLanguageRes()) === null || _a === void 0 ? void 0 : _a.lanResTag, group.caption),
                    codeName: group.id,
                    id: group.id,
                    hidden: group.hidden,
                    items: items,
                });
            }
        });
        const noGroupItems = this.items.filter((item) => { return !item.groupCodeName; });
        menuItems.push.apply(menuItems, noGroupItems);
        this.menuItems = [...menuItems];
    }
    /**
     * 获取子项
     *
     * @param {any[]} items
     * @param {string} id
     * @returns {*}
     * @memberof DrbarControlBase
     */
    getItem(id) {
        return this.items.find((item) => {
            return item.id == id;
        });
    }
    /**
     * 节点选中
     *
     * @param {*} $event
     * @memberof DrbarControlBase
     */
    onSelect($event, isFirst = false) {
        if ($event == this.selection.id) {
            return;
        }
        const newSelectItem = this.getItem($event);
        this.selection = newSelectItem;
        this.ctrlEvent({ controlname: this.controlInstance.name, action: 'selectionchange', data: [this.selection] });
    }
    /**
     * 设置关系项状态
     *
     * @param {any[]} items
     * @param {boolean} state
     * @memberof DrbarControlBase
     */
    handleFormChange(args) {
        if (args && Object.is(args.srfuf, '1')) {
            this.items.forEach((item) => {
                // 取消禁用
                item.disabled = false;
                if (item.view) {
                    // 设置导航参数
                    const navigateContext = item.view.getPSNavigateContexts() || [];
                    if (navigateContext && navigateContext.length > 0) {
                        const localContext = Util.formatNavParam(navigateContext);
                        let _context = Util.computedNavData(args, this.context, this.viewparams, localContext);
                        item.localContext = _context;
                    }
                    const navigateParams = item.view.getPSNavigateParams() || [];
                    if (navigateParams && navigateParams.length > 0) {
                        const localViewParam = Util.formatNavParam(navigateParams);
                        let _param = Util.computedNavData(args, this.context, this.viewparams, localViewParam);
                        item.localViewParam = _param;
                    }
                }
            });
        }
        if (args.srfparentkey) {
            Object.assign(this.context, { srfparentkey: args.srfparentkey });
            Object.assign(this.viewparams, { srfparentkey: args.srfparentkey });
        }
        if (args.srfparentdename) {
            Object.assign(this.context, { srfparentdename: args.srfparentdename });
            Object.assign(this.viewparams, { srfparentdename: args.srfparentdename });
        }
        this.$forceUpdate();
    }
}
