

// REMOVE once we switch to TypeScript 5.x
// OffscreenCanvas has a convertToBlob method, but TypeScript 4.x doesn't have the proper types for it.

/** @internal */
export declare type OffscreenCanvasExt = OffscreenCanvas & {
	convertToBlob: (options?: any) => Promise<Blob>;
}

// REMOVE once usage of browsers that don't support OffscreenCanvas is low
// Shim for missing OffscreenCanvas in iOS 16.x
export function initShims() {
	if (typeof globalThis !== undefined && !('OffscreenCanvas' in globalThis)) {
		// @ts-ignore
		globalThis['OffscreenCanvas'] = class OffscreenCanvas  {
			canvas: HTMLCanvasElement;
			constructor(width, height) {
				this.canvas = document.createElement("canvas");
				this.canvas.width = width;
				this.canvas.height = height;

				// @ts-ignore
				this.canvas.convertToBlob = (type?: string, quality?: any) => {
					return new Promise(resolve => {
						this.canvas.toBlob(resolve, type, quality);
					});
				};

				// @ts-ignore
				return this.canvas;
			}
		};
	}
}