import * as __WEBPACK_EXTERNAL_MODULE_lodash__ from "lodash";
import * as __WEBPACK_EXTERNAL_MODULE__collection_field_interface_index_mjs__ from "../collection-field-interface/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__collection_template_index_mjs__ from "../collection-template/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__DataSource_mjs__ from "./DataSource.mjs";
function _define_property(obj, key, value) {
    if (key in obj) Object.defineProperty(obj, key, {
        value: value,
        enumerable: true,
        configurable: true,
        writable: true
    });
    else obj[key] = value;
    return obj;
}
const DEFAULT_DATA_SOURCE_KEY = 'main';
const DEFAULT_DATA_SOURCE_TITLE = '{{t("Main")}}';
class DataSourceManager {
    addCollectionMixins() {
        let mixins = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : [];
        const newMixins = mixins.filter((mixin)=>!this.collectionMixins.includes(mixin));
        if (!newMixins.length) return;
        this.collectionMixins.push(...newMixins);
        this.getDataSources().forEach((dataSource)=>dataSource.collectionManager.reAddCollections());
    }
    getDataSources(filterCollection) {
        const allDataSources = Object.values(this.dataSourceInstancesMap);
        return filterCollection ? __WEBPACK_EXTERNAL_MODULE_lodash__["default"].filter(allDataSources, filterCollection) : allDataSources;
    }
    getDataSource(key) {
        return key ? this.dataSourceInstancesMap[key] : this.dataSourceInstancesMap[DEFAULT_DATA_SOURCE_KEY];
    }
    removeDataSources(keys) {
        keys.forEach((key)=>{
            delete this.dataSourceInstancesMap[key];
        });
    }
    addDataSource(DataSource, options) {
        const dataSourceInstance = new DataSource(options, this);
        this.dataSourceInstancesMap[dataSourceInstance.key] = dataSourceInstance;
        return dataSourceInstance;
    }
    async addDataSources(request, DataSource) {
        if (this.multiDataSources.some((param)=>{
            let [req, DS] = param;
            return req === request && DS === DataSource;
        })) return;
        this.multiDataSources.push([
            request,
            DataSource
        ]);
    }
    getAllCollections() {
        let options = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : {};
        const { filterCollection, filterDataSource } = options;
        return this.getDataSources(filterDataSource).reduce((acc, dataSource)=>{
            acc.push({
                ...dataSource.getOptions(),
                collections: dataSource.collectionManager.getCollections(filterCollection)
            });
            return acc;
        }, []);
    }
    addFieldInterfaceGroups(options) {
        this.collectionFieldInterfaceManager.addFieldInterfaceGroups(options);
    }
    addCollectionTemplates() {
        let templateClasses = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : [];
        this.collectionTemplateManager.addCollectionTemplates(templateClasses);
    }
    addFieldInterfaces() {
        let fieldInterfaceClasses = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : [];
        this.collectionFieldInterfaceManager.addFieldInterfaces(fieldInterfaceClasses);
    }
    async reload() {
        await Promise.all(this.multiDataSources.map(async (param)=>{
            let [request, DataSource] = param;
            const list = await request();
            list.map((options)=>this.addDataSource(DataSource, options));
        }));
        return Promise.all(this.getDataSources().map((dataSource)=>dataSource.reload()));
    }
    constructor(options = {}, app){
        _define_property(this, "options", void 0);
        _define_property(this, "app", void 0);
        _define_property(this, "dataSourceInstancesMap", void 0);
        _define_property(this, "multiDataSources", void 0);
        _define_property(this, "collectionMixins", void 0);
        _define_property(this, "collectionTemplateManager", void 0);
        _define_property(this, "collectionFieldInterfaceManager", void 0);
        this.options = options;
        this.app = app;
        this.dataSourceInstancesMap = {};
        this.multiDataSources = [];
        this.collectionMixins = [];
        this.collectionTemplateManager = new __WEBPACK_EXTERNAL_MODULE__collection_template_index_mjs__.CollectionTemplateManager(options.collectionTemplates, this);
        this.collectionFieldInterfaceManager = new __WEBPACK_EXTERNAL_MODULE__collection_field_interface_index_mjs__.CollectionFieldInterfaceManager(options.fieldInterfaces, options.fieldInterfaceGroups, this);
        this.collectionMixins.push(...options.collectionMixins || []);
        this.addDataSource(__WEBPACK_EXTERNAL_MODULE__DataSource_mjs__.LocalDataSource, {
            key: DEFAULT_DATA_SOURCE_KEY,
            displayName: DEFAULT_DATA_SOURCE_TITLE,
            collections: options.collections || []
        });
        (options.dataSources || []).forEach((dataSourceOptions)=>{
            this.addDataSource(__WEBPACK_EXTERNAL_MODULE__DataSource_mjs__.LocalDataSource, dataSourceOptions);
        });
    }
}
export { DEFAULT_DATA_SOURCE_KEY, DEFAULT_DATA_SOURCE_TITLE, DataSourceManager };
