/*
 * SPDX-FileCopyrightText: syuilo and misskey-project
 * SPDX-License-Identifier: AGPL-3.0-only
 */

import { MediaProxy } from '@@/js/media-proxy.js';
import { url } from '@@/js/config.js';
import { instance } from '@/instance.js';

let _mediaProxy: MediaProxy | null = null;

// Default fallback URLs
const DEFAULT_IMAGE_FALLBACK = `${url}/fallback.webp`;
const DEFAULT_STATIC_FALLBACK = `${url}/fallback-static.webp`;

/**
 * Get a proxied image URL.
 */
export function getProxiedImageUrl(...args: Parameters<MediaProxy['getProxiedImageUrl']>): string {
	try {
		if (_mediaProxy == null) {
			_mediaProxy = new MediaProxy(instance, url);
		}
		return _mediaProxy.getProxiedImageUrl(...args);
	} catch (error) {
		console.error('Error in getProxiedImageUrl:', error);
		return DEFAULT_IMAGE_FALLBACK;
	}
}

/**
 * Get a nullable proxied image URL.
 */
export function getProxiedImageUrlNullable(...args: Parameters<MediaProxy['getProxiedImageUrlNullable']>): string | null {
	try {
		if (_mediaProxy == null) {
			_mediaProxy = new MediaProxy(instance, url);
		}
		return _mediaProxy.getProxiedImageUrlNullable(...args);
	} catch (error) {
		console.error('Error in getProxiedImageUrlNullable:', error);
		return null;
	}
}

/**
 * Get a static image URL.
 */
export function getStaticImageUrl(...args: Parameters<MediaProxy['getStaticImageUrl']>): string {
	try {
		if (_mediaProxy == null) {
			_mediaProxy = new MediaProxy(instance, url);
		}
		return _mediaProxy.getStaticImageUrl(...args);
	} catch (error) {
		console.error('Error in getStaticImageUrl:', error);
		return DEFAULT_STATIC_FALLBACK;
	}
}
