import { UmbRelationTypeDetailServerDataSource } from './relation-type-detail.server.data-source.js';
import { UMB_RELATION_TYPE_DETAIL_STORE_CONTEXT } from './relation-type-detail.store.context-token.js';
import { UmbRepositoryBase } from '../../../../core/repository/index.js';
// TODO: create read detail repository mixin
export class UmbRelationTypeDetailRepository extends UmbRepositoryBase {
    #init;
    #detailStore;
    #detailSource = new UmbRelationTypeDetailServerDataSource(this);
    constructor(host) {
        super(host);
        this.#init = this.consumeContext(UMB_RELATION_TYPE_DETAIL_STORE_CONTEXT, (instance) => {
            if (instance) {
                this.#detailStore = instance;
            }
        }).asPromise({ preventTimeout: true });
    }
    /**
     * Requests the detail for the given unique
     * @param {string} unique
     * @returns {*}
     * @memberof UmbDetailRepositoryBase
     */
    async requestByUnique(unique) {
        if (!unique)
            throw new Error('Unique is missing');
        await this.#init;
        const { data, error } = await this.#detailSource.read(unique);
        if (data) {
            this.#detailStore?.append(data);
        }
        return { data, error, asObservable: () => this.#detailStore.byUnique(unique) };
    }
    /**
     * Returns a promise with an observable of the detail for the given unique
     * @param {string} unique
     * @returns {*}
     * @memberof UmbDetailRepositoryBase
     */
    async byUnique(unique) {
        if (!unique)
            throw new Error('Unique is missing');
        await this.#init;
        return this.#detailStore.byUnique(unique);
    }
}
export default UmbRelationTypeDetailRepository;
