import { UmbControllerBase } from '../../../../libs/class-api/index.js';
import { tryExecute } from '../../resources/index.js';
/**
 * A data source base for items that fetches items from the server
 * @class UmbItemServerDataSourceBase
 * @implements {DocumentTreeDataSource}
 */
export class UmbItemServerDataSourceBase extends UmbControllerBase {
    #getItems;
    #mapper;
    /**
     * Creates an instance of UmbItemServerDataSourceBase.
     * @param {UmbControllerHost} host - The controller host for this controller to be appended to
     * @param args
     * @memberof UmbItemServerDataSourceBase
     */
    constructor(host, args) {
        super(host);
        this.#getItems = args.getItems;
        this.#mapper = args.mapper;
    }
    /**
     * Fetches the items for the given uniques from the server
     * @param {Array<string>} uniques
     * @returns {*}
     * @memberof UmbItemServerDataSourceBase
     */
    async getItems(uniques) {
        if (!this.#getItems)
            throw new Error('getItems is not implemented');
        if (!uniques)
            throw new Error('Uniques are missing');
        const { data, error } = await tryExecute(this, this.#getItems(uniques));
        return { data: this._getMappedItems(data), error };
    }
    _getMappedItems(items) {
        if (!items)
            return undefined;
        if (!this.#mapper)
            throw new Error('Mapper is not implemented');
        return items.map((item) => this.#mapper(item));
    }
}
