import {DataSourceFactory} from "zhc-birt-core";
// import DataSourceFactory from "@birt/components/DataSource/DataSourceFactory";
import {WebSocketConection} from "zhc-birt-core";

export default {
    data() {
        return {
            datasource: {},
        };
    },
    provide() {
        return { datasource: this.datasource };
    },
    computed: {
        confDataSources() {
            return this.conf && this.conf.dataSources;
        },
    },
    methods: {
        isInitDataSources() {
            if (this.confDataSources) {
                this.initDataSources(this.confDataSources);
                return true;
            }
            return false;
        },
        // 实例化工作薄数据源对象
        initDataSources(dataSources) {
            if (dataSources) {
                let me = this;
                dataSources.forEach((ds) => {
                    if (!me.datasource[ds.controlId]) {
                        ds.dataset = me.dataset;
                        me.datasource[ds.controlId] = DataSourceFactory.newInstance(ds);
                    }
                });
            }
        },
    },
    beforeDestroy() {
        for (var key in this.datasource) {
            let obj = this.datasource[key];
            if (obj instanceof WebSocketConection) {
                obj.close();
            }
        }
    },
}