import { __decorate } from "tslib";
import { DataTypes, ModelTool, Util } from '@ibizstudio/runtime';
import { Component, Prop } from 'vue-property-decorator';
import { VueLifeCycleProcessing } from '../../../decorators';
import { EditorBase } from '../editor-base/editor-base';
/**
 * 文本框编辑器
 *
 * @export
 * @class TextboxEditor
 * @extends {EditorBase}
 */
let TextboxEditor = class TextboxEditor extends EditorBase {
    /**
     * 编辑器初始化
     *
     * @memberof TextboxEditor
     */
    async initEditor() {
        var _a, _b, _c, _d;
        await super.initEditor();
        let unitName = (_a = this.parentItem) === null || _a === void 0 ? void 0 : _a.unitName;
        let appDeField = (_c = (_b = this.parentItem) === null || _b === void 0 ? void 0 : _b.getPSAppDEField) === null || _c === void 0 ? void 0 : _c.call(_b);
        switch ((_d = this.editorInstance) === null || _d === void 0 ? void 0 : _d.editorType) {
            case 'TEXTBOX':
                this.customProps.isDebounce = this.isDebounce;
                this.customProps.type = ModelTool.isNumberField(appDeField) ? 'number' : 'text';
                this.customProps.unit = unitName;
                this.customProps.precision = ModelTool.getPrecision(this.editorInstance, appDeField);
                break;
            case 'PASSWORD':
                this.customProps.isDebounce = this.isDebounce;
                this.customProps.type = 'password';
                break;
            case 'TEXTAREA':
                this.customProps.isDebounce = this.isDebounce;
                this.customProps.type = 'textarea';
                break;
            case 'TEXTAREA_10':
                this.customProps.isDebounce = this.isDebounce;
                this.customProps.type = 'textarea';
                this.customProps.textareaId = Util.createUUID();
                // todo lxm getEditorCssStyle
                // this.customProps.textareaStyle = this.editorInstance?.getEditorCssStyle || "";
                this.customProps.rows = 10;
                break;
            case 'NUMBER':
                this.customProps.isDebounce = this.isDebounce;
                this.customProps.type = 'number';
                this.customProps.unit = unitName;
                this.customProps.precision = ModelTool.getPrecision(this.editorInstance, appDeField);
                break;
            case 'MARKDOWN':
                this.customProps.mode = this.editorInstance.mode ? this.editorInstance.mode : 'EDIT';
                break;
        }
        this.initFormatParams();
    }
    /**
     * 初始化格式化参数
     *
     * @param {*} value
     * @memberof TextboxEditor
     */
    initFormatParams() {
        var _a, _b, _c, _d, _e;
        this.customProps.valueFormat = '';
        let unitName = (_a = this.parentItem) === null || _a === void 0 ? void 0 : _a.unitName;
        let appDeField = (_c = (_b = this.parentItem) === null || _b === void 0 ? void 0 : _b.getPSAppDEField) === null || _c === void 0 ? void 0 : _c.call(_b);
        if (appDeField === null || appDeField === void 0 ? void 0 : appDeField.stdDataType) {
            this.customProps.dataType = DataTypes.toString(appDeField.stdDataType);
        }
        if (appDeField === null || appDeField === void 0 ? void 0 : appDeField.valueFormat) {
            this.customProps.valueFormat = appDeField === null || appDeField === void 0 ? void 0 : appDeField.valueFormat;
        }
        if (this.valueFormat) {
            this.customProps.valueFormat = this.valueFormat;
        }
        if ((_d = this.editorInstance.editorParams) === null || _d === void 0 ? void 0 : _d.valueFormat) {
            this.customProps.valueFormat = (_e = this.editorInstance.editorParams) === null || _e === void 0 ? void 0 : _e.valueFormat;
        }
        if (unitName) {
            this.customProps.unitName = unitName;
        }
    }
    /**
     * 编辑器change事件
     *
     * @param {*} value
     * @memberof TextboxEditor
     */
    handleChange($event) {
        this.editorChange({ name: this.editorInstance.name, value: $event });
    }
    /**
     * 编辑器enter事件
     *
     * @param {*} value
     * @memberof TextboxEditor
     */
    handleEnter($event) {
        this.$emit('enter', arguments);
    }
    /**
     * 绘制默认内容
     *
     * @returns {*}
     * @memberof TextboxEditor
     */
    renderTextbox() {
        return this.$createElement(this.editorComponentName, {
            props: Object.assign({ name: this.editorInstance.name, itemValue: this.value, disabled: this.disabled }, this.customProps),
            on: {
                change: this.handleChange,
                enter: this.handleEnter,
            },
            style: this.customStyle,
        });
    }
    /**
     * 绘制默认内容
     *
     * @returns {*}
     * @memberof TextboxEditor
     */
    renderParameterDefinition() {
        return this.$createElement(this.editorComponentName, {
            props: Object.assign({ name: this.editorInstance.name, value: this.value, disabled: this.disabled }, this.customProps),
            on: {
                'data-change': this.handleChange,
                enter: this.handleEnter,
            },
            style: this.customStyle,
        });
    }
    /**
     * 绘制内置插件内容
     *
     * @returns {*}
     * @memberof TextboxEditor
     */
    renderTextboxColorPicker() {
        return this.$createElement(this.editorComponentName, {
            props: Object.assign({ name: this.editorInstance.name, value: this.value, data: this.contextData }, this.customProps),
            on: {
                change: this.handleChange,
                colorChange: ($event) => {
                    this.editorChange($event);
                },
            },
            style: this.customStyle,
        });
    }
    /**
     * 绘制工作流审批组件
     *
     * @returns {*}
     * @memberof TextareaEditor
     */
    renderWfapproval(editorTypeStyle) {
        var _a, _b, _c;
        const param = {
            context: this.context,
            viewparams: this.viewparams,
            appEntityCodeName: (_c = (_b = (_a = this.containerCtrl) === null || _a === void 0 ? void 0 : _a.getPSAppDataEntity) === null || _b === void 0 ? void 0 : _b.call(_a)) === null || _c === void 0 ? void 0 : _c.codeName,
        };
        if (Object.is(editorTypeStyle, 'TEXTAREA_WFAPPROVAL') || Object.is(editorTypeStyle, 'TEXTAREA_WFAPPROVALEXTENDTIMELINE')) {
            Object.assign(param, {
                value: this.value,
            });
        }
        return this.$createElement(this.editorComponentName, {
            props: param,
            on: { change: this.handleChange },
            style: this.customStyle,
        });
    }
    /**
     * 绘制内容
     *
     * @returns {*}
     * @memberof TextboxEditor
     */
    render() {
        var _a, _b, _c, _d;
        if (!this.editorIsLoaded) {
            return null;
        }
        const { editorType: type, editorStyle: style } = this.editorInstance;
        const editorTypeStyle = `${type}${style && style != 'DEFAULT' ? '_' + style : ''}`;
        switch (editorTypeStyle) {
            case 'TEXTBOX':
            case 'PASSWORD':
            case 'TEXTAREA':
            case 'TEXTAREA_10':
            case 'NUMBER':
                if ((_a = this.editorInstance.editorParams) === null || _a === void 0 ? void 0 : _a['max']) {
                    this.customProps.max = parseInt((_b = this.editorInstance.editorParams) === null || _b === void 0 ? void 0 : _b['max']);
                }
                if ((_c = this.editorInstance.editorParams) === null || _c === void 0 ? void 0 : _c['min']) {
                    this.customProps.min = parseInt((_d = this.editorInstance.editorParams) === null || _d === void 0 ? void 0 : _d['min']);
                }
            case 'MARKDOWN':
                return this.renderTextbox();
            case 'TEXTBOX_COLORPICKER':
                return this.renderTextboxColorPicker();
            case 'TEXTAREA_WFAPPROVAL':
            case 'TEXTAREA_WFAPPROVALTIMELINE':
            case 'TEXTAREA_WFAPPROVALEXTENDTIMELINE':
                return this.renderWfapproval(editorTypeStyle);
            case 'TEXTAREA_ParameterDefinition':
                return this.renderParameterDefinition();
        }
    }
};
__decorate([
    Prop()
], TextboxEditor.prototype, "isDebounce", void 0);
TextboxEditor = __decorate([
    Component({}),
    VueLifeCycleProcessing()
], TextboxEditor);
export default TextboxEditor;
