import * as __WEBPACK_EXTERNAL_MODULE__tachybase_client__ from "@tachybase/client";
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;
}
describe('CollectionTemplateManager', ()=>{
    let collectionTemplateManager;
    beforeEach(()=>{
        const app = new __WEBPACK_EXTERNAL_MODULE__tachybase_client__.Application();
        collectionTemplateManager = app.dataSourceManager.collectionTemplateManager;
    });
    it('adds new templates', ()=>{
        class A extends __WEBPACK_EXTERNAL_MODULE__tachybase_client__.CollectionTemplate {
            constructor(...args){
                super(...args), _define_property(this, "name", 'a');
            }
        }
        collectionTemplateManager.addCollectionTemplates([
            A
        ]);
        expect(collectionTemplateManager.getCollectionTemplate('a')).instanceOf(A);
        expect(collectionTemplateManager.getCollectionTemplates()).toHaveLength(1);
        class B extends __WEBPACK_EXTERNAL_MODULE__tachybase_client__.CollectionTemplate {
            constructor(...args){
                super(...args), _define_property(this, "name", 'b');
            }
        }
        collectionTemplateManager.addCollectionTemplates([
            B
        ]);
        expect(collectionTemplateManager.getCollectionTemplate('b')).instanceOf(B);
        expect(collectionTemplateManager.getCollectionTemplates()).toHaveLength(2);
    });
    it('overrides duplicates', ()=>{
        class A extends __WEBPACK_EXTERNAL_MODULE__tachybase_client__.CollectionTemplate {
            constructor(...args){
                super(...args), _define_property(this, "name", 'a');
            }
        }
        class B extends __WEBPACK_EXTERNAL_MODULE__tachybase_client__.CollectionTemplate {
            constructor(...args){
                super(...args), _define_property(this, "name", 'a');
            }
        }
        collectionTemplateManager.addCollectionTemplates([
            A,
            B
        ]);
        expect(collectionTemplateManager.getCollectionTemplate('a')).instanceOf(B);
        expect(collectionTemplateManager.getCollectionTemplates()).toHaveLength(1);
    });
    it('should re-add collections', ()=>{
        class CollectionTest extends __WEBPACK_EXTERNAL_MODULE__tachybase_client__.Collection {
        }
        class A extends __WEBPACK_EXTERNAL_MODULE__tachybase_client__.CollectionTemplate {
            constructor(...args){
                super(...args), _define_property(this, "name", 'a'), _define_property(this, "Collection", CollectionTest);
            }
        }
        const collectionA = {
            name: 'collectionA',
            template: 'a'
        };
        collectionTemplateManager.dataSourceManager.getDataSource().collectionManager.addCollections([
            collectionA
        ]);
        expect(collectionTemplateManager.dataSourceManager.getDataSource().collectionManager.getCollection('collectionA')).instanceOf(__WEBPACK_EXTERNAL_MODULE__tachybase_client__.Collection);
        collectionTemplateManager.addCollectionTemplates([
            A
        ]);
        expect(collectionTemplateManager.dataSourceManager.getDataSource().collectionManager.getCollection('collectionA')).instanceOf(CollectionTest);
    });
    describe('getCollectionTemplates', ()=>{
        beforeEach(()=>{
            class A extends __WEBPACK_EXTERNAL_MODULE__tachybase_client__.CollectionTemplate {
                constructor(...args){
                    super(...args), _define_property(this, "name", 'a'), _define_property(this, "supportDataSourceType", [
                        'sourceA'
                    ]);
                }
            }
            class B extends __WEBPACK_EXTERNAL_MODULE__tachybase_client__.CollectionTemplate {
                constructor(...args){
                    super(...args), _define_property(this, "name", 'b'), _define_property(this, "notSupportDataSourceType", [
                        'sourceA'
                    ]);
                }
            }
            class C extends __WEBPACK_EXTERNAL_MODULE__tachybase_client__.CollectionTemplate {
                constructor(...args){
                    super(...args), _define_property(this, "name", 'c');
                }
            }
            collectionTemplateManager.addCollectionTemplates([
                A,
                B,
                C
            ]);
        });
        it('should return all stored templates when no dataSourceType is provided', ()=>{
            const templates = collectionTemplateManager.getCollectionTemplates();
            expect(templates.length).toBe(3);
        });
        it('should return supported templates when dataSourceType is provided', ()=>{
            const templates = collectionTemplateManager.getCollectionTemplates('sourceA');
            expect(templates.length).toBe(2);
            expect(templates[0].name).toBe('a');
            expect(templates[1].name).toBe('c');
        });
        it('should return empty array when an unsupported dataSourceType is provided', ()=>{
            const templates = collectionTemplateManager.getCollectionTemplates('sourceB');
            expect(templates.length).toBe(2);
            expect(templates[0].name).toBe('b');
            expect(templates[1].name).toBe('c');
        });
    });
});
