import { dataSourceWorker } from '../../workers/data-source';
import { BaseDataSource } from './BaseDataSource';
export class SimpleDataSource extends BaseDataSource {
    constructor(data) {
        super(data);
        this.processor = dataSourceWorker().publicApi;
    }
    /**
     * Entry point to getting data
     */
    async fetchData(options) {
        return this.paginate(options, this.sort(options, this.load(options)));
    }
    /**
     * Fits the data source API
     */
    async load(options) {
        return {
            data: this.data,
            filtered: !options && this.filtered ? this.filtered : this.data,
            total: !options && this.filtered ? this.filtered.length : this.data.length
        };
    }
    /**
     * Paginate the complete data set
     */
    async paginate(options, data) {
        let loaded = await data || await this.load(options);
        if (loaded.data) {
            this.data = loaded.data;
        }
        if (loaded.filtered) {
            this.filtered = loaded.filtered;
            loaded = { data: loaded.filtered, total: loaded.total };
        }
        if (!options.range) {
            return loaded;
        }
        return {
            data: loaded.data.slice(options.range[0], options.range[0] + options.size),
            total: loaded.total
        };
    }
    async sort(options, data) {
        if (data) {
            return (options.sort || options.filter || options.group) ? this.processData(options, data) : data;
        }
        return this.paginate(options, this.processData(options, this.load(options)));
    }
    async processData(options, waiting) {
        const data = await waiting;
        return this.processor(data, options);
    }
}
