"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}]}]');
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('CollectionManager', ()=>{
    let collectionManager;
    beforeEach(()=>{
        const app = new client_namespaceObject.Application({});
        collectionManager = app.getCollectionManager();
    });
    describe('constructor', ()=>{
        it('should initialize with empty collections', ()=>{
            expect(collectionManager.collectionInstancesMap).toEqual({});
            expect(collectionManager.collectionInstancesArr).toEqual([]);
        });
    });
    describe('addCollections', ()=>{
        it('should add new collections', ()=>{
            const collections = [
                {
                    name: 'test'
                }
            ];
            collectionManager.addCollections(collections);
            expect(collectionManager.collectionInstancesArr.length).toBe(1);
            expect(collectionManager.collectionInstancesMap['test']).instanceOf(client_namespaceObject.Collection);
        });
        it('should merge with existing collections', ()=>{
            const initialCollections = [
                {
                    name: 'initial'
                }
            ];
            const additionalCollections = [
                {
                    name: 'additional'
                }
            ];
            collectionManager.addCollections(initialCollections);
            collectionManager.addCollections(additionalCollections);
            expect(collectionManager.collectionInstancesArr.length).toBe(2);
        });
        it('should not add duplicate collections', ()=>{
            const collections = [
                {
                    name: 'test'
                },
                {
                    name: 'test'
                }
            ];
            collectionManager.addCollections(collections);
            expect(collectionManager.collectionInstancesArr.length).toBe(1);
        });
        it('custom Collection class', ()=>{
            class CustomCollection extends client_namespaceObject.Collection {
            }
            class CustomTemplate extends client_namespaceObject.CollectionTemplate {
                constructor(...args){
                    super(...args), _define_property(this, "name", 'custom'), _define_property(this, "Collection", CustomCollection);
                }
            }
            const collections = [
                {
                    name: 'test',
                    template: 'custom'
                }
            ];
            collectionManager.dataSourceManager.addCollectionTemplates([
                CustomTemplate
            ]);
            collectionManager.addCollections(collections);
            const collection = collectionManager.getCollection('test');
            expect(collection).instanceOf(CustomCollection);
        });
        it('custom Template with transform', ()=>{
            class CustomTemplate extends client_namespaceObject.CollectionTemplate {
                transform(collection) {
                    collection.test = 'transformed';
                    return collection;
                }
                constructor(...args){
                    super(...args), _define_property(this, "name", 'custom');
                }
            }
            const collections = [
                {
                    name: 'test',
                    template: 'custom'
                }
            ];
            collectionManager.dataSourceManager.addCollectionTemplates([
                CustomTemplate
            ]);
            collectionManager.addCollections(collections);
            const collection = collectionManager.getCollection('test');
            expect(collection.getOption('test')).toBe('transformed');
        });
    });
    describe('setCollections', ()=>{
        it('should replace existing collections', ()=>{
            const initialCollections = [
                {
                    name: 'initial'
                }
            ];
            const newCollections = [
                {
                    name: 'new'
                }
            ];
            collectionManager.addCollections(initialCollections);
            collectionManager.setCollections(newCollections);
            expect(collectionManager.collectionInstancesArr.length).toBe(1);
            expect(Object.keys(collectionManager.collectionInstancesMap)).toEqual([
                'new'
            ]);
        });
    });
    describe('reAddCollections', ()=>{
        it('should re-add existing collections', ()=>{
            const initialCollections = [
                {
                    name: 'a',
                    rawTitle: 'aa'
                },
                {
                    name: 'b',
                    rawTitle: 'bb'
                }
            ];
            collectionManager.addCollections(initialCollections);
            collectionManager.getCollection('a');
            const spy = vitest.spyOn(collectionManager, 'addCollections');
            collectionManager.reAddCollections();
            expect(collectionManager.collectionInstancesArr.length).toBe(2);
            expect(spy).toHaveBeenCalledWith([
                collectionManager.getCollection('a').getOptions(),
                collectionManager.getCollection('b').getOptions()
            ]);
        });
        it('should re-add specified collections with filter', ()=>{
            const initialCollections = [
                {
                    name: 'a',
                    rawTitle: 'aa'
                },
                {
                    name: 'b',
                    rawTitle: 'bb'
                }
            ];
            collectionManager.addCollections(initialCollections);
            const spy = vitest.spyOn(collectionManager, 'addCollections');
            collectionManager.reAddCollections([
                collectionManager.getCollection('a')
            ]);
            expect(collectionManager.collectionInstancesArr.length).toBe(2);
            expect(spy).toHaveBeenCalledWith([
                collectionManager.getCollection('a').getOptions()
            ]);
        });
    });
    describe('getCollection', ()=>{
        it('should get a collection by name', ()=>{
            const collections = [
                {
                    name: 'test'
                }
            ];
            collectionManager.addCollections(collections);
            const collection = collectionManager.getCollection('test');
            expect(collection).toBeDefined();
        });
        it('should return undefined for non-existing collection', ()=>{
            const collection = collectionManager.getCollection('nonExisting');
            expect(collection).toBeUndefined();
        });
        it('should handle path with associations', ()=>{
            collectionManager.addCollections(collections_namespaceObject);
            const collection = collectionManager.getCollection('users.roles');
            expect(collection).toBeDefined();
        });
        it('if collection is object, should return the collection instance', ()=>{
            const collection = collectionManager.getCollection({
                name: 'test'
            });
            expect(collection).instanceOf(client_namespaceObject.Collection);
            expect(collection.name).toBe('test');
        });
    });
    describe('getCollections', ()=>{
        it('should return all collections', ()=>{
            const collections = [
                {
                    name: 'test1'
                },
                {
                    name: 'test2'
                }
            ];
            collectionManager.addCollections(collections);
            const allCollections = collectionManager.getCollections();
            expect(allCollections.length).toBe(2);
        });
        it('should return filtered collections', ()=>{
            const collections = [
                {
                    name: 'test1'
                },
                {
                    name: 'test2'
                }
            ];
            collectionManager.addCollections(collections);
            const filteredCollections = collectionManager.getCollections((c)=>'test1' === c.name);
            expect(filteredCollections.length).toBe(1);
            expect(filteredCollections[0].name).toBe('test1');
        });
    });
    describe('getCollectionName', ()=>{
        it('should return the name of a collection', ()=>{
            const collections = [
                {
                    name: 'test'
                }
            ];
            collectionManager.addCollections(collections);
            const name = collectionManager.getCollectionName('test');
            expect(name).toBe('test');
        });
    });
    describe('getCollectionField', ()=>{
        beforeEach(()=>{
            collectionManager.addCollections(collections_namespaceObject);
        });
        it('should return undefined for invalid path', ()=>{
            const field = collectionManager.getCollectionField('invalid');
            expect(field).toBeUndefined();
            const field2 = collectionManager.getCollectionField(void 0);
            expect(field2).toBeUndefined();
        });
        it('should return a collection field by path', ()=>{
            const field = collectionManager.getCollectionField('users.nickname');
            expect(field).toBeDefined();
            expect(field.name).toBe('nickname');
        });
        it('if collection not found, should return undefined', ()=>{
            const field = collectionManager.getCollectionField('invalid.nickname');
            expect(field).toBeUndefined();
        });
        it('if field is object, should return the field', ()=>{
            const field = collectionManager.getCollectionField({
                name: 'nickname'
            });
            expect(field).toEqual({
                name: 'nickname'
            });
        });
    });
    describe('getCollectionFields', ()=>{
        beforeEach(()=>{
            collectionManager.addCollections(collections_namespaceObject);
        });
        it('should return fields of a specified collection', ()=>{
            const fields = collectionManager.getCollectionFields('users');
            expect(fields.length).greaterThan(1);
        });
        it('if collection not found, should return an empty array', ()=>{
            const fields = collectionManager.getCollectionFields('invalid');
            expect(fields).toEqual([]);
        });
    });
    describe('clone', ()=>{
        it('should create a clone of the collection manager', ()=>{
            const collections = [
                {
                    name: 'test'
                }
            ];
            collectionManager.addCollections(collections);
            const clone = collectionManager.clone();
            expect(clone).not.toBe(collectionManager);
            expect(clone.collectionInstancesArr).toEqual(collectionManager.collectionInstancesArr);
            expect(clone.collectionInstancesMap).toEqual(collectionManager.collectionInstancesMap);
            expect(clone.dataSource).toBe(collectionManager.dataSource);
        });
    });
});
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
});
