import { UmbUserConfigServerDataSource } from './user-config.server.data-source.js';
import { UMB_USER_CONFIG_STORE_CONTEXT } from './user-config.store.token.js';
import { UmbRepositoryBase } from '../../../../core/repository/index.js';
export class UmbUserConfigRepository extends UmbRepositoryBase {
    #dataStore;
    #dataSource = new UmbUserConfigServerDataSource(this);
    constructor(host) {
        super(host);
        this.initialized = new Promise((resolve) => {
            this.consumeContext(UMB_USER_CONFIG_STORE_CONTEXT, async (store) => {
                if (store) {
                    this.#dataStore = store;
                    await this.#init();
                    resolve();
                }
            });
        });
    }
    async #init() {
        // Check if the store already has data
        if (this.#dataStore?.getState()) {
            return;
        }
        const { data } = await this.#dataSource.getUserConfig();
        if (data) {
            this.#dataStore?.update(data);
        }
    }
    /**
     * Subscribe to the entire user configuration.
     */
    all() {
        if (!this.#dataStore) {
            throw new Error('Data store not initialized');
        }
        return this.#dataStore.all();
    }
    /**
     * Subscribe to a part of the user configuration.
     * @param part
     */
    part(part) {
        if (!this.#dataStore) {
            throw new Error('Data store not initialized');
        }
        return this.#dataStore.part(part);
    }
}
export default UmbUserConfigRepository;
