import { createValidSVGString, SVGString as SVGFactory } from "@lmvz-ds/lib-ts/validation/svg.js";
import { Micro } from "effect";
import { getRegisteredIconProvider } from "./icons-registry";
export const emptyDefaultSvg = () => SVGFactory(`<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"></svg>`);
async function toValidSvgStringWithFallback(value) {
    try {
        return await Micro.runPromise(createValidSVGString(value));
    }
    catch {
        console.error('Invalid SVG string:', value);
        return emptyDefaultSvg();
    }
}
export async function resolveIconSvg(options) {
    const { icon, iconset } = options;
    const iconSvg = await (async () => {
        const provider = getRegisteredIconProvider(iconset);
        if (!provider) {
            console.warn(`No icon provider registered. Unable to resolve icon "${icon}"${iconset ? ` from set "${iconset}"` : ''}. Using default icon.`);
            return;
        }
        try {
            const resolved = await Promise.resolve(provider.resolve(icon));
            if (!resolved?.svg) {
                console.warn(`Icon "${icon}"${iconset ? ` from set "${iconset}"` : ''} not found in provider. Using default icon.`);
                return;
            }
            const validated = await toValidSvgStringWithFallback(resolved.svg);
            if (validated) {
                return validated;
            }
        }
        catch (error) {
            console.error(`Error resolving icon "${icon}" with provider:`, error);
            return;
        }
        return;
    })();
    if (iconSvg)
        return iconSvg;
    console.error(`Icon "${icon}" not found${iconset ? ` in set "${iconset}"` : ''}. Using default icon.`);
    return emptyDefaultSvg();
}
export function typedIconFromSet(iconset, icon) {
    return {
        iconset,
        icon,
    };
}
