import React, { useEffect, useRef, useMemo } from 'react';
// Use blurhash instead of fast-blurhash, because it's an import for version without webworker (Safari).
// And on Safari fast-blurhash is slower than blurash.
import { decode } from 'blurhash';
import { blurhashCanvasDefaultProps } from './props';
import worker from './worker';
// @ts-ignore [sto lat za cywilizowanym swiatem]
const isOffscreenSupport = typeof OffscreenCanvas !== 'undefined';
let idGen = 0;
const BlurhashCanvasWorker = ({ loading, hash, width, height, punch, imageRef, ...props }) => {
    const ref = useRef();
    const offCanvasRef = useRef();
    const isTransferedCanvasRef = useRef();
    const id = useMemo(() => ++idGen, []);
    useEffect(() => {
        var _a;
        if ((_a = imageRef === null || imageRef === void 0 ? void 0 : imageRef.current) === null || _a === void 0 ? void 0 : _a.complete)
            return;
        const canvas = ref.current;
        if (!canvas)
            return;
        const isTransfered = isTransferedCanvasRef.current;
        if (!isTransfered) {
            // @ts-ignore [HTMLCanvasElement 2077]
            offCanvasRef.current = canvas.transferControlToOffscreen();
        }
        const offCanvas = offCanvasRef.current;
        const msg = { width, height, punch, hash, id };
        worker.postMessage(isTransfered ? msg : { ...msg, canvas: offCanvas }, isTransfered ? [] : [offCanvas]);
        isTransferedCanvasRef.current = true;
    }, [hash, width, height, punch]);
    return React.createElement("canvas", { ...props, height: height, width: width, ref: ref });
};
const BlurhashCanvasFallback = ({ loading, hash, width, height, punch, imageRef, ...props }) => {
    const ref = useRef();
    useEffect(() => {
        var _a;
        if ((_a = imageRef === null || imageRef === void 0 ? void 0 : imageRef.current) === null || _a === void 0 ? void 0 : _a.complete)
            return;
        const draw = () => {
            const canvas = ref.current;
            if (!canvas)
                return;
            const pixels = decode(hash, width, height, punch);
            const ctx = canvas.getContext('2d');
            const imageData = ctx.createImageData(width, height);
            imageData.data.set(pixels);
            ctx.putImageData(imageData, 0, 0);
        };
        if (loading === 'lazy') {
            const timeout = setTimeout(draw, 0);
            return () => clearTimeout(timeout);
        }
        else {
            draw();
        }
    }, [hash, width, height, punch]);
    return React.createElement("canvas", { ...props, height: height, width: width, ref: ref });
};
const BlurhashCanvas = ({ loading, ...props }) => {
    const canUseWorker = isOffscreenSupport && !!worker && loading === 'lazy';
    const Component = canUseWorker ? BlurhashCanvasWorker : BlurhashCanvasFallback;
    return React.createElement(Component, { loading: loading, ...props });
};
BlurhashCanvas.defaultProps = blurhashCanvasDefaultProps;
export default BlurhashCanvas;
//# sourceMappingURL=BlurhashCanvas.js.map