"use strict";
var __webpack_exports__ = {};
const client_namespaceObject = require("@tachybase/client");
var collections_namespaceObject = JSON.parse('[{"key":"h7b9i8khc3q","name":"users","inherit":false,"hidden":false,"description":null,"category":[],"namespace":"users.users","duplicator":{"dumpable":"optional","with":"rolesUsers"},"sortable":"sort","model":"UserModel","createdBy":true,"updatedBy":true,"logging":true,"from":"db2cm","title":"{{t(\\"Users\\")}}","rawTitle":"{{t(\\"Users\\")}}","fields":[{"uiSchema":{"type":"number","title":"{{t(\\"ID\\")}}","x-component":"InputNumber","x-read-pretty":true,"rawTitle":"{{t(\\"ID\\")}}"},"key":"ffp1f2sula0","name":"id","type":"bigInt","interface":"id","description":null,"collectionName":"users","parentKey":null,"reverseKey":null,"autoIncrement":true,"primaryKey":true,"allowNull":false},{"uiSchema":{"type":"string","title":"{{t(\\"Nickname\\")}}","x-component":"Input","rawTitle":"{{t(\\"Nickname\\")}}"},"key":"vrv7yjue90g","name":"nickname","type":"string","interface":"input","description":null,"collectionName":"users","parentKey":null,"reverseKey":null},{"uiSchema":{"type":"string","title":"{{t(\\"Username\\")}}","x-component":"Input","x-validator":{"username":true},"required":true,"rawTitle":"{{t(\\"Username\\")}}"},"key":"2ccs6evyrub","name":"username","type":"string","interface":"input","description":null,"collectionName":"users","parentKey":null,"reverseKey":null,"unique":true},{"uiSchema":{"type":"string","title":"{{t(\\"Email\\")}}","x-component":"Input","x-validator":"email","required":true,"rawTitle":"{{t(\\"Email\\")}}"},"key":"rrskwjl5wt1","name":"email","type":"string","interface":"email","description":null,"collectionName":"users","parentKey":null,"reverseKey":null,"unique":true},{"key":"t09bauwm0wb","name":"roles","type":"belongsToMany","interface":"m2m","description":null,"collectionName":"users","parentKey":null,"reverseKey":null,"target":"roles","foreignKey":"userId","otherKey":"roleName","onDelete":"CASCADE","sourceKey":"id","targetKey":"name","through":"rolesUsers","uiSchema":{"type":"array","title":"{{t(\\"Roles\\")}}","x-component":"AssociationField","x-component-props":{"multiple":true,"fieldNames":{"label":"title","value":"name"}}}},{"key":"1pz0art9mt7","name":"f_n2fu6hvprct","type":"string","interface":"select","description":null,"collectionName":"t_vwpds9fs4xs","parentKey":null,"reverseKey":null,"uiSchema":{"enum":[{"value":"test1","label":"test1"},{"value":"test2","label":"test2"}],"type":"string","x-component":"Select","title":"test"}}]},{"key":"pqnenvqrzxr","name":"roles","inherit":false,"hidden":false,"description":null,"category":[],"namespace":"acl.acl","duplicator":{"dumpable":"required","with":"uiSchemas"},"autoGenId":false,"model":"RoleModel","filterTargetKey":"name","sortable":true,"from":"db2cm","title":"{{t(\\"Roles\\")}}","rawTitle":"{{t(\\"Roles\\")}}","fields":[{"uiSchema":{"type":"string","title":"{{t(\\"Role UID\\")}}","x-component":"Input","rawTitle":"{{t(\\"Role UID\\")}}"},"key":"jbz9m80bxmp","name":"name","type":"uid","interface":"input","description":null,"collectionName":"roles","parentKey":null,"reverseKey":null,"prefix":"r_","primaryKey":true},{"uiSchema":{"type":"string","title":"{{t(\\"Role name\\")}}","x-component":"Input","rawTitle":"{{t(\\"Role name\\")}}"},"key":"faywtz4sf3u","name":"title","type":"string","interface":"input","description":null,"collectionName":"roles","parentKey":null,"reverseKey":null,"unique":true,"translation":true},{"key":"1enkovm9sye","name":"description","type":"string","interface":null,"description":null,"collectionName":"roles","parentKey":null,"reverseKey":null}]}]');
describe('DataSourceManager', ()=>{
    describe('mixins', ()=>{
        test('init should work', ()=>{
            class DemoCollectionMixin extends client_namespaceObject.Collection {
                a() {
                    return 'test-' + this.name;
                }
            }
            const app = new client_namespaceObject.Application({
                dataSourceManager: {
                    collections: collections_namespaceObject,
                    collectionMixins: [
                        DemoCollectionMixin
                    ]
                }
            });
            const user = app.getCollectionManager().getCollection('users');
            expect(user.a()).toBe('test-users');
        });
        test('plugin should work', async ()=>{
            class DemoCollectionMixin extends client_namespaceObject.Collection {
                b() {
                    return 'test-' + this.name;
                }
            }
            class MyPlugin extends client_namespaceObject.Plugin {
                async load() {
                    this.app.getCollectionManager().addCollections(collections_namespaceObject);
                    this.app.dataSourceManager.addCollectionMixins([
                        DemoCollectionMixin
                    ]);
                }
            }
            const app = new client_namespaceObject.Application({
                plugins: [
                    MyPlugin
                ]
            });
            await app.load();
            const user = app.getCollectionManager().getCollection('users');
            expect(user.b()).toBe('test-users');
        });
        test('multiple mixins should work', ()=>{
            class DemoCollectionMixin1 extends client_namespaceObject.Collection {
                c() {
                    return 'test1-' + this.name;
                }
            }
            class DemoCollectionMixin2 extends client_namespaceObject.Collection {
                d() {
                    return 'test2-' + this.name;
                }
            }
            const app = new client_namespaceObject.Application({
                dataSourceManager: {
                    collections: collections_namespaceObject,
                    collectionMixins: [
                        DemoCollectionMixin1,
                        DemoCollectionMixin2
                    ]
                }
            });
            const user = app.getCollectionManager().getCollection('users');
            expect(user.c()).toBe('test1-users');
            expect(user.d()).toBe('test2-users');
        });
        test('after add mixins, collection should be re-instantiated', ()=>{
            class DemoCollectionMixin extends client_namespaceObject.Collection {
                e() {
                    return 'test-' + this.name;
                }
            }
            const app = new client_namespaceObject.Application({
                dataSourceManager: {
                    collections: collections_namespaceObject
                }
            });
            const user = app.getCollectionManager().getCollection('users');
            expect(user.e).toBeUndefined();
            app.dataSourceManager.addCollectionMixins([
                DemoCollectionMixin
            ]);
            const user2 = app.getCollectionManager().getCollection('users');
            expect(user2.e()).toBe('test-users');
        });
    });
    describe('getAllCollections', ()=>{
        test('getAllCollections() should work', ()=>{
            const app = new client_namespaceObject.Application({
                dataSourceManager: {
                    dataSources: [
                        {
                            key: 'a',
                            displayName: 'a'
                        }
                    ]
                }
            });
            app.getCollectionManager().addCollections(collections_namespaceObject);
            app.getCollectionManager('a').addCollections([
                collections_namespaceObject["0"]
            ]);
            const allCollections = app.dataSourceManager.getAllCollections();
            expect(allCollections.length).toBe(2);
            expect(allCollections[0].key).toBe(client_namespaceObject.DEFAULT_DATA_SOURCE_KEY);
            expect(allCollections[1].key).toBe('a');
            expect(allCollections[0].collections.length).toBe(2);
            expect(allCollections[1].collections.length).toBe(1);
        });
    });
    describe('addDataSource', ()=>{
        test('should add a data source', ()=>{
            const app = new client_namespaceObject.Application();
            const dataSourceManager = app.dataSourceManager;
            expect(dataSourceManager.getDataSources()).toHaveLength(1);
            const dataSource = dataSourceManager.addDataSource(client_namespaceObject.LocalDataSource, {
                key: 'test',
                displayName: 'Test'
            });
            expect(dataSourceManager.getDataSources()).toHaveLength(2);
            expect(dataSourceManager.getDataSource('test')).toBe(dataSource);
        });
    });
    describe('removeDataSources', ()=>{
        test('should remove data sources', ()=>{
            const app = new client_namespaceObject.Application();
            const dataSourceManager = app.dataSourceManager;
            dataSourceManager.addDataSource(client_namespaceObject.LocalDataSource, {
                key: 'test1',
                displayName: 'Test 1'
            });
            dataSourceManager.addDataSource(client_namespaceObject.LocalDataSource, {
                key: 'test2',
                displayName: 'Test 2'
            });
            expect(dataSourceManager.getDataSources()).toHaveLength(3);
            dataSourceManager.removeDataSources([
                'test1'
            ]);
            expect(dataSourceManager.getDataSources()).toHaveLength(2);
            expect(dataSourceManager.getDataSource('test1')).toBeUndefined();
            expect(dataSourceManager.getDataSource('test2')).toBeDefined();
        });
    });
    describe('reload', ()=>{
        test('should reload data sources', async ()=>{
            const app = new client_namespaceObject.Application();
            const dataSourceManager = app.dataSourceManager;
            const dataSource1 = dataSourceManager.addDataSource(client_namespaceObject.LocalDataSource, {
                key: 'test1',
                displayName: 'Test 1'
            });
            const dataSource2 = dataSourceManager.addDataSource(client_namespaceObject.LocalDataSource, {
                key: 'test2',
                displayName: 'Test 2'
            });
            const reloadSpy1 = vitest.spyOn(dataSource1, 'reload');
            const reloadSpy2 = vitest.spyOn(dataSource2, 'reload');
            await dataSourceManager.reload();
            expect(reloadSpy1).toHaveBeenCalledTimes(1);
            expect(reloadSpy2).toHaveBeenCalledTimes(1);
        });
    });
});
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
});
