import { UMB_MEMBER_GROUP_ENTITY_TYPE } from '../../entity.js';
import { UmbId } from '../../../../core/id/index.js';
import { tryExecute } from '../../../../core/resources/index.js';
import { MemberGroupService } from '../../../../core/backend-api/index.js';
/**
 * A data source for the Member Group that fetches data from the server
 * @class UmbMemberGroupServerDataSource
 * @implements {RepositoryDetailDataSource}
 */
export class UmbMemberGroupServerDataSource {
    #host;
    /**
     * Creates an instance of UmbMemberGroupServerDataSource.
     * @param {UmbControllerHost} host - The controller host for this controller to be appended to
     * @memberof UmbMemberGroupServerDataSource
     */
    constructor(host) {
        this.#host = host;
    }
    /**
     * Creates a new Member Group scaffold
     * @param {(string | null)} parentUnique
     * @returns { CreateMemberGroupRequestModel }
     * @memberof UmbMemberGroupServerDataSource
     */
    async createScaffold() {
        const data = {
            entityType: UMB_MEMBER_GROUP_ENTITY_TYPE,
            unique: UmbId.new(),
            name: '',
        };
        return { data };
    }
    /**
     * Fetches a Member Group with the given id from the server
     * @param {string} unique
     * @returns {*}
     * @memberof UmbMemberGroupServerDataSource
     */
    async read(unique) {
        if (!unique)
            throw new Error('Unique is missing');
        const { data, error } = await tryExecute(this.#host, MemberGroupService.getMemberGroupById({ path: { id: unique } }));
        if (error || !data) {
            return { error };
        }
        const MemberGroup = {
            entityType: UMB_MEMBER_GROUP_ENTITY_TYPE,
            unique: data.id,
            name: data.name,
        };
        return { data: MemberGroup };
    }
    /**
     * Inserts a new Member Group on the server
     * @param {UmbMemberGroupDetailModel} model
     * @returns {*}
     * @memberof UmbMemberGroupServerDataSource
     */
    async create(model) {
        if (!model)
            throw new Error('Member Group is missing');
        const body = {
            name: model.name,
            id: model.unique,
        };
        const { data, error } = await tryExecute(this.#host, MemberGroupService.postMemberGroup({
            body,
        }));
        if (data && typeof data === 'string') {
            return this.read(data);
        }
        return { error };
    }
    /**
     * Updates a MemberGroup on the server
     * @param {UmbMemberGroupDetailModel} MemberGroup
     * @param model
     * @returns {*}
     * @memberof UmbMemberGroupServerDataSource
     */
    async update(model) {
        if (!model.unique)
            throw new Error('Unique is missing');
        // TODO: make data mapper to prevent errors
        // TODO:  add type UpdateMemberGroupRequestModel
        const body = {
            name: model.name,
        };
        const { error } = await tryExecute(this.#host, MemberGroupService.putMemberGroupById({
            path: { id: model.unique },
            body,
        }));
        if (!error) {
            return this.read(model.unique);
        }
        return { error };
    }
    /**
     * Deletes a Member Group on the server
     * @param {string} unique
     * @returns {*}
     * @memberof UmbMemberGroupServerDataSource
     */
    async delete(unique) {
        if (!unique)
            throw new Error('Unique is missing');
        return tryExecute(this.#host, MemberGroupService.deleteMemberGroupById({
            path: { id: unique },
        }));
    }
}
