var __read = (this && this.__read) || function (o, n) {
    var m = typeof Symbol === "function" && o[Symbol.iterator];
    if (!m) return o;
    var i = m.call(o), r, ar = [], e;
    try {
        while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
    }
    catch (error) { e = { error: error }; }
    finally {
        try {
            if (r && !r.done && (m = i["return"])) m.call(i);
        }
        finally { if (e) throw e.error; }
    }
    return ar;
};
import { Fragment as _Fragment, jsx as _jsx } from "react/jsx-runtime";
import { useState, useEffect } from 'react';
import UTIF from 'utif';
var TiffViewer = function (_a) {
    var src = _a.src;
    var _b = __read(useState(null), 2), imageUrl = _b[0], setImageUrl = _b[1];
    var _c = __read(useState(null), 2), isTiff = _c[0], setIsTiff = _c[1];
    var _d = __read(useState(true), 2), loading = _d[0], setLoading = _d[1];
    useEffect(function () {
        if (!src)
            return;
        setLoading(true);
        var xhr = new XMLHttpRequest();
        xhr.open('GET', src);
        xhr.responseType = 'arraybuffer';
        xhr.onload = function (e) {
            try {
                var response = e.target.response;
                var contentType = xhr.getResponseHeader('Content-Type');
                if (contentType &&
                    (contentType.includes('image/tiff') || contentType.includes('image/tif'))) {
                    setIsTiff(true);
                    var ifds = UTIF.decode(response);
                    UTIF.decodeImage(response, ifds[0]);
                    var rgba = UTIF.toRGBA8(ifds[0]);
                    var imageWidth = ifds[0].width;
                    var imageHeight = ifds[0].height;
                    var cnv = document.createElement('canvas');
                    cnv.width = imageWidth;
                    cnv.height = imageHeight;
                    var ctx = cnv.getContext('2d');
                    if (ctx) {
                        var imageData = ctx.createImageData(imageWidth, imageHeight);
                        imageData.data.set(rgba);
                        ctx.putImageData(imageData, 0, 0);
                        var dataUrl = cnv.toDataURL('image/png');
                        setImageUrl(dataUrl);
                    }
                }
                else {
                    setIsTiff(false);
                }
            }
            catch (error) {
                console.error('Error processing TIFF file', error);
                setIsTiff(false);
            }
            finally {
                setLoading(false);
            }
        };
        xhr.onerror = function (e) {
            console.error('Error loading file', e);
            setIsTiff(false);
            setLoading(false);
        };
        xhr.send();
    }, [src]);
    if (loading) {
        return _jsx(_Fragment, {});
    }
    return isTiff ? _jsx("img", { src: imageUrl || '', alt: "TIFF" }) : _jsx("img", { src: src, alt: "Image" });
};
export default TiffViewer;
