import { UMB_STYLESHEET_ENTITY_TYPE, UMB_STYLESHEET_FOLDER_ENTITY_TYPE } from '../../entity.js';
import { UmbManagementApiStylesheetItemDataRequestManager } from './stylesheet-item.server.request-manager.js';
import { UmbServerFilePathUniqueSerializer } from '../../../../core/server-file-system/index.js';
import { UmbControllerBase } from '../../../../../libs/class-api/index.js';
/**
 * A data source for stylesheet items that fetches data from the server
 * @class UmbStylesheetItemServerDataSource
 * @implements {UmbItemDataSource}
 */
export class UmbStylesheetItemServerDataSource extends UmbControllerBase {
    #serverFilePathUniqueSerializer = new UmbServerFilePathUniqueSerializer();
    #itemRequestManager = new UmbManagementApiStylesheetItemDataRequestManager(this);
    /**
     * Fetches the items for the given uniques from the server
     * @param {Array<string>} uniques
     * @returns {*}
     * @memberof UmbStylesheetItemServerDataSource
     */
    async getItems(uniques) {
        if (!uniques)
            throw new Error('Uniques are missing');
        const paths = uniques
            .map((unique) => {
            const serverPath = this.#serverFilePathUniqueSerializer.toServerPath(unique);
            return serverPath ? serverPath : null;
        })
            .filter((x) => x !== null);
        const { data, error } = await this.#itemRequestManager.getItems(paths);
        if (data) {
            const items = data.map((item) => {
                return {
                    entityType: item.isFolder ? UMB_STYLESHEET_FOLDER_ENTITY_TYPE : UMB_STYLESHEET_ENTITY_TYPE,
                    unique: this.#serverFilePathUniqueSerializer.toUnique(item.path),
                    parentUnique: item.parent ? this.#serverFilePathUniqueSerializer.toUnique(item.parent.path) : null,
                    name: item.name,
                    isFolder: item.isFolder,
                };
            });
            return { data: items };
        }
        return { error };
    }
}
