import { AbstractTextureLoader } from '../loader.js';
import { getDevice } from '../../../app/api.js';

/**
 * Web image loader
 * @internal
 */ class WebImageLoader extends AbstractTextureLoader {
    supportMIMEType(mimeType) {
        return mimeType === 'image/jpeg' || mimeType === 'image/png' || mimeType === 'image/webp';
    }
    async load(mimeType, data, srgb, samplerOptions, texture) {
        if (!mimeType) {
            throw new Error('unknown image file type');
        }
        const blob = new Blob([
            data
        ], {
            type: mimeType
        });
        const options = {
            texture,
            samplerOptions
        };
        try {
            const bm = await createImageBitmap(blob, {
                premultiplyAlpha: 'none'
            });
            const tex = getDevice().createTexture2DFromImage(bm, srgb, options);
            if (!tex) {
                throw new Error('create texture from ImageBitmap failed');
            }
            bm.close?.();
            return tex;
        } catch  {
            const src = URL.createObjectURL(blob);
            try {
                const img = await this.loadHTMLImage(src);
                const cvs = document.createElement('canvas');
                cvs.width = img.naturalWidth || img.width;
                cvs.height = img.naturalHeight || img.height;
                const ctx = cvs.getContext('2d');
                if (!ctx) {
                    throw new Error('2D context not available');
                }
                ctx.drawImage(img, 0, 0);
                const tex = getDevice().createTexture2DFromImage(cvs, srgb, options);
                if (!tex) {
                    throw new Error('create texture from canvas failed');
                }
                return tex;
            } finally{
                URL.revokeObjectURL(src);
            }
        }
    }
    async loadHTMLImage(src) {
        return new Promise((resolve, reject)=>{
            const img = new Image();
            img.onload = ()=>resolve(img);
            img.onerror = (err)=>reject(err);
            img.src = src;
        });
    }
}

export { WebImageLoader };
//# sourceMappingURL=webimage_loader.js.map
