import { SVGString } from "@lmvz-ds/lib-ts/validation/svg.js";
import { beforeAll, beforeEach, describe, expect, test, vi } from "@stencil/vitest";
import { emptyDefaultSvg, resolveIconSvg } from "./icons";
import { registerIconProvider } from "./icons-registry";
const fetchMock = vi.fn();
describe('icon utils:', () => {
    beforeAll(() => {
        vi.stubGlobal('DOMParser', class {
            parseFromString(str, _type) {
                return {
                    documentElement: {
                        nodeName: typeof str === 'string' && str.trimStart().startsWith('<svg') ? 'svg' : 'not-svg',
                    },
                };
            }
        });
    });
    beforeEach(() => {
        vi.clearAllMocks();
        fetchMock.mockReset();
        vi.stubGlobal('fetch', fetchMock);
    });
    const validSvg = '<svg width="24" height="24" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"></svg>';
    const invalidSvg = '<div>not an svg</div>';
    test('SVGString branding enforces valid SVG', () => {
        expect(() => SVGString(validSvg)).not.toThrow();
        expect(() => SVGString(invalidSvg)).toThrow();
        expect(() => SVGString('')).toThrow();
        expect(() => SVGString(123)).toThrow();
    });
    test('emptyDefaultSvg is a valid branded SVGString', () => {
        expect(() => SVGString(emptyDefaultSvg())).not.toThrow();
        expect(emptyDefaultSvg()).toContain('<svg');
    });
    test('resolveIconSvg fails loading "book" with no iconset/provider and falls back to default', async () => {
        const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => undefined);
        const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => undefined);
        const resolved = await resolveIconSvg({ icon: 'book' });
        expect(resolved).toBe(emptyDefaultSvg());
        expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining('No icon provider registered.'));
        expect(errorSpy).toHaveBeenCalledWith(expect.stringContaining('Using default icon.'));
        warnSpy.mockRestore();
        errorSpy.mockRestore();
    });
    test('resolveIconSvg resolves icon from a valid iconset provider', async () => {
        registerIconProvider('test-set-success', {
            resolve(name) {
                if (name === 'test-icon') {
                    return { name, svg: validSvg };
                }
                return undefined;
            },
        });
        const resolved = await resolveIconSvg({ iconset: 'test-set-success', icon: 'test-icon' });
        expect(resolved).toContain('<svg');
        expect(() => SVGString(resolved)).not.toThrow();
    });
    test('resolveIconSvg falls back when icon is missing in a valid iconset provider', async () => {
        const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => undefined);
        const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => undefined);
        registerIconProvider('test-set-miss', {
            resolve(name) {
                return name;
            },
        });
        const resolved = await resolveIconSvg({ iconset: 'test-set-miss', icon: 'book' });
        expect(resolved).toBe(emptyDefaultSvg());
        expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining('not found in provider'));
        expect(errorSpy).toHaveBeenCalledWith(expect.stringContaining('Using default icon.'));
        warnSpy.mockRestore();
        errorSpy.mockRestore();
    });
});
