import { __decorate } from "tslib";
import { ModelTool, Util } from '@ibizstudio/runtime';
import { Vue, Prop, Emit } from 'vue-property-decorator';
import { AppComponentService } from '../../../app-service';
import { Watch } from '../../../decorators';
/**
 * editor解析器基类
 *
 * @export
 * @class EditorBase
 * @extends {Vue}
 */
export class EditorBase extends Vue {
    constructor() {
        super(...arguments);
        /**
         * 自定义样式的对象
         *
         * @type {*}
         * @memberof EditorBase
         */
        this.customStyle = {};
        /**
         * 设置自定义props
         *
         * @type {*}
         * @memberof EditorBase
         */
        this.customProps = {};
        /**
         * 编辑器是否初始化完成
         *
         * @type {boolean}
         * @memberof EditorBase
         */
        this.editorIsLoaded = false;
    }
    /**
     * 编辑器change事件
     *
     * @param {*} value
     * @memberof EditorBase
     */
    editorChange(value) { }
    /**
     * 生命周期-created
     *
     * @memberof EditorBase
     */
    created() { }
    /**
     * editorJsonStr值变化
     *
     * @param {*} newVal
     * @param {*} oldVal
     * @memberof EditorBase
     */
    onEditorInstanceChange(newVal, oldVal) {
        if (newVal && newVal != oldVal) {
            this.initEditorBase();
        }
    }
    /**
     * 编辑器初始化(基类)
     *
     * @memberof EditorBase
     */
    async initEditorBase() {
        this.editorChange = this.editorChange.bind(this);
        this.editorComponentName = AppComponentService.getEditorComponents(this.editorInstance.editorType, this.editorInstance.editorStyle);
        this.setCustomStyle();
        this.setCustomProps();
        await this.initEditor();
        this.setEditorParams();
        this.editorIsLoaded = true;
    }
    /**
     * 编辑器初始化
     *
     * @memberof EditorBase
     */
    async initEditor() {
        var _a, _b, _c, _d, _e, _f;
        try {
            // 加载编辑器实体
            await ((_c = (_b = (_a = this.editorInstance) === null || _a === void 0 ? void 0 : _a.getPSAppDataEntity) === null || _b === void 0 ? void 0 : _b.call(_a)) === null || _c === void 0 ? void 0 : _c.fill());
            // 加载编辑器代码表
            await ((_f = (_e = (_d = this.editorInstance) === null || _d === void 0 ? void 0 : _d.getPSAppCodeList) === null || _e === void 0 ? void 0 : _e.call(_d)) === null || _f === void 0 ? void 0 : _f.fill());
        }
        catch (error) {
            console.error(error);
        }
    }
    /**
     * 设置编辑器的自定义高宽
     *
     * @memberof EditorBase
     */
    setCustomStyle() {
        let { editorWidth, editorHeight } = this.editorInstance;
        this.customStyle = {};
        if (!Util.isEmpty(editorWidth) && editorWidth != 0) {
            this.customStyle.width = editorWidth > 1 ? editorWidth + "px" : editorWidth * 100 + "%";
        }
        if (!Util.isEmpty(editorHeight) && editorHeight != 0) {
            this.customStyle.height = editorHeight > 1 ? editorHeight + "px" : editorHeight * 100 + "%";
        }
    }
    /**
     * 设置编辑器的自定义参数
     *
     * @memberof EditorBase
     */
    setCustomProps() {
        this.customProps = {
            placeholder: this.editorInstance.placeHolder,
        };
        // 只读状态
        if (this.editorInstance.readOnly) {
            Object.assign(this.customProps, {
                readonly: this.editorInstance.readOnly,
            });
        }
    }
    /**
     * 设置编辑器导航参数
     *
     * @param keys 编辑器参数key
     * @memberof EditorBase
     */
    setEditorParams() {
        let _this = this;
        if (!this.editorInstance.editorParams) {
            return;
        }
        Object.assign(this.customProps, {
            localContext: ModelTool.getNavigateContext(this.editorInstance),
            localParam: ModelTool.getNavigateParams(this.editorInstance)
        });
        for (const key in this.editorInstance.editorParams) {
            let param;
            if (key == 'uploadparams' || key == 'exportparams' || key == 'readonly') {
                param = eval('(' + this.editorInstance.editorParams[key] + ')');
            }
            else {
                param = this.editorInstance.editorParams[key];
            }
            if (key.indexOf('.') != -1) {
                let splitArr = key.split('.');
                switch (splitArr[0]) {
                    case "SRFNAVPARAM":
                        Object.assign(this.customProps.localParam, { [splitArr[1]]: param });
                        break;
                    case "SRFNAVCTX":
                        Object.assign(this.customProps.localContext, { [splitArr[1]]: param });
                        break;
                }
            }
            else {
                if (param) {
                    this.customProps[key] = param;
                }
            }
        }
    }
    /**
     * 绘制内容
     *
     * @returns {*}
     * @memberof EditorBase
     */
    render() {
        return <div>{this.editorInstance ? this.$t('app.editor.nooutput') : this.$t('app.editor.noexist')}</div>;
    }
}
__decorate([
    Prop()
], EditorBase.prototype, "value", void 0);
__decorate([
    Prop()
], EditorBase.prototype, "editorInstance", void 0);
__decorate([
    Prop()
], EditorBase.prototype, "containerCtrl", void 0);
__decorate([
    Prop()
], EditorBase.prototype, "parentItem", void 0);
__decorate([
    Prop()
], EditorBase.prototype, "context", void 0);
__decorate([
    Prop()
], EditorBase.prototype, "viewparams", void 0);
__decorate([
    Prop()
], EditorBase.prototype, "contextData", void 0);
__decorate([
    Prop({ default: false })
], EditorBase.prototype, "disabled", void 0);
__decorate([
    Prop()
], EditorBase.prototype, "service", void 0);
__decorate([
    Prop()
], EditorBase.prototype, "valueFormat", void 0);
__decorate([
    Emit('change')
], EditorBase.prototype, "editorChange", null);
__decorate([
    Watch('editorInstance', { immediate: true })
], EditorBase.prototype, "onEditorInstanceChange", null);
