import { UMB_TEMPLATE_ENTITY_TYPE } from '../../entity.js';
import { UmbId } from '../../../../core/id/index.js';
import { TemplateService } from '../../../../core/backend-api/index.js';
import { tryExecute } from '../../../../core/resources/index.js';
/**
 * A data source for the Template that fetches data from the server
 * @class UmbTemplateServerDataSource
 * @implements {RepositoryDetailDataSource}
 */
export class UmbTemplateServerDataSource {
    #host;
    /**
     * Creates an instance of UmbTemplateServerDataSource.
     * @param {UmbControllerHost} host - The controller host for this controller to be appended to
     * @memberof UmbTemplateServerDataSource
     */
    constructor(host) {
        this.#host = host;
    }
    /**
     * Creates a new Template scaffold
     * @param {Partial<UmbTemplateDetailModel>} [preset]
     * @returns { CreateTemplateRequestModel }
     * @memberof UmbTemplateServerDataSource
     */
    async createScaffold(preset = {}) {
        const scaffold = '@using Umbraco.Cms.Web.Common.PublishedModels;\n@inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage\n@{\n\tLayout = null;\n}';
        const data = {
            entityType: UMB_TEMPLATE_ENTITY_TYPE,
            unique: UmbId.new(),
            name: '',
            alias: '',
            content: scaffold,
            masterTemplate: preset.masterTemplate ?? null,
            ...preset,
        };
        return { data };
    }
    /**
     * Fetches a Template with the given id from the server
     * @param {string} unique
     * @returns {*}
     * @memberof UmbTemplateServerDataSource
     */
    async read(unique) {
        if (!unique)
            throw new Error('Unique is missing');
        const { data, error } = await tryExecute(this.#host, TemplateService.getTemplateById({ path: { id: unique } }));
        if (error || !data) {
            return { error };
        }
        // TODO: make data mapper to prevent errors
        const template = {
            entityType: UMB_TEMPLATE_ENTITY_TYPE,
            unique: data.id,
            name: data.name,
            content: data.content || null,
            alias: data.alias,
            masterTemplate: data.masterTemplate ? { unique: data.masterTemplate.id } : null,
        };
        return { data: template };
    }
    /**
     * Inserts a new Template on the server
     * @param {UmbTemplateDetailModel} model
     * @returns {*}
     * @memberof UmbTemplateServerDataSource
     */
    async create(model) {
        if (!model)
            throw new Error('Template is missing');
        // TODO: make data mapper to prevent errors
        const body = {
            id: model.unique,
            name: model.name,
            content: model.content,
            alias: model.alias,
        };
        const { data, error } = await tryExecute(this.#host, TemplateService.postTemplate({
            body,
        }));
        if (data) {
            return this.read(data);
        }
        return { error };
    }
    /**
     * Updates a Template on the server
     * @param {UmbTemplateDetailModel} Template
     * @param model
     * @returns {*}
     * @memberof UmbTemplateServerDataSource
     */
    async update(model) {
        if (!model.unique)
            throw new Error('Unique is missing');
        // TODO: make data mapper to prevent errors
        const body = {
            name: model.name,
            content: model.content,
            alias: model.alias,
        };
        const { error } = await tryExecute(this.#host, TemplateService.putTemplateById({
            path: { id: model.unique },
            body,
        }));
        if (!error) {
            return this.read(model.unique);
        }
        return { error };
    }
    /**
     * Deletes a Template on the server
     * @param {string} unique
     * @returns {*}
     * @memberof UmbTemplateServerDataSource
     */
    async delete(unique) {
        if (!unique)
            throw new Error('Unique is missing');
        return tryExecute(this.#host, TemplateService.deleteTemplateById({
            path: { id: unique },
        }));
    }
}
