import { tryExecute } from '../../core/resources/index.js';
import { DocumentService, PreviewService } from '../../core/backend-api/index.js';
import { UmbRepositoryBase } from '../../core/repository/index.js';
export class UmbPreviewRepository extends UmbRepositoryBase {
    constructor(host) {
        super(host);
    }
    /**
     * Gets the preview URL for a document.
     * @param {string} unique The unique identifier of the document.
     * @param {string} providerAlias The alias of the URL provider registered on the server.
     * @param {string | undefined} culture The culture to preview (undefined means invariant).
     * @param {string | undefined} segment The segment to preview (undefined means no specific segment).
     * @returns {DocumentUrlInfoModel} The preview URLs of the document.
     * @memberof UmbPreviewRepository
     */
    async getPreviewUrl(unique, providerAlias, culture, segment) {
        const { data, error } = await tryExecute(this, DocumentService.getDocumentByIdPreviewUrl({
            path: { id: unique },
            query: { providerAlias, culture, segment },
        }));
        if (error) {
            throw new Error(error.message);
        }
        return data;
    }
    /**
     * Gets the published URL info for a document.
     * @param {string} unique The unique identifier of the document.
     * @param {string | undefined} culture The culture to preview (undefined means invariant).
     * @returns {DocumentUrlInfoModel} The published URL info of the document.
     * @memberof UmbPreviewRepository
     */
    async getPublishedUrl(unique, culture) {
        if (!unique)
            return null;
        const { data, error } = await tryExecute(this, DocumentService.getDocumentUrls({ query: { id: [unique] } }));
        if (error) {
            throw new Error(error.message);
        }
        if (!data?.length)
            return null;
        // TODO: [LK] Review the logic here, unsure whether this is correct. When will the array have more than one item?
        const urlInfo = culture ? data[0].urlInfos.find((x) => x.culture === culture) : data[0].urlInfos[0];
        return urlInfo;
    }
    /**
     * Exits preview mode.
     * @returns {Promise<void>}
     * @memberof UmbPreviewRepository
     */
    async exit() {
        await tryExecute(this, PreviewService.deletePreview(), { disableNotifications: true });
        return;
    }
}
