/**
 * WordPress's media modal (`wp.media`, the `media-editor` script), opened to choose one image: only the parts PrintBlob's
 * and PrintBlob Pro's admin scripts use. WordPress publishes no type package for it.
 */
export interface MediaImage {
	id: number;
	url: string;
	width?: number;
	height?: number;
	sizes?: Partial< Record< 'thumbnail' | 'large', { url: string } > >;
}

export interface MediaLabels {
	title: string;
	button: string;
}

interface MediaFrame {
	on( event: 'select', callback: () => void ): void;
	open(): void;
	state(): { get( name: 'selection' ): { first(): { toJSON(): MediaImage } } };
}

declare const wp: {
	media( options: { title: string; button: { text: string }; library: { type: 'image' }; multiple: false } ): MediaFrame;
};

export function chooseImage( labels: MediaLabels, chosen: ( image: MediaImage ) => void ): void {
	const frame = wp.media( { title: labels.title, button: { text: labels.button }, library: { type: 'image' }, multiple: false } );
	frame.on( 'select', () => chosen( frame.state().get( 'selection' ).first().toJSON() ) );
	frame.open();
}
