import { UMB_WEBHOOK_ENTITY_TYPE } from '../../../entity.js';
import { UmbId } from '../../../../core/id/index.js';
import { WebhookService } from '../../../../core/backend-api/index.js';
import { tryExecute } from '../../../../core/resources/index.js';
/**
 * A data source for the Webhook that fetches data from the server
 * @class UmbWebhookDetailServerDataSource
 * @implements {UmbDetailDataSource<UmbWebhookDetailModel>}
 */
export class UmbWebhookDetailServerDataSource {
    #host;
    /**
     * Creates an instance of UmbWebhookDetailServerDataSource.
     * @param {UmbControllerHost} host - The controller host for this controller to be appended to
     * @memberof UmbWebhookDetailServerDataSource
     */
    constructor(host) {
        this.#host = host;
    }
    /**
     * Creates a new Webhook scaffold
     * @param {Partial<UmbWebhookDetailModel>} [preset]
     * @returns { CreateWebhookRequestModel }
     * @memberof UmbWebhookDetailServerDataSource
     */
    async createScaffold(preset = {}) {
        const data = {
            entityType: UMB_WEBHOOK_ENTITY_TYPE,
            unique: UmbId.new(),
            headers: {},
            events: [],
            enabled: true,
            url: '',
            name: '',
            description: '',
            contentTypes: [],
            ...preset,
        };
        return { data };
    }
    /**
     * Fetches a Webhook with the given id from the server
     * @param {string} unique
     * @returns {*}
     * @memberof UmbWebhookDetailServerDataSource
     */
    async read(unique) {
        if (!unique)
            throw new Error('Unique is missing');
        const { data, error } = await tryExecute(this.#host, WebhookService.getWebhookById({ path: { id: unique } }));
        if (error || !data) {
            return { error };
        }
        // TODO: make data mapper to prevent errors
        const dataType = {
            entityType: UMB_WEBHOOK_ENTITY_TYPE,
            unique: data.id,
            headers: data.headers,
            events: data.events,
            enabled: data.enabled,
            url: data.url,
            name: data.name ?? '',
            description: data.description,
            contentTypes: data.contentTypeKeys,
        };
        return { data: dataType };
    }
    /**
     * Inserts a new Webhook on the server
     * @param {UmbWebhookDetailModel} model
     * @returns {*}
     * @memberof UmbWebhookDetailServerDataSource
     */
    async create(model) {
        if (!model)
            throw new Error('Webhook is missing');
        // TODO: make data mapper to prevent errors
        const body = {
            id: model.unique,
            headers: model.headers,
            events: model.events.map((event) => event.alias),
            enabled: model.enabled,
            url: model.url,
            name: model.name,
            description: model.description,
            contentTypeKeys: model.contentTypes,
        };
        const { data, error } = await tryExecute(this.#host, WebhookService.postWebhook({
            body,
        }));
        if (data) {
            return this.read(data);
        }
        return { error };
    }
    /**
     * Updates a Webhook on the server
     * @param {UmbWebhookDetailModel} Webhook
     * @param model
     * @returns {*}
     * @memberof UmbWebhookDetailServerDataSource
     */
    async update(model) {
        if (!model.unique)
            throw new Error('Unique is missing');
        // TODO: make data mapper to prevent errors
        const body = {
            headers: model.headers,
            events: model.events.map((event) => event.alias),
            enabled: model.enabled,
            url: model.url,
            name: model.name,
            description: model.description,
            contentTypeKeys: model.contentTypes,
        };
        const { error } = await tryExecute(this.#host, WebhookService.putWebhookById({
            path: { id: model.unique },
            body,
        }));
        if (!error) {
            return this.read(model.unique);
        }
        return { error };
    }
    /**
     * Deletes a Webhook on the server
     * @param {string} unique
     * @returns {*}
     * @memberof UmbWebhookDetailServerDataSource
     */
    async delete(unique) {
        if (!unique)
            throw new Error('Unique is missing');
        return tryExecute(this.#host, WebhookService.deleteWebhookById({
            path: { id: unique },
        }));
    }
}
