import * as _ from 'lodash-es';
const { defaults } = _;
export default class SharedData {
    _storage;
    constructor(storage, initialControl = {}) {
        if (!storage) {
            throw new Error('Storage is required for SharedData');
        }
        this._storage = storage;
        defaults(this._storage, {
            sharedDeployment: {},
            sharedWorkspaces: {},
            sharedEntities: {},
            sharedApplication: {},
            sharedSource: {},
            control: initialControl,
            props: {},
        });
        this._storage.sharedApplication.nodeDependencies = this._storage.sharedApplication.nodeDependencies ?? {};
    }
    getSource() {
        return this._storage.sharedSource;
    }
    getControl() {
        return this._storage.control;
    }
    getApplication() {
        if (!this._storage.sharedApplication)
            throw new Error('Shared application not loaded');
        return this._storage.sharedApplication;
    }
    setEntity(entityName, entity) {
        this._storage.sharedEntities[entityName] = entity;
    }
    hasEntity(entityName) {
        return Boolean(this._storage.sharedEntities[entityName]);
    }
    getEntity(entityName) {
        const entity = this._storage.sharedEntities[entityName];
        if (!entity) {
            throw new Error(`Entity definition not loaded for ${entityName}`);
        }
        return entity;
    }
    getEntities(entityNames = Object.keys(this._storage.sharedEntities)) {
        return entityNames.map(entityName => ({ entityName, entity: this.getEntity(entityName) }));
    }
    getEntitiesMap() {
        return this._storage.sharedEntities;
    }
}
