import { UMB_LANGUAGE_ENTITY_TYPE } from '../../entity.js';
import { LanguageService } from '../../../core/backend-api/index.js';
import { tryExecute } from '../../../core/resources/index.js';
/**
 * A data source for the Language that fetches data from the server
 * @class UmbLanguageServerDataSource
 * @implements {RepositoryDetailDataSource}
 */
export class UmbLanguageServerDataSource {
    #host;
    /**
     * Creates an instance of UmbLanguageServerDataSource.
     * @param {UmbControllerHost} host - The controller host for this controller to be appended to
     * @memberof UmbLanguageServerDataSource
     */
    constructor(host) {
        this.#host = host;
    }
    /**
     * Creates a new Language scaffold
     * @param {Partial<UmbLanguageDetailModel>} [preset]
     * @returns { CreateLanguageRequestModel }
     * @memberof UmbLanguageServerDataSource
     */
    async createScaffold(preset = {}) {
        const data = {
            entityType: UMB_LANGUAGE_ENTITY_TYPE,
            fallbackIsoCode: null,
            isDefault: false,
            isMandatory: false,
            name: '',
            unique: '',
            ...preset,
        };
        return { data };
    }
    /**
     * Fetches a Language with the given id from the server
     * @param {string} unique
     * @returns {*}
     * @memberof UmbLanguageServerDataSource
     */
    async read(unique) {
        if (!unique)
            throw new Error('Unique is missing');
        const { data, error } = await tryExecute(this.#host, LanguageService.getLanguageByIsoCode({ path: { isoCode: unique } }));
        if (error || !data) {
            return { error };
        }
        // TODO: make data mapper to prevent errors
        const dataType = {
            entityType: UMB_LANGUAGE_ENTITY_TYPE,
            fallbackIsoCode: data.fallbackIsoCode || null,
            isDefault: data.isDefault,
            isMandatory: data.isMandatory,
            name: data.name,
            unique: data.isoCode,
        };
        return { data: dataType };
    }
    /**
     * Inserts a new Language on the server
     * @param {UmbLanguageDetailModel} model
     * @returns {*}
     * @memberof UmbLanguageServerDataSource
     */
    async create(model) {
        if (!model)
            throw new Error('Language is missing');
        // TODO: make data mapper to prevent errors
        const body = {
            fallbackIsoCode: model.fallbackIsoCode,
            isDefault: model.isDefault,
            isMandatory: model.isMandatory,
            isoCode: model.unique,
            name: model.name,
        };
        const { data, error } = await tryExecute(this.#host, LanguageService.postLanguage({
            body,
        }));
        if (data && typeof data === 'string') {
            return this.read(data);
        }
        return { error };
    }
    /**
     * Updates a Language on the server
     * @param {UmbLanguageDetailModel} Language
     * @param model
     * @returns {*}
     * @memberof UmbLanguageServerDataSource
     */
    async update(model) {
        if (!model.unique)
            throw new Error('Unique is missing');
        // TODO: make data mapper to prevent errors
        const body = {
            fallbackIsoCode: model.fallbackIsoCode,
            isDefault: model.isDefault,
            isMandatory: model.isMandatory,
            name: model.name,
        };
        const { error } = await tryExecute(this.#host, LanguageService.putLanguageByIsoCode({
            path: { isoCode: model.unique },
            body,
        }));
        if (!error) {
            return this.read(model.unique);
        }
        return { error };
    }
    /**
     * Deletes a Language on the server
     * @param {string} unique
     * @returns {*}
     * @memberof UmbLanguageServerDataSource
     */
    async delete(unique) {
        if (!unique)
            throw new Error('Unique is missing');
        return tryExecute(this.#host, LanguageService.deleteLanguageByIsoCode({
            path: { isoCode: unique },
        }));
    }
}
