import { DocumentService } from '../../../core/backend-api/index.js';
import { tryExecute } from '../../../core/resources/index.js';
/**
 * A data source for the Document Public Access that fetches data from the server
 * @class UmbDocumentPublicAccessServerDataSource
 * @implements {RepositoryDetailDataSource}
 */
export class UmbDocumentPublicAccessServerDataSource {
    #host;
    /**
     * Creates an instance of UmbDocumentPublicAccessServerDataSource.
     * @param {UmbControllerHost} host - The controller host for this controller to be appended to
     * @memberof UmbDocumentPublicAccessServerDataSource
     */
    constructor(host) {
        this.#host = host;
    }
    /**
     * Creates the Public Access for the given Document unique
     * @param {string} unique
     * @param {PublicAccessRequestModel} data
     * @memberof UmbDocumentPublicAccessServerDataSource
     */
    async create(unique, data) {
        if (!unique)
            throw new Error('unique is missing');
        return tryExecute(this.#host, DocumentService.postDocumentByIdPublicAccess({ path: { id: unique }, body: data }));
    }
    /**
     * Fetches the Public Access for the given Document unique
     * @param {string} unique
     * @param {boolean} includeAncestors - Whether to include ancestor Public Access settings in the response
     * @memberof UmbDocumentPublicAccessServerDataSource
     */
    async read(unique, includeAncestors) {
        if (!unique)
            throw new Error('unique is missing');
        // NOTE: The entity will not be present, when fetching Public Access for a descendant of a protected Document.
        //       This is a perfectly valid scenario, which is handled in the view. In other words, just use tryExecute here.
        return tryExecute(this.#host, DocumentService.getDocumentByIdPublicAccess({ path: { id: unique }, query: { includeAncestors } }));
    }
    /**
     * Updates Public Access for the given Document unique
     * @param {string} unique
     * @param {PublicAccessRequestModel} data
     * @param body
     * @memberof UmbDocumentPublicAccessServerDataSource
     */
    async update(unique, body) {
        if (!unique)
            throw new Error('unique is missing');
        return tryExecute(this.#host, DocumentService.putDocumentByIdPublicAccess({ path: { id: unique }, body }));
    }
    /**
     * Deletes Public Access for the given Document unique
     * @param {string} unique
     * @memberof UmbDocumentPublicAccessServerDataSource
     */
    async delete(unique) {
        if (!unique)
            throw new Error('unique is missing');
        return tryExecute(this.#host, DocumentService.deleteDocumentByIdPublicAccess({ path: { id: unique } }));
    }
}
