import { UmbContextBase } from '../../../../libs/class-api/index.js';
import { UmbContextToken } from '../../../../libs/context-api/index.js';
import { DocumentService } from '../../../core/backend-api/index.js';
import { tryExecute } from '../../../core/resources/index.js';
// TODO: Turn this into a Repository with a Store that holds the cache [NL]
/**
 * A context for fetching and caching the document configuration.
 * @internal
 */
export class UmbDocumentConfigurationContext extends UmbContextBase {
    /**
     * The cached document configuration.
     */
    static #documentConfiguration;
    constructor(host) {
        super(host, UMB_DOCUMENT_CONFIGURATION_CONTEXT);
    }
    /**
     * Get the document configuration from the server, or return the cached configuration if it has already been fetched.
     * @returns A promise that resolves to the document configuration, or null if the configuration could not be fetched.
     */
    getDocumentConfiguration() {
        return (UmbDocumentConfigurationContext.#documentConfiguration ??= this.fetchDocumentConfiguration());
    }
    /**
     * Fetch the document configuration from the server.
     * @returns A promise that resolves to the document configuration, or null if the configuration could not be fetched.
     */
    async fetchDocumentConfiguration() {
        const { data } = await tryExecute(this, DocumentService.getDocumentConfiguration());
        return data ?? null;
    }
}
// Export as default to work as a global context:
export default UmbDocumentConfigurationContext;
/**
 * @internal
 */
export const UMB_DOCUMENT_CONFIGURATION_CONTEXT = new UmbContextToken('UmbDocumentConfigurationContext');
