/**
 * 数据源最佳处理方案: 
 *  a. 多实例模式下,如果单靠Api模式使用,那一定变的复杂,所以pro-dataSource组件将会代替完成多实例场景
 *  b. 单例模式下建议直接使用dataSource mixins
 * @author xiufu.wang
 */

export default {
    name: 'ProDatasource',
    componentName: 'ProDatasource',
    props: {
        dataSource: Object,
        // 可以指定token前缀
        dataSourceKey: String
    },
    created() {
        this.isDataSource = true
        if (this.dataSource) {
            //获取实例token
            this._dataSource = this.dataSource.clone(this.dataSourceKey)
        }
    },
    computed: {
        datas() {
            return this._dataSource ? this._dataSource.datas : null
        },
        formatDatas() {
            return this._dataSource.handleFormatDatas(this.datas, this._dataSource.mergeOption)
        },
        mergeOption() {
            return this._dataSource.mergeOption
        },
        loading() {
            return this._dataSource.loading
        },
        pagination() {
            return this._dataSource.pagination
        }
    },
    methods: {
        resetDatas(fn) { 
            return this._dataSource.resetDatas(fn)
        },
        updateOption(noptions, ntarget){ 
            this._dataSource.loadDatas(noptions, ntarget)
        },
        loadDatas(extParams) {
            if (this._dataSource) {
                return this._dataSource.loadDatas(extParams)
            }
        },
        getInited() {
            if (this._dataSource) {
                return this._dataSource.getInited()
            }
        },
        clearDatas(resetData) {
            this._dataSource.clearDatas(resetData)
        },
        fetchSuggestions(queryString, cb) {
            this._dataSource.fetchSuggestions(queryString, cb)
        },
        'size-change': function (pageSize) {
            this._dataSource['size-change'](pageSize)
        },
        'current-change': function (current) {
            this._dataSource['current-change'](current)
        },
        getPrimitiveData() {
            return this._dataSource.getPrimitiveData()
        },
        getSummaryData() {
            return this._dataSource.getSummaryData()
        },
        getSpanMethod() {
            return this._dataSource.getSpanMethod()
        },
        tableAsyncTreeload(row, treeNode, cb) {
            return this._dataSource.tableAsyncTreeload(row, treeNode, cb)
        },
        treeAsyncTreeload(treeNode, cb) {
            return this._dataSource.treeAsyncTreeload(treeNode, cb)
        },
        selectQueryLoad(query) {
            return this._dataSource.selectQueryLoad(query)
        },
        cascaderAsyncloads(query) {
            return this._dataSource.cascaderAsyncloads(query)
        },
        // dataSource.invorkDataSource('customerMethod')
        invorkDataSource(method) {
            return (...args) => {
                this._dataSource[method](...args)
            }
        },

    },
    render() {
        if (this.$scopedSlots.default && this._dataSource) {
            const vnodes = this.$scopedSlots.default(this._dataSource)
            if (Array.isArray(vnodes)) {
                return vnodes[0]
            }
        }
        return null
    },
    beforeDestroy() { 
        this._dataSource.destroy()
    } 
}