import { Cache, Duration, Effect } from "effect";
import { httpClient, responseAsText } from "../../utils/http";
import { createAssetUrlWithBasePath } from "../../utils/url";
import { createValidSVGString, SVGString } from "../../utils/validation/svg";
export const emptyDefaultSvg = () => SVGString(`<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"></svg>`);
const createIconCache = () => Cache.make({
    capacity: 200,
    timeToLive: Duration.infinity,
    lookup: (key) => Effect.gen(function* () {
        const name = key;
        const assetPath = yield* createAssetUrlWithBasePath(`./icons/${name}.svg`);
        const res = yield* httpClient(assetPath);
        const responseString = yield* responseAsText(res);
        return yield* createValidSVGString(responseString);
    }),
});
let cacheInstance = null;
export function fetchIconSvg(name) {
    const key = name;
    return Effect.gen(function* () {
        if (!cacheInstance) {
            cacheInstance = yield* createIconCache();
        }
        return yield* cacheInstance.get(key);
    });
}
export function clearIconCache() {
    return Effect.gen(function* () {
        if (cacheInstance) {
            yield* cacheInstance.invalidateAll;
        }
        else {
            Effect.logWarning('Attempted to clear icon cache before it was initialized');
        }
    });
}
