import { createWidgetStore } from "@src/lib/markdown/editor/widgets/store";
import { describe, expect, it } from "vitest";

describe("createWidgetStore", () => {
    it("unregisterHost removes only the portal whose host identity matches registration", () => {
        const store = createWidgetStore();
        const host214 = document.createElement("span");
        const host227 = document.createElement("span");
        store.register({
            portalKey: 1,
            id: "list-bullet:214",
            host: host214,
            render: () => null,
        });
        store.register({
            portalKey: 2,
            id: "list-bullet:227",
            host: host227,
            render: () => null,
        });
        expect(store.getSnapshot()).toHaveLength(2);

        expect(store.hasPortalKey(1)).toBe(true);
        expect(store.hasPortalKey(99)).toBe(false);

        store.unregisterHost(host214);
        expect(store.getSnapshot()).toHaveLength(1);
        expect(store.getSnapshot()[0]?.id).toBe("list-bullet:227");

        store.unregisterHost(host227);
        expect(store.getSnapshot()).toHaveLength(0);
    });

    it("same logical id on two hosts keeps two portals until each host unregisters", () => {
        const store = createWidgetStore();
        const hostA = document.createElement("span");
        const hostB = document.createElement("span");
        store.register({
            portalKey: 10,
            id: "list-bullet:214",
            host: hostA,
            render: () => null,
        });
        store.register({
            portalKey: 11,
            id: "list-bullet:214",
            host: hostB,
            render: () => null,
        });
        expect(store.getSnapshot()).toHaveLength(2);

        store.unregisterHost(hostA);
        expect(store.getSnapshot()).toHaveLength(1);
        expect(store.getSnapshot()[0]?.host).toBe(hostB);

        store.unregisterHost(hostB);
        expect(store.getSnapshot()).toHaveLength(0);
    });

    it("re-registering the same host evicts the previous portalKey (one portal per host)", () => {
        const store = createWidgetStore();
        const host = document.createElement("span");
        store.register({
            portalKey: 1,
            id: "checkbox:2-5",
            host,
            render: () => null,
        });
        expect(store.getSnapshot()).toHaveLength(1);

        store.register({
            portalKey: 2,
            id: "checkbox:2-5",
            host,
            render: () => null,
        });
        expect(store.getSnapshot()).toHaveLength(1);
        expect(store.getSnapshot()[0]?.portalKey).toBe(2);
        expect(store.hasPortalKey(1)).toBe(false);
        expect(store.hasPortalKey(2)).toBe(true);
    });

    it("unregisterHost with expectedPortalKey skips when a newer registration owns the host", () => {
        const store = createWidgetStore();
        const host = document.createElement("span");
        store.register({
            portalKey: 10,
            id: "checkbox:2-5",
            host,
            render: () => null,
        });
        store.register({
            portalKey: 11,
            id: "checkbox:2-5",
            host,
            render: () => null,
        });
        store.unregisterHost(host, 10);
        expect(store.getSnapshot()).toHaveLength(1);
        expect(store.getSnapshot()[0]?.portalKey).toBe(11);

        store.unregisterHost(host, 11);
        expect(store.getSnapshot()).toHaveLength(0);
    });
});
