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

export default {
    data() {
        return {
            dataset: {},

        };
    },
    provide() {
        return { dataset: this.dataset };
    },
    computed: {
        // 获取数据集请求地址,从this.$HCBIRT.loadUrl
        loadUrl() {
            return this.conf.loadUrl || "/data/querys";
        }
    },
    methods: {
        // 判断workbook表单是否有修改
        isDirty() {
            for (let dsId in this.dataset) {
                let dsInsc = this.dataset[dsId];
                if (dsInsc && dsInsc.isDirty && dsInsc.isDirty()) {
                    return true;
                }
            }
            return false;
        },
        // 初始化工作表数据集
        _initDataSets(dataSets) {
            if (dataSets) {
                let newStores = [];
                let me = this;
                dataSets.forEach((ds, i) => {
                    if (!me.dataset[ds.controlId]) {
                        debugger
                        me.dataset[ds.controlId] = this._createDataSetInsc(ds);
                        me.dataset[ds.controlId].$i = i;
                        newStores.push(ds.controlId);
                    }
                });
                this._setSlaveDatasetInsc();
                this._applyPushParam(this.param, newStores);
            }
        },
        // 获取从表数据集实例
        _getSlaveDataSetInsc(slaveConf) {
            if (slaveConf) {
                let dsInsc = [];
                slaveConf.forEach(sci => {
                    let dsName = sci.name;
                    dsInsc = dsInsc.concat(this.getStoreByName(dsName));
                })
                return dsInsc;
            }
        },
        // 创建数据集实例
        _createDataSetInsc(dsConf) {
            debugger
            let dsInsc = DataSourceFactory.newInstance(
                Object.assign(dsConf, { axios: this.axios, saveUrl: this.saveUrl })
            );
            return dsInsc;
        },
        // 设置数据集主从关系
        _setSlaveDatasetInsc() {
            debugger
            for (let dsId in this.dataset) {
                let dsInsc = this.dataset[dsId];
                if (dsInsc.getSlaveDataset) {
                    let slaveDs = dsInsc.getSlaveDataset()
                    let slaveDsInsc = this._getSlaveDataSetInsc(slaveDs);
                    dsInsc.setSlaveDatasetInsc(slaveDsInsc);
                }
            }
        },
        // 根据键值获取数据集配置
        _getDatasetConf(name, key) {
            let isId = (name == "controlId");
            let sheets = this.getSheets();
            let retuDs = [];
            for (let i = 0, l = sheets.length; i < l; i++) {
                let sht = sheets[i];
                let shtSets = sht.dataSets;
                if (shtSets) {
                    for (let j = 0, m = shtSets.length; j < m; j++) {
                        let ds = shtSets[j];
                        if (ds[name] == key) {
                            if (isId) {
                                return ds;
                            } else {
                                retuDs.push(ds);
                            }
                        }
                    }
                }
            }
            return !isId && retuDs;
        },
        // 根据数据集Id获取数据集对象实例
        getDataSetInstance(datasetId) {
            return this.dataset[datasetId];
        },
        // 获取数据集列表
        getStores() {
            let stores = [];
            for (var dsId in this.dataset) {
                stores.push(this.dataset[dsId]);
            }
            return stores;
        },
        // 根据数据集id,获取数据集对象
        getStore(dsId) {
            if (this.dataset[dsId]) {
                return this.dataset[dsId];
            }
            let dsConf = this._getDatasetConf("controlId", dsId);
            if (dsConf) {
                let dsInsc = this._createDataSetInsc(dsConf);
                this.dataset[dsConf.controlId] = dsInsc;
                return dsInsc;
            }
            // this.getSheets().forEach(sheet => {
            //     sheet.dataSets && sheet.dataSets.forEach(ds => {
            //         if (ds.controlId == dsId) {
            //             this.dataset[ds.controlId] = this._createDataSetInsc(ds);
            //             return ds;
            //         }
            //     })
            // })
        },
        // 根据数据name,获取数据集对象
        getStoreByName(dsName) {
            let dsConf = this._getDatasetConf("name", dsName);
            let dsInsc = [];
            dsConf.forEach(dsf => {
                let di = this.getStore(dsf.controlId);
                if (di) {
                    dsInsc.push(di);
                }
            })
            return dsInsc;
        },
        // 获取数据集自动接收条件
        getReceiveCdions(param, newStores) {
            let receiveCdions = [];
            for (let dsId in this.dataset) {
                // 当有标记新创建数据集时，只有新创建数据集应用下推,避免旧数据集重复应用下推
                if (newStores && (newStores.indexOf(dsId) == -1)) continue;
                let ds = this.dataset[dsId];
                let recvCdion = ds && ds.getReceiveCdion(param);
                if (recvCdion) {
                    let i = ds.$i;
                    receiveCdions.splice(i, 0, recvCdion);
                }
            }
            return receiveCdions;
        }
    }
}