"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const react_1 = __importStar(require("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.
const blurhash_1 = require("blurhash");
const props_1 = require("./props");
const worker_1 = __importDefault(require("./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 = (0, react_1.useRef)();
    const offCanvasRef = (0, react_1.useRef)();
    const isTransferedCanvasRef = (0, react_1.useRef)();
    const id = (0, react_1.useMemo)(() => ++idGen, []);
    (0, react_1.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_1.default.postMessage(isTransfered ? msg : { ...msg, canvas: offCanvas }, isTransfered ? [] : [offCanvas]);
        isTransferedCanvasRef.current = true;
    }, [hash, width, height, punch]);
    return react_1.default.createElement("canvas", { ...props, height: height, width: width, ref: ref });
};
const BlurhashCanvasFallback = ({ loading, hash, width, height, punch, imageRef, ...props }) => {
    const ref = (0, react_1.useRef)();
    (0, react_1.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 = (0, blurhash_1.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_1.default.createElement("canvas", { ...props, height: height, width: width, ref: ref });
};
const BlurhashCanvas = ({ loading, ...props }) => {
    const canUseWorker = isOffscreenSupport && !!worker_1.default && loading === 'lazy';
    const Component = canUseWorker ? BlurhashCanvasWorker : BlurhashCanvasFallback;
    return react_1.default.createElement(Component, { loading: loading, ...props });
};
BlurhashCanvas.defaultProps = props_1.blurhashCanvasDefaultProps;
exports.default = BlurhashCanvas;
//# sourceMappingURL=BlurhashCanvas.js.map