import { UmbPartialViewDetailServerDataSource } from '../../repository/partial-view-detail.server.data-source.js';
import { UmbServerFilePathUniqueSerializer, appendFileExtensionIfNeeded, } from '../../../../core/server-file-system/index.js';
import { PartialViewService } from '../../../../core/backend-api/index.js';
import { tryExecute } from '../../../../core/resources/index.js';
export class UmbRenamePartialViewServerDataSource {
    #host;
    #detailDataSource;
    #serverFilePathUniqueSerializer = new UmbServerFilePathUniqueSerializer();
    constructor(host) {
        this.#host = host;
        this.#detailDataSource = new UmbPartialViewDetailServerDataSource(this.#host);
    }
    /**
     * Rename Partial View
     * @param {string} unique
     * @param {string} name
     * @returns {*}
     * @memberof UmbRenamePartialViewServerDataSource
     */
    async rename(unique, name) {
        if (!unique)
            throw new Error('Unique is missing');
        if (!name)
            throw new Error('Name is missing');
        const path = this.#serverFilePathUniqueSerializer.toServerPath(unique);
        if (!path)
            throw new Error('Path is missing');
        const body = {
            name: appendFileExtensionIfNeeded(name, '.cshtml'),
        };
        const { data, error } = await tryExecute(this.#host, PartialViewService.putPartialViewByPathRename({
            path: { path },
            body,
        }));
        if (data && typeof data === 'string') {
            const newPath = decodeURIComponent(data);
            const newPathUnique = this.#serverFilePathUniqueSerializer.toUnique(newPath);
            return this.#detailDataSource.read(newPathUnique);
        }
        return { error };
    }
}
