import { memoize } from "../../../utils/memoize.js";
class IconFetchError extends Error {
    static fromStatusCode(url, statusCode, statusText) {
        return new IconFetchError(url, `status code ${statusCode} (${statusText})`);
    }
    static fromError(url, error) {
        return new IconFetchError(url, 'an error', error);
    }
    constructor(url, errorMessage, errorObject){
        super(`Could not fetch icon from ${url}, got ${errorMessage}.`), this.url = url, this.errorObject = errorObject;
    }
}
const fetchIconUnmemoized = async (url)=>fetch(url).catch((e)=>{
        throw IconFetchError.fromError(url, e);
    }).then((response)=>{
        if (200 !== response.status && 304 !== response.status) throw IconFetchError.fromStatusCode(url, response.status, response.statusText);
        return response.text();
    });
const memoizedFetchIcon = memoize(fetchIconUnmemoized, (url)=>url, {
    maxEntries: 20
});
const fetchIcon = memoizedFetchIcon.fn;
function clearIconCache() {
    memoizedFetchIcon.clearCache();
}
export { clearIconCache, fetchIcon };
