import { UmbContextBase } from '../../../../libs/class-api/index.js';
import { UmbContextToken } from '../../../../libs/context-api/index.js';
import { MediaService } from '../../../core/backend-api/index.js';
import { tryExecute } from '../../../core/resources/index.js';
/**
 * A context for fetching and caching the media configuration.
 * @internal
 */
export class UmbMediaConfigurationContext extends UmbContextBase {
    /**
     * The cached media configuration.
     */
    static #mediaConfiguration;
    constructor(host) {
        super(host, UMB_MEDIA_CONFIGURATION_CONTEXT);
    }
    /**
     * Get the media configuration from the server, or return the cached configuration if it has already been fetched.
     * @returns A promise that resolves to the media configuration, or null if the configuration could not be fetched.
     */
    getMediaConfiguration() {
        return (UmbMediaConfigurationContext.#mediaConfiguration ??= this.#fetchMediaConfiguration());
    }
    /**
     * Fetch the media configuration from the server.
     * @returns A promise that resolves to the media configuration, or null if the configuration could not be fetched.
     */
    async #fetchMediaConfiguration() {
        const { data } = await tryExecute(this, MediaService.getMediaConfiguration());
        return data ?? null;
    }
}
// Export as default to work as a global context:
export default UmbMediaConfigurationContext;
/**
 * @internal
 */
export const UMB_MEDIA_CONFIGURATION_CONTEXT = new UmbContextToken('UmbMediaConfigurationContext');
