"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.n = function(module) {
        var getter = module && module.__esModule ? function() {
            return module['default'];
        } : function() {
            return module;
        };
        __webpack_require__.d(getter, {
            a: getter
        });
        return getter;
    };
})();
(()=>{
    __webpack_require__.d = function(exports1, definition) {
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
(()=>{
    __webpack_require__.o = function(obj, prop) {
        return Object.prototype.hasOwnProperty.call(obj, prop);
    };
})();
(()=>{
    __webpack_require__.r = function(exports1) {
        if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
            value: 'Module'
        });
        Object.defineProperty(exports1, '__esModule', {
            value: true
        });
    };
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
    DEFAULT_DATA_SOURCE_KEY: ()=>DEFAULT_DATA_SOURCE_KEY,
    DataSourceManager: ()=>DataSourceManager,
    DEFAULT_DATA_SOURCE_TITLE: ()=>DEFAULT_DATA_SOURCE_TITLE
});
const external_lodash_namespaceObject = require("lodash");
var external_lodash_default = /*#__PURE__*/ __webpack_require__.n(external_lodash_namespaceObject);
const index_js_namespaceObject = require("../collection-field-interface/index.js");
const external_collection_template_index_js_namespaceObject = require("../collection-template/index.js");
const external_DataSource_js_namespaceObject = require("./DataSource.js");
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 ? external_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 external_collection_template_index_js_namespaceObject.CollectionTemplateManager(options.collectionTemplates, this);
        this.collectionFieldInterfaceManager = new index_js_namespaceObject.CollectionFieldInterfaceManager(options.fieldInterfaces, options.fieldInterfaceGroups, this);
        this.collectionMixins.push(...options.collectionMixins || []);
        this.addDataSource(external_DataSource_js_namespaceObject.LocalDataSource, {
            key: DEFAULT_DATA_SOURCE_KEY,
            displayName: DEFAULT_DATA_SOURCE_TITLE,
            collections: options.collections || []
        });
        (options.dataSources || []).forEach((dataSourceOptions)=>{
            this.addDataSource(external_DataSource_js_namespaceObject.LocalDataSource, dataSourceOptions);
        });
    }
}
var __webpack_export_target__ = exports;
for(var __webpack_i__ in __webpack_exports__)__webpack_export_target__[__webpack_i__] = __webpack_exports__[__webpack_i__];
if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', {
    value: true
});
