import { UMB_DOCUMENT_BLUEPRINT_FOLDER_ENTITY_TYPE } from '../../../entity.js';
import { DocumentBlueprintService } from '../../../../../core/backend-api/index.js';
import { tryExecute } from '../../../../../core/resources/index.js';
import { UmbId } from '../../../../../core/id/index.js';
/**
 * A data source for a Document Blueprint folder that fetches data from the server
 * @class UmbDocumentBlueprintFolderServerDataSource
 * @implements {RepositoryDetailDataSource}
 */
export class UmbDocumentBlueprintFolderServerDataSource {
    #host;
    /**
     * Creates an instance of UmbDocumentBlueprintFolderServerDataSource.
     * @param {UmbControllerHost} host - The controller host for this controller to be appended to
     * @memberof UmbDocumentBlueprintFolderServerDataSource
     */
    constructor(host) {
        this.#host = host;
    }
    /**
     * Creates a scaffold for a Document Blueprint folder
     * @param {Partial<UmbFolderModel>} [preset]
     * @returns {*}
     * @memberof UmbDocumentBlueprintFolderServerDataSource
     */
    async createScaffold(preset) {
        const scaffold = {
            entityType: UMB_DOCUMENT_BLUEPRINT_FOLDER_ENTITY_TYPE,
            unique: UmbId.new(),
            name: '',
            ...preset,
        };
        return { data: scaffold };
    }
    /**
     * Fetches a Document Blueprint folder from the server
     * @param {string} unique
     * @returns {*}
     * @memberof UmbDocumentBlueprintFolderServerDataSource
     */
    async read(unique) {
        if (!unique)
            throw new Error('Unique is missing');
        const { data, error } = await tryExecute(this.#host, DocumentBlueprintService.getDocumentBlueprintFolderById({
            path: { id: unique },
        }));
        if (data) {
            const mappedData = {
                entityType: UMB_DOCUMENT_BLUEPRINT_FOLDER_ENTITY_TYPE,
                unique: data.id,
                name: data.name,
            };
            return { data: mappedData };
        }
        return { error };
    }
    /**
     * Creates a Document Blueprint folder on the server
     * @param {UmbFolderModel} model
     * @returns {*}
     * @memberof UmbDocumentBlueprintFolderServerDataSource
     */
    async create(model, parentUnique) {
        if (!model)
            throw new Error('Model is missing');
        if (!model.unique)
            throw new Error('Unique is missing');
        if (!model.name)
            throw new Error('Name is missing');
        const body = {
            id: model.unique,
            parent: parentUnique ? { id: parentUnique } : null,
            name: model.name,
        };
        const { error } = await tryExecute(this.#host, DocumentBlueprintService.postDocumentBlueprintFolder({
            body,
        }));
        if (!error) {
            return this.read(model.unique);
        }
        return { error };
    }
    /**
     * Updates a Document Blueprint folder on the server
     * @param {UmbFolderModel} model
     * @returns {*}
     * @memberof UmbDocumentBlueprintFolderServerDataSource
     */
    async update(model) {
        if (!model)
            throw new Error('Model is missing');
        if (!model.unique)
            throw new Error('Unique is missing');
        if (!model.name)
            throw new Error('Folder name is missing');
        const { error } = await tryExecute(this.#host, DocumentBlueprintService.putDocumentBlueprintFolderById({
            path: { id: model.unique },
            body: { name: model.name },
        }));
        if (!error) {
            return this.read(model.unique);
        }
        return { error };
    }
    /**
     * Deletes a Document Blueprint folder on the server
     * @param {string} unique
     * @returns {*}
     * @memberof UmbDocumentBlueprintServerDataSource
     */
    async delete(unique) {
        if (!unique)
            throw new Error('Unique is missing');
        return tryExecute(this.#host, DocumentBlueprintService.deleteDocumentBlueprintFolderById({
            path: { id: unique },
        }));
    }
}
