import { __decorate } from "tslib";
import { GetModelService, ModelTool, Util } from '@ibizstudio/runtime';
import { Component, Prop } from 'vue-property-decorator';
import { EditorBase } from '../editor-base/editor-base';
import { VueLifeCycleProcessing } from '../../../decorators';
import { PluginService } from '../../../app-service';
/**
 * 自动完成编辑器
 *
 * @export
 * @class DataPickerEditor
 * @extends {EditorBase}
 */
let DataPickerEditor = class DataPickerEditor extends EditorBase {
    constructor() {
        super(...arguments);
        /**
         * @description 插件工厂
         * @type {PluginService}
         * @memberof DataPickerEditor
         */
        this.PluginFactory = PluginService.getInstance();
    }
    /**
     * 编辑器初始化
     *
     * @memberof DataPickerEditor
     */
    async initEditor() {
        var _a, _b, _c, _d, _e, _f, _g;
        await super.initEditor();
        await this.initModelService();
        // 加载链接视图和选择视图
        let pickupAppView = (_b = (_a = this.editorInstance) === null || _a === void 0 ? void 0 : _a.getPickupPSAppView) === null || _b === void 0 ? void 0 : _b.call(_a);
        let linkAppView = (_d = (_c = this.editorInstance) === null || _c === void 0 ? void 0 : _c.getLinkPSAppView) === null || _d === void 0 ? void 0 : _d.call(_c);
        await (pickupAppView === null || pickupAppView === void 0 ? void 0 : pickupAppView.fill(true));
        await (linkAppView === null || linkAppView === void 0 ? void 0 : linkAppView.fill(true));
        this.codeList = (_f = (_e = this.editorInstance) === null || _e === void 0 ? void 0 : _e.getPSAppCodeList) === null || _f === void 0 ? void 0 : _f.call(_e);
        const { placeHolder } = this.editorInstance;
        this.customProps.deMajorField = ModelTool.getEditorMajorName(this.editorInstance);
        this.customProps.deKeyField = ModelTool.getEditorKeyName(this.editorInstance);
        switch ((_g = this.editorInstance) === null || _g === void 0 ? void 0 : _g.editorType) {
            // 数据链接
            case 'PICKEREX_LINKONLY':
                this.customProps.editorType = 'linkonly';
                await this.initLinkViewParams();
                break;
            // 数据选择（无按钮）
            case 'PICKEREX_NOBUTTON':
                this.customProps.editorType = 'nobutton';
                this.customProps.showButton = false;
            // 数据选择
            case 'PICKER':
                this.initPickupViewParams();
                this.initAcParams();
                break;
            // 数据选择（无AC、数据链接）
            case 'PICKEREX_NOAC_LINK':
                this.customProps.editorType = 'pickup-no-ac';
                this.initPickupViewParams();
                await this.initLinkViewParams();
                break;
            // 数据选择（下拉、数据链接）
            case 'PICKEREX_TRIGGER_LINK':
                this.customProps.editorType = 'dropdown-link';
                this.customProps.showButton = false;
                this.initPickupViewParams();
                await this.initLinkViewParams();
                this.initAcParams();
                break;
            // 数据选择（下拉）
            case 'PICKEREX_TRIGGER':
                this.customProps.editorType = 'dropdown';
                this.initAcParams();
                break;
            // 数据选择（无AC）
            case 'PICKEREX_NOAC':
                this.customProps.editorType = 'pickup-no-ac';
                this.initPickupViewParams();
                break;
            // 数据选择（数据链接）
            case 'PICKEREX_LINK':
                this.initPickupViewParams();
                await this.initLinkViewParams();
                this.initAcParams();
                break;
            // 数据选择（下拉视图）
            case 'PICKEREX_DROPDOWNVIEW':
                this.customProps.pickupView = pickupAppView || undefined;
                break;
            // 数据选择（下拉视图、数据链接）
            case 'PICKEREX_DROPDOWNVIEW_LINK':
                this.customProps.pickupView = pickupAppView || undefined;
                await this.initLinkViewParams();
                break;
            // 数据选择（嵌入选择视图）
            case 'PICKUPVIEW':
                this.customProps.emptyText = placeHolder;
                this.customProps.viewModelData = pickupAppView || undefined;
                this.customProps.refreshitems = this.editorInstance.getParentPSModelObject().getResetItemNames();
                break;
            // 地址框（选择）
            case 'ADDRESSPICKUP':
                this.initPickupViewParams();
                this.initAcParams();
                break;
            // 地址框（支持选择、AC）
            case 'ADDRESSPICKUP_AC':
                this.initPickupViewParams();
                this.initAcParams();
                break;
        }
    }
    /**
     * 初始化选择视图相关参数
     *
     * @memberof DataPickerEditor
     */
    initPickupViewParams() {
        var _a;
        let pickupAppView = this.editorInstance.getPickupPSAppView();
        if (pickupAppView) {
            const view = {
                viewname: 'app-view-shell',
                title: this.$tl((_a = pickupAppView.getCapPSLanguageRes()) === null || _a === void 0 ? void 0 : _a.lanResTag, pickupAppView.title),
                width: pickupAppView === null || pickupAppView === void 0 ? void 0 : pickupAppView.width,
                height: pickupAppView === null || pickupAppView === void 0 ? void 0 : pickupAppView.height,
                placement: pickupAppView === null || pickupAppView === void 0 ? void 0 : pickupAppView.openMode,
                isRedirectView: pickupAppView.hasOwnProperty('redirectView') ? pickupAppView.redirectView : false,
                viewpath: pickupAppView === null || pickupAppView === void 0 ? void 0 : pickupAppView.modelPath
            };
            this.customProps.pickupView = view;
        }
    }
    /**
    * 初始化模型服务
    *
    * @memberof DataPickerEditor
    */
    async initModelService() {
        this.modelService = await GetModelService(this.context);
    }
    /**
     * 初始化链接视图相关参数
     *
     * @memberof DataPickerEditor
     */
    async initLinkViewParams() {
        var _a, _b, _c, _d;
        let linkAppView = this.editorInstance.getLinkPSAppView();
        await (linkAppView === null || linkAppView === void 0 ? void 0 : linkAppView.fill(true));
        if (linkAppView) {
            const view = {
                viewname: 'app-view-shell',
                title: this.$tl((_a = linkAppView.getCapPSLanguageRes()) === null || _a === void 0 ? void 0 : _a.lanResTag, linkAppView.title),
                width: linkAppView === null || linkAppView === void 0 ? void 0 : linkAppView.width,
                height: linkAppView === null || linkAppView === void 0 ? void 0 : linkAppView.height,
                placement: linkAppView === null || linkAppView === void 0 ? void 0 : linkAppView.openMode,
                deResParameters: Util.formatAppDERSPath(this.context, linkAppView.getPSAppDERSPaths()),
                isRedirectView: linkAppView.redirectView,
                viewpath: linkAppView === null || linkAppView === void 0 ? void 0 : linkAppView.modelPath
            };
            Object.defineProperty(view, 'viewModel', { enumerable: false, writable: true, value: linkAppView });
            if (linkAppView === null || linkAppView === void 0 ? void 0 : linkAppView.getPSAppDataEntity()) {
                Object.assign(view, {
                    parameters: [
                        { pathName: Util.srfpluralize(((_b = linkAppView.getPSAppDataEntity()) === null || _b === void 0 ? void 0 : _b.codeName) || '').toLowerCase(), parameterName: (_c = linkAppView.getPSAppDataEntity()) === null || _c === void 0 ? void 0 : _c.codeName.toLowerCase() },
                        { pathName: 'views', parameterName: (_d = linkAppView.getPSDEViewCodeName()) === null || _d === void 0 ? void 0 : _d.toLowerCase() },
                    ]
                });
            }
            else {
                Object.assign(view, {
                    parameters: [
                        { pathName: 'views', parameterName: linkAppView.codeName.toLowerCase() }
                    ]
                });
            }
            this.customProps.linkview = view;
        }
    }
    /**
     * 初始化AC相关参数
     *
     * @memberof DataPickerEditor
     */
    initAcParams() {
        this.customProps.acParams = ModelTool.getAcParams(this.editorInstance);
        this.customProps.sort = ModelTool.getAcSort(this.editorInstance);
        this.acItemPlugin = ModelTool.getAcItemPlugin(this.editorInstance);
        // 实体自填模式
        if (this.editorInstance.getPSAppDEACMode()) {
            const acMode = this.editorInstance.getPSAppDEACMode();
            // 数据项
            if (acMode.getPSDEACModeDataItems() &&
                acMode.getPSDEACModeDataItems().length > 0) {
                this.customProps.dataItems = acMode.getPSDEACModeDataItems();
            }
            // 支持分页
            if (acMode.enablePagingBar) {
                this.customProps.enablePagingBar = acMode.enablePagingBar;
            }
            else {
                this.customProps.enablePagingBar = false;
            }
            // 分页数量
            if (acMode.pagingSize) {
                this.customProps.limit = acMode.pagingSize;
            }
        }
    }
    /**
     * 绘制嵌入视图
     *
     * @memberof DataPickerEditor
     */
    renderEmbedPicker() {
        var _a;
        return this.$createElement(this.editorComponentName, {
            props: Object.assign({ name: this.editorInstance.name, value: this.value, disabled: this.disabled, ignorefieldvaluechange: this.ignorefieldvaluechange, data: JSON.stringify(this.contextData), context: this.context, viewparams: this.viewparams, valueItem: ((_a = this.parentItem) === null || _a === void 0 ? void 0 : _a.valueItemName) || '' }, this.customProps),
            on: { formitemvaluechange: this.editorChange },
            style: this.customStyle
        });
    }
    /**
     * 默认绘制
     *
     * @memberof DataPickerEditor
     */
    renderDefault() {
        var _a, _b, _c, _d;
        return this.$createElement(this.editorComponentName, {
            props: Object.assign({ parentCodeName: (_a = this.parentItem) === null || _a === void 0 ? void 0 : _a.codeName, name: this.editorInstance.name, value: this.value, disabled: this.disabled, data: this.contextData, context: this.context, viewparams: this.viewparams, service: this.service, acItemPlugin: this.acItemPlugin ? true : false, pickUpData: ((_b = this.editorInstance.editorParams) === null || _b === void 0 ? void 0 : _b['pickupdata']) ? (_c = this.editorInstance.editorParams) === null || _c === void 0 ? void 0 : _c['pickupdata'] : null, valueitem: ((_d = this.parentItem) === null || _d === void 0 ? void 0 : _d.valueItemName) || '', editorInstance: this.editorInstance }, this.customProps),
            on: { formitemvaluechange: this.editorChange },
            style: this.customStyle,
            scopedSlots: {
                acItemPlugin: ({ item }) => {
                    if (this.acItemPlugin) {
                        const pluginInstance = this.PluginFactory.getPluginInstance('DEACMODE', this.acItemPlugin.pluginCode);
                        if (pluginInstance) {
                            return pluginInstance.renderAcItem(this.$createElement, item, this.value, this);
                        }
                    }
                }
            }
        });
    }
    /**
     * 数据选择插件（地图定位）
     *
     * @memberof DataPickerEditor
     */
    renderMapPosition() {
        return this.$createElement(this.editorComponentName, {
            props: Object.assign({ name: this.editorInstance.name, value: this.value, disabled: this.disabled, data: this.contextData, context: this.context, viewparams: this.viewparams }, this.customProps),
            on: {
                change: (value) => { this.editorChange({ name: this.editorInstance.name, value: value }); },
                itemChange: (val1, val2) => { this.editorChange(val1); this.editorChange(val2); }
            }
        });
    }
    /**
     * 单位选择器
     *
     * @memberof DataPickerEditor
     */
    renderOrgSelect() {
        var _a;
        let params = this.initSelectPickerParams();
        Object.assign(params, {
            filter: ((_a = this.editorInstance.editorParams) === null || _a === void 0 ? void 0 : _a['filter']) ? this.editorInstance.editorParams['filter'] : 'srforgid',
        });
        return this.$createElement(this.editorComponentName, {
            props: params,
            on: {
                "select-change": this.editorChange.bind(this),
            }
        });
    }
    /**
     * 部门人员选择器
     *
     * @memberof DataPickerEditor
     */
    renderDepartmentPersonnel() {
        var _a, _b;
        let params = this.initSelectPickerParams();
        Object.assign(params, {
            filter: ((_a = this.editorInstance.editorParams) === null || _a === void 0 ? void 0 : _a['filter']) ? this.editorInstance.editorParams['filter'] : 'srfpdept',
        });
        if ((_b = this.editorInstance.editorParams) === null || _b === void 0 ? void 0 : _b['treeurl']) {
            Object.assign(params, {
                treeurl: this.editorInstance.editorParams['treeurl'],
            });
        }
        return this.$createElement(this.editorComponentName, {
            props: params,
            on: {
                formitemvaluechange: this.editorChange.bind(this)
            }
        });
    }
    /**
     * 单位人员选择器
     *
     * @memberof DataPickerEditor
     */
    renderGroupSelect() {
        var _a, _b, _c;
        let params = this.initSelectPickerParams();
        Object.assign(params, {
            filter: ((_a = this.editorInstance.editorParams) === null || _a === void 0 ? void 0 : _a['filter']) ? this.editorInstance.editorParams['filter'] : 'srforgid',
        });
        if ((_b = this.editorInstance.editorParams) === null || _b === void 0 ? void 0 : _b['treeurl']) {
            Object.assign(params, {
                treeurl: this.editorInstance.editorParams['treeurl'],
            });
        }
        if ((_c = this.editorInstance.editorParams) === null || _c === void 0 ? void 0 : _c['editorMode']) {
            Object.assign(params, {
                editorMode: this.editorInstance.editorParams['editorMode'],
            });
        }
        return this.$createElement(this.editorComponentName, {
            props: params,
            on: {
                formitemvaluechange: this.editorChange.bind(this)
            }
        });
    }
    /**
     * 部门选择器
     *
     * @memberof DataPickerEditor
     */
    renderDepartmentSelect() {
        var _a;
        let params = this.initSelectPickerParams();
        Object.assign(params, {
            filter: ((_a = this.editorInstance.editorParams) === null || _a === void 0 ? void 0 : _a['filter']) ? this.editorInstance.editorParams['filter'] : 'srforgid',
        });
        return this.$createElement(this.editorComponentName, {
            props: params,
            on: {
                "select-change": this.editorChange.bind(this)
            }
        });
    }
    /**
     * 单多选列表框
     *
     * @memberof DataPickerEditor
     */
    renderCommonMicrocom() {
        let params = this.initSelectPickerParams();
        Object.is(params, {
            viewparams: this.viewparams
        });
        return this.$createElement(this.editorComponentName, {
            props: params,
            on: {
                formitemvaluechange: this.editorChange.bind(this)
            }
        });
    }
    /**
     * 组织部门人员微服务组件参数处理
     *
     * @memberof DataPickerEditor
     */
    initSelectPickerParams() {
        var _a, _b, _c, _d, _e, _f, _g, _h;
        let params = {
            name: this.editorInstance.name,
            value: this.value,
            valueitem: ((_a = this.parentItem) === null || _a === void 0 ? void 0 : _a.valueItemName) || '',
            multiple: ((_b = this.editorInstance.editorParams) === null || _b === void 0 ? void 0 : _b['multiple']) ? JSON.parse(this.editorInstance.editorParams['multiple']) : false,
            url: (_c = this.editorInstance.editorParams) === null || _c === void 0 ? void 0 : _c['url'],
            filter: (_d = this.editorInstance.editorParams) === null || _d === void 0 ? void 0 : _d['filter'],
            requestMode: (_f = (_e = this.editorInstance.editorParams) === null || _e === void 0 ? void 0 : _e['requestMode']) === null || _f === void 0 ? void 0 : _f.toLowerCase(),
            fillMap: ((_g = this.editorInstance.editorParams) === null || _g === void 0 ? void 0 : _g['fillMap']) ? eval('(' + this.editorInstance.editorParams['fillMap'] + ')') : { id: ((_h = this.parentItem) === null || _h === void 0 ? void 0 : _h.valueItemName) || '', label: this.editorInstance.name },
            disabled: this.disabled,
            data: this.contextData,
            context: this.context,
            style: this.customStyle,
            localParam: this.customProps.localParam,
            localContext: this.customProps.localContext,
        };
        if (this.codeList) {
            Object.assign(params, {
                tag: this.codeList.codeName,
                codelistType: this.codeList.codeListType,
                renderMode: this.codeList.orMode,
                valueSeparator: this.codeList.valueSeparator,
                textSeparator: this.codeList.textSeparator
            });
        }
        return params;
    }
    /**
     * 绘制内容
     *
     * @returns {*}
     * @memberof DataPickerEditor
     */
    render() {
        if (!this.editorIsLoaded) {
            return null;
        }
        const { editorType: type, editorStyle: style } = this.editorInstance;
        const editorTypeStyle = `${type}${style && style != 'DEFAULT' ? '_' + style : ''}`;
        switch (editorTypeStyle) {
            case 'PICKEREX_LINKONLY':
            case 'PICKER':
            case 'PICKEREX_NOBUTTON':
            case 'PICKEREX_NOAC_LINK':
            case 'PICKEREX_TRIGGER_LINK':
            case 'PICKEREX_TRIGGER':
            case 'PICKEREX_NOAC':
            case 'PICKEREX_LINK':
            case 'PICKEREX_DROPDOWNVIEW':
            case 'PICKEREX_DROPDOWNVIEW_LINK':
            case 'ADDRESSPICKUP':
            case 'ADDRESSPICKUP_AC':
                return this.renderDefault();
            case 'PICKUPVIEW':
                return this.renderEmbedPicker();
            case 'PICKER_MAPPOSITION':
                return this.renderMapPosition();
            case 'PICKER_ORGSELECT':
            case 'PICKER_ORGMULTIPLE':
            case 'PICKER_ALLORGSELECT':
            case 'PICKER_ALLORGMULTIPLE':
                return this.renderOrgSelect();
            case 'PICKER_ALLDEPTPERSONSELECT':
            case 'PICKER_ALLDEPTPERSONMULTIPLE':
            case 'PICKER_DEPTPERSONSELECT':
            case 'PICKER_DEPTPERSONMULTIPLE':
                return this.renderDepartmentPersonnel();
            case 'PICKER_ALLEMPSELECT':
            case 'PICKER_ALLEMPMULTIPLE':
            case 'PICKER_EMPSELECT':
            case 'PICKER_EMPMULTIPLE':
                return this.renderGroupSelect();
            case 'PICKER_ALLDEPATMENTSELECT':
            case 'PICKER_ALLDEPATMENTMULTIPLE':
            case 'PICKER_DEPATMENTSELECT':
            case 'PICKER_DEPATMENTMULTIPLE':
                return this.renderDepartmentSelect();
            case 'PICKER_COMMONMICROCOM':
                return this.renderCommonMicrocom();
        }
    }
};
__decorate([
    Prop()
], DataPickerEditor.prototype, "ignorefieldvaluechange", void 0);
DataPickerEditor = __decorate([
    Component({}),
    VueLifeCycleProcessing()
], DataPickerEditor);
export default DataPickerEditor;
