import { __decorate } from "tslib";
import { Component } from 'vue-property-decorator';
import moment from 'moment';
import { VueLifeCycleProcessing } from '../../../decorators';
import { EditorBase } from '../editor-base/editor-base';
import { Util } from '@ibizstudio/runtime';
/**
 * 日期时间选择编辑器
 *
 * @export
 * @class DatePickerEditor
 * @extends {EditorBase}
 */
let DatePickerEditor = class DatePickerEditor extends EditorBase {
    /**
     * 编辑器初始化
     *
     * @memberof DatePickerEditor
     */
    async initEditor() {
        var _a, _b;
        await super.initEditor();
        this.initValueFormat();
        this.customProps.placeholder = this.editorInstance.placeHolder || '请选择时间...';
        this.customProps.transfer = Object.is('SEARCHFORM', (_a = this.containerCtrl) === null || _a === void 0 ? void 0 : _a.controlType) ? false : true;
        this.customStyle.minWidth = '150px';
        switch ((_b = this.editorInstance) === null || _b === void 0 ? void 0 : _b.editorType) {
            // 时间选择控件
            case 'DATEPICKEREX':
                this.customProps.type = 'date';
                this.customProps.format = 'yyyy-MM-dd HH:mm:ss';
                break;
            // 时间选择控件_无小时
            case 'DATEPICKEREX_NOTIME':
                this.customProps.type = 'date';
                this.customProps.format = 'yyyy-MM-dd';
                break;
            // 时间选择器(新）
            case 'DATEPICKER':
                this.customProps.type = 'datetime';
                this.customProps.format = 'yyyy-MM-dd HH:mm:ss';
                break;
            // 时间选择控件_小时
            case 'DATEPICKEREX_HOUR':
                this.customProps.format = 'yyyy-MM-dd HH';
                break;
            // 时间选择控件_分钟
            case 'DATEPICKEREX_MINUTE':
                this.customProps.format = 'yyyy-MM-dd HH:mm';
                break;
            // 时间选择控件_秒钟
            case 'DATEPICKEREX_SECOND':
                this.customProps.format = 'yyyy-MM-dd HH:mm:ss';
                break;
            // 时间选择控件_无日期
            case 'DATEPICKEREX_NODAY':
                this.customProps.format = 'HH:mm:ss';
                break;
            // 时间选择控件_无日期无秒钟
            case 'DATEPICKEREX_NODAY_NOSECOND':
                this.customProps.format = 'HH:mm';
                break;
            // 时间选择控件_无秒钟
            case 'DATEPICKEREX_NOSECOND':
                this.customProps.format = 'yyyy-MM-dd HH:mm';
                break;
        }
    }
    /**
     * 日期格式初始化
     *
     * @memberof DatePickerEditor
     */
    initValueFormat() {
        var _a, _b, _c, _d, _e, _f;
        const entity = (_b = (_a = this.containerCtrl) === null || _a === void 0 ? void 0 : _a.getPSAppDataEntity) === null || _b === void 0 ? void 0 : _b.call(_a);
        if (entity) {
            this.valueFormat = (_f = entity.findPSAppDEField((_e = (_d = (_c = this.parentItem) === null || _c === void 0 ? void 0 : _c.getPSAppDEField) === null || _d === void 0 ? void 0 : _d.call(_c)) === null || _e === void 0 ? void 0 : _e.codeName)) === null || _f === void 0 ? void 0 : _f.valueFormat;
        }
    }
    /**
     * 编辑器change回调
     *
     * @param {{ name: string; value: any }} $event
     * @memberof DatePickerEditor
     */
    handleChange(value1, value2) {
        let tempFormat = this.customProps.format.replace('yyyy', 'YYYY').replace('dd', 'DD');
        if (this.valueFormat && this.valueFormat != tempFormat) {
            value1 = this.formatDate(value1, tempFormat, this.valueFormat);
        }
        this.editorChange({ name: this.editorInstance.name, value: value1 });
    }
    /**
     * 设置编辑器的自定义高宽
     *
     * @memberof DatePickerEditor
     */
    setCustomStyle() {
        let { editorWidth, editorHeight } = this.editorInstance;
        this.customStyle = {};
        if (!Util.isEmpty(editorWidth) && editorWidth != 0) {
            this.handleEditorWidth(editorWidth);
        }
        if (!Util.isEmpty(editorHeight) && editorHeight != 0) {
            this.customStyle.height = editorHeight > 1 ? editorHeight + 'px' : editorHeight * 100 + '%';
        }
    }
    /**
     * 根据类型处理编辑器宽度
     *
     * @memberof DatePickerEditor
     */
    handleEditorWidth(width) {
        var _a;
        switch ((_a = this.editorInstance) === null || _a === void 0 ? void 0 : _a.editorType) {
            // 时间选择控件
            case 'DATEPICKEREX':
            case 'DATEPICKER':
            case 'DATEPICKEREX_SECOND':
                this.customProps.format = 'yyyy-MM-dd HH:mm:ss';
                this.calcCustomStyle(width, 160);
                break;
            case 'DATEPICKEREX_MINUTE':
            case 'DATEPICKEREX_NOSECOND':
                this.calcCustomStyle(width, 140);
                break;
            case 'DATEPICKEREX_HOUR':
                this.calcCustomStyle(width, 120);
                break;
            case 'DATEPICKEREX_NOTIME':
                this.calcCustomStyle(width, 100);
                break;
            case 'DATEPICKEREX_NODAY':
                this.calcCustomStyle(width, 90);
                break;
            case 'DATEPICKEREX_NODAY_NOSECOND':
                this.calcCustomStyle(width, 70);
                break;
        }
    }
    /**
     * 计算自定义样式
     *
     * @param date  时间
     * @param format    格式
     * @memberof DatePickerEditor
     */
    calcCustomStyle(value, limit) {
        if (value == limit) {
            this.customStyle.width = '100%';
        }
        else {
            this.customStyle.width = value > 1 ? value + 'px' : value * 100 + '%';
        }
    }
    /**
     * 日期格式化
     *
     * @param date  时间
     * @param format    格式
     * @memberof DatePickerEditor
     */
    formatDate(date, valueFormat, typeFormat) {
        if (!typeFormat) {
            return date;
        }
        try {
            const tempDate = moment(date, valueFormat).format(typeFormat);
            if (tempDate == 'Invalid date') {
                return date;
            }
            else {
                return tempDate;
            }
        }
        catch (error) {
            return date;
        }
    }
    /**
     * 绘制内容
     *
     * @returns {*}
     * @memberof DatePickerEditor
     */
    render() {
        if (!this.editorIsLoaded) {
            return null;
        }
        return this.$createElement(this.editorComponentName, {
            props: Object.assign({ name: this.editorInstance.name, value: this.value, disabled: this.disabled }, this.customProps),
            on: { 'on-change': this.handleChange },
            style: this.customStyle,
        });
    }
};
DatePickerEditor = __decorate([
    Component({}),
    VueLifeCycleProcessing()
], DatePickerEditor);
export default DatePickerEditor;
