import { AbstractRepository } from '@crosslab/service-common';
import { DeviceGroupModel } from '../../model.js';
export class DeviceGroupRepository extends AbstractRepository {
    dependencies = {};
    constructor() {
        super('Device Group');
    }
    dependenciesMet() {
        if (!this.dependencies.deviceOverview)
            return false;
        return true;
    }
    initialize(entityManager) {
        this.repository = entityManager.getRepository(DeviceGroupModel);
    }
    async create(data) {
        const model = await super.create(data);
        model.type = 'group';
        return model;
    }
    async write(model, data) {
        if (!this.isInitialized())
            this.throwUninitializedRepositoryError();
        const { type: _type, description: _description, devices, isPublic: _isPublic, name: _name, owner: _owner, viewer: _viewer, ...additionalAttributes } = { ...model.additionalAttributes, ...data };
        await this.dependencies.deviceOverview.write(model, data);
        if (devices)
            model.devices = devices;
        model.additionalAttributes = additionalAttributes;
    }
    async format(model) {
        if (!this.isInitialized())
            this.throwUninitializedRepositoryError();
        const devices = model.devices;
        return {
            ...model.additionalAttributes,
            ...(await this.dependencies.deviceOverview.format(model)),
            type: 'group',
            devices: devices.filter((value, index, array) => array.findIndex(device => device.url === value.url) === index),
        };
    }
}
//# sourceMappingURL=deviceGroup.js.map