import { UMB_STYLESHEET_FOLDER_ENTITY_TYPE } from '../../../entity.js';
import { UmbServerFilePathUniqueSerializer } from '../../../../../core/server-file-system/index.js';
import { StylesheetService } from '../../../../../core/backend-api/index.js';
import { tryExecute } from '../../../../../core/resources/index.js';
import { UmbId } from '../../../../../core/id/index.js';
/**
 * A data source for Stylesheet folders that fetches data from the server
 * @class UmbStylesheetFolderServerDataSource
 * @implements {RepositoryDetailDataSource}
 */
export class UmbStylesheetFolderServerDataSource {
    #host;
    #serverFilePathUniqueSerializer = new UmbServerFilePathUniqueSerializer();
    /**
     * Creates an instance of UmbStylesheetFolderServerDataSource.
     * @param {UmbControllerHost} host - The controller host for this controller to be appended to
     * @memberof UmbStylesheetFolderServerDataSource
     */
    constructor(host) {
        this.#host = host;
    }
    async createScaffold(preset) {
        const scaffold = {
            entityType: UMB_STYLESHEET_FOLDER_ENTITY_TYPE,
            unique: UmbId.new(),
            name: '',
            ...preset,
        };
        return { data: scaffold };
    }
    /**
     * Fetches a Stylesheet folder from the server
     * @param {string} unique
     * @returns {UmbDataSourceResponse<UmbFolderModel>}
     * @memberof UmbStylesheetFolderServerDataSource
     */
    async read(unique) {
        if (!unique)
            throw new Error('Unique is missing');
        const path = this.#serverFilePathUniqueSerializer.toServerPath(unique);
        if (!path)
            throw new Error('Cannot read stylesheet folder without a path');
        const { data, error } = await tryExecute(this.#host, StylesheetService.getStylesheetFolderByPath({
            path: { path },
        }));
        if (data) {
            const mappedData = {
                entityType: UMB_STYLESHEET_FOLDER_ENTITY_TYPE,
                unique: this.#serverFilePathUniqueSerializer.toUnique(data.path),
                parentUnique: data.parent ? this.#serverFilePathUniqueSerializer.toUnique(data.parent.path) : null,
                name: data.name,
            };
            return { data: mappedData };
        }
        return { error };
    }
    /**
     * Creates a Stylesheet folder on the server
     * @param {UmbCreateFolderModel} model
     * @returns {UmbDataSourceResponse<UmbFolderModel>}
     * @memberof UmbStylesheetFolderServerDataSource
     */
    async create(model, parentUnique) {
        if (!model)
            throw new Error('Data is missing');
        if (!model.unique)
            throw new Error('Unique is missing');
        if (!model.name)
            throw new Error('Name is missing');
        const parentPath = new UmbServerFilePathUniqueSerializer().toServerPath(parentUnique);
        const body = {
            parent: parentPath ? { path: parentPath } : null,
            name: model.name,
        };
        const { data, error } = await tryExecute(this.#host, StylesheetService.postStylesheetFolder({
            body,
        }));
        if (data && typeof data === 'string') {
            const newPath = decodeURIComponent(data);
            const newPathUnique = this.#serverFilePathUniqueSerializer.toUnique(newPath);
            return this.read(newPathUnique);
        }
        return { error };
    }
    /**
     * Deletes a Stylesheet folder on the server
     * @param {string} unique
     * @returns {UmbDataSourceErrorResponse}
     * @memberof UmbStylesheetServerDataSource
     */
    async delete(unique) {
        if (!unique)
            throw new Error('Unique is missing');
        const path = this.#serverFilePathUniqueSerializer.toServerPath(unique);
        if (!path)
            throw new Error('Cannot delete stylesheet folder without a path');
        return tryExecute(this.#host, StylesheetService.deleteStylesheetFolderByPath({
            path: { path },
        }));
    }
    async update() {
        throw new Error('Updating is not supported');
    }
}
