declare global {
	interface Window {
		__PDFDRAFT_PDF_INVOICES_FOR_WOOCOMMERCE__?: {
			pluginUrl?: string;
		};
	}
}

const ctx = require.context('@/images', false, /\.(webp|png|jpe?g)$/i);
const IMAGE_MANIFEST: Record<string, string> = Object.fromEntries(
	ctx.keys().map( ( key ) => [ key.replace( './', '' ), ctx( key ) as string ] ),
);

export function getPreviewImageUrl( filename: string ): string | undefined {
	if ( IMAGE_MANIFEST[ filename ] ) return IMAGE_MANIFEST[ filename ];

	const base = window.__PDFDRAFT_PDF_INVOICES_FOR_WOOCOMMERCE__?.pluginUrl;
	if ( ! base ) return undefined;
	return `${ base }/dist/images/${ filename }`;
}
