import { UmbRepositoryBase } from '../repository-base.js';
export class UmbDetailRepositoryBase extends UmbRepositoryBase {
    #init;
    #detailStore;
    constructor(host, detailSource, detailStoreContextAlias) {
        super(host);
        if (!detailSource)
            throw new Error('Detail source is missing');
        if (!detailStoreContextAlias)
            throw new Error('Detail store context alias is missing');
        this.detailDataSource = new detailSource(host);
        // TODO: ideally no preventTimeouts here.. [NL]
        this.#init = this.consumeContext(detailStoreContextAlias, (instance) => {
            this.#detailStore = instance;
        })
            .asPromise({ preventTimeout: true })
            // Ignore the error, we can assume that the flow was stopped (asPromise failed), but it does not mean that the consumption was not successful.
            .catch(() => undefined);
    }
    /**
     * Creates a scaffold
     * @param {UmbDeepPartialObject<DetailModelType>} [preset]
     * @returns {*}
     * @memberof UmbDetailRepositoryBase
     */
    async createScaffold(preset) {
        return this.detailDataSource.createScaffold(preset);
    }
    /**
     * 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.detailDataSource.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 {DetailModelType} model
     * @param {string | null} [parentUnique]
     * @returns {*}
     * @memberof UmbDetailRepositoryBase
     */
    async create(model, parentUnique) {
        if (!model)
            throw new Error('Data is missing');
        await this.#init;
        const { data: createdData, error } = await this.detailDataSource.create(model, parentUnique);
        if (createdData) {
            this.#detailStore?.append(createdData);
        }
        return { data: createdData, error };
    }
    /**
     * Saves the given data
     * @param {DetailModelType} model
     * @returns {*}
     * @memberof UmbDetailRepositoryBase
     */
    async save(model) {
        if (!model)
            throw new Error('Data is missing');
        if (!model.unique)
            throw new Error('Unique is missing');
        await this.#init;
        const { data: updatedData, error } = await this.detailDataSource.update(model);
        if (updatedData) {
            this.#detailStore?.updateItem(model.unique, updatedData);
        }
        return { data: updatedData, error };
    }
    /**
     * Deletes the detail for the given unique
     * @param {string} unique
     * @returns {*}
     * @memberof UmbDetailRepositoryBase
     */
    async delete(unique) {
        if (!unique)
            throw new Error('Unique is missing');
        await this.#init;
        const { error } = await this.detailDataSource.delete(unique);
        if (!error) {
            this.#detailStore?.removeItem(unique);
        }
        return { error };
    }
    /**
     * 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);
    }
    destroy() {
        this.#detailStore = undefined;
        this.detailDataSource = undefined;
        super.destroy();
    }
}
