"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "NetworkImage", {
    enumerable: true,
    get: function() {
        return NetworkImage;
    }
});
const _jsxruntime = require("react/jsx-runtime");
const _css = require("../utils/css");
const _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
const _utils = require("./utils");
const _icons = require("./icons");
function _getRequireWildcardCache(nodeInterop) {
    if (typeof WeakMap !== "function") return null;
    var cacheBabelInterop = new WeakMap();
    var cacheNodeInterop = new WeakMap();
    return (_getRequireWildcardCache = function(nodeInterop) {
        return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
    })(nodeInterop);
}
function _interop_require_wildcard(obj, nodeInterop) {
    if (!nodeInterop && obj && obj.__esModule) {
        return obj;
    }
    if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
        return {
            default: obj
        };
    }
    var cache = _getRequireWildcardCache(nodeInterop);
    if (cache && cache.has(obj)) {
        return cache.get(obj);
    }
    var newObj = {
        __proto__: null
    };
    var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
    for(var key in obj){
        if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
            var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
            if (desc && (desc.get || desc.set)) {
                Object.defineProperty(newObj, key, desc);
            } else {
                newObj[key] = obj[key];
            }
        }
    }
    newObj.default = obj;
    if (cache) {
        cache.set(obj, newObj);
    }
    return newObj;
}
const NetworkImage = /*#__PURE__*/ (0, _react.memo)((props)=>{
    const { rounded = true } = props;
    const [url, setUrl] = _react.default.useState();
    const [loading, setLoading] = (0, _react.useState)(false);
    const [failed, setFailed] = (0, _react.useState)(false);
    const [isPlaceholder, setIsPlacholder] = (0, _react.useState)(()=>props.type === "placeholder");
    const currentUrl = (0, _react.useRef)();
    (0, _react.useEffect)(()=>{
        if (typeof props.type === "undefined" && typeof props.symbol === "undefined") {
            throw new Error("NetworkImage must have a type or symbol");
        }
        if (props.type === "placeholder" || props.type === "unknown") {
            return;
        }
        const img = new Image();
        setLoading(true);
        img.onload = function() {
            if (currentUrl.current !== img.src) {
                return;
            }
            setUrl(img.src);
            setLoading(false);
            setFailed(false);
        };
        img.onerror = function() {
            // setIsPlacholder(true);
            setFailed(true);
            setLoading(false);
        };
        // if (props.type === "token") {
        // }
        try {
            if (props.type === "symbol" || props.type === "token") {
                let name = props.name;
                if (typeof props.symbol === "string") {
                    const arr = props.symbol?.split("_");
                    name = arr[1];
                }
                // coin logos
                img.src = `https://oss.orderly.network/static/symbol_logo/${name}.png`;
            }
            if (props.type === "chain") {
                img.src = `https://oss.orderly.network/static/network_logo/${props.id}.png`;
            }
            if (props.type === "wallet") {
                const split = props.name?.split(" ");
                const formatWalletName = split?.[0]?.toLowerCase();
                img.src = `https://oss.orderly.network/static/wallet_icon/${formatWalletName}.png`;
            }
            if (props.type === "path") {
                img.src = props.path;
            }
            currentUrl.current = img.src;
        } catch (e) {
            setIsPlacholder(true);
        }
    // crypto logos
    // https://cryptologos.cc/logos/
    // img.src = `https://cryptologos.cc/logos/${props.name.toLowerCase()}-${props.size}.png?v=010`;
    }, [
        props.type,
        props.symbol,
        props.name,
        props.id
    ]);
    const icon = (0, _react.useMemo)(()=>{
        // if (failed) {
        if (props.type === "unknown") {
            return /*#__PURE__*/ (0, _jsxruntime.jsx)("span", {
                className: "orderly-text-base-contrast/50 orderly-text-[10px]",
                children: "U"
            });
        }
        // }
        if (!url) {
            return null;
        }
        return /*#__PURE__*/ (0, _jsxruntime.jsx)("img", {
            src: url,
            alt: props.name
        });
    }, [
        url,
        failed,
        props.type
    ]);
    const size = (0, _react.useMemo)(()=>{
        return (0, _utils.getSize)(props.size);
    }, [
        props.size
    ]);
    if (isPlaceholder) {
        return /*#__PURE__*/ (0, _jsxruntime.jsx)(_icons.WalletIcon, {
            size: 20
        });
    }
    return /*#__PURE__*/ (0, _jsxruntime.jsx)("div", {
        className: (0, _css.cn)("orderly-inline-flex orderly-overflow-hidden orderly-leading-none orderly-text-center orderly-items-center orderly-justify-center", (isPlaceholder || loading) && "orderly-bg-slate-200", rounded && "orderly-rounded-full", loading && "orderly-animate-pulse", (failed || props.type === "unknown") && "orderly-bg-base-100", props.className),
        style: {
            width: size,
            height: size,
            backgroundColor: props.backgroundColor
        },
        children: icon
    });
});
NetworkImage.displayName = "NetworkImage";

//# sourceMappingURL=networkImage.js.map