import { __decorate } from "tslib";
import { Vue, Component, Prop } from 'vue-property-decorator';
import { isEmpty, isNil } from 'ramda';
import { getControl } from '@ibizstudio-ex/runtime';
/**
 * 属性表单呈现容器
 *
 * @export
 * @class PropertyFormContainer
 * @extends {Vue}
 */
let PropertyFormContainer = class PropertyFormContainer extends Vue {
    constructor() {
        super(...arguments);
        /**
         * 当前激活部件
         *
         * @type {{}}
         * @memberof PropertyFormContainer
         */
        this.activateControl = {};
        /**
         * 当前选中数据
         *
         * @type {*}
         * @memberof PropertyFormContainer
         */
        this.selectData = null;
        /**
         * 选中上下文
         *
         * @author chitanda
         * @date 2022-11-23 12:11:10
         * @type {(IContext | null)}
         */
        this.selectContext = null;
    }
    created() {
        this.selectChange = this.selectChange.bind(this);
        this.reload = this.reload.bind(this);
        const { select } = this.ctx.ctrl;
        select.evt.on('change', this.selectChange);
        this.ctx.evt.on('reload', this.reload);
        if (select.data) {
            this.selectChange(select.data);
        }
    }
    /**
     * 重新加载
     *
     * @memberof PropertyFormContainer
     */
    reload() {
        this.selectChange(this.ctx.ctrl.select.data);
    }
    /**
     * 实例销毁前
     *
     * @memberof PropertyFormContainer
     */
    destroyed() {
        this.ctx.evt.off('reload', this.reload);
        this.ctx.ctrl.select.evt.off('change', this.selectChange);
    }
    /**
     * 选中数据变更
     *
     * @author chitanda
     * @date 2022-11-23 12:11:38
     * @param {IEntity} [data]
     * @return {*}  {Promise<void>}
     */
    async selectChange(data) {
        const { context } = this.ctx.ctrl.select;
        if (context) {
            this.selectContext = context;
        }
        if (isNil(data) || isEmpty(data)) {
            this.selectData = {};
            this.activateControl = {};
            this.$forceUpdate();
        }
        else {
            this.selectData = data;
            const ctrlModel = getControl(this.viewModel, data.itemtype, true);
            if (ctrlModel) {
                const ctrlName = ctrlModel === null || ctrlModel === void 0 ? void 0 : ctrlModel.name.toLowerCase();
                this.activateControl = {
                    key: ctrlName,
                    ctrl: ctrlModel,
                    srfkey: data === null || data === void 0 ? void 0 : data.srfkey,
                    entity: (await ctrlModel.getPSAppDataEntity().fill()),
                };
                this.$forceUpdate();
                if (ctrlModel) {
                    this.$nextTick(() => {
                        this.formLoad(ctrlName, {});
                    });
                }
            }
        }
    }
    /**
     * 表单加载数据
     *
     * @param {string} tag
     * @param {*} data
     * @param {number} [count=0]
     * @return {*}  {void}
     * @memberof PropertyFormContainer
     */
    formLoad(tag, data, count = 0) {
        if (count > 100) {
            return;
        }
        count++;
        setTimeout(() => {
            var _a;
            const ref = this.$refs[tag];
            if (((_a = ref === null || ref === void 0 ? void 0 : ref.ctrl) === null || _a === void 0 ? void 0 : _a.controlIsLoaded) == true) {
                ref.ctrl.load(data);
            }
            else {
                this.formLoad(tag, data, count);
            }
        }, 50);
    }
    /**
     * 绘制函数
     *
     * @param {CreateElement} h
     * @memberof PropertyFormContainer
     */
    render(h) {
        if (!this.activateControl.ctrl || !this.viewModel) {
            return null;
        }
        const dynamicProps = {
            context: Object.assign(Object.assign(Object.assign({}, this.ctx.context), this.selectContext), { [this.activateControl.entity.codeName.toLowerCase()]: this.activateControl.srfkey }),
            designCtx: Object.assign({}, this.ctx),
        };
        const staticProps = {};
        Object.defineProperty(staticProps, 'modelData', {
            enumerable: false,
            value: this.activateControl.ctrl,
        });
        return h('app-control-shell', {
            staticClass: this.activateControl.key,
            props: {
                staticProps,
                dynamicProps,
            },
            key: this.activateControl.key,
            ref: this.activateControl.key,
        });
    }
};
__decorate([
    Prop()
], PropertyFormContainer.prototype, "ctx", void 0);
__decorate([
    Prop()
], PropertyFormContainer.prototype, "viewModel", void 0);
PropertyFormContainer = __decorate([
    Component({})
], PropertyFormContainer);
export { PropertyFormContainer };
