import { nativeGetAppearance, nativeStartListener, nativeStopListener, closeLib, } from "./ffi.js";
export { terminal } from "./terminal.js";
class AppearanceImpl {
    listeners = new Set();
    listening = false;
    async current() {
        return nativeGetAppearance();
    }
    async on(event, listener) {
        if (event !== "change")
            return;
        this.listeners.add(listener);
        if (!this.listening) {
            this.listening = true;
            await nativeStartListener((modeInt) => {
                const mode = modeInt === 1 ? "dark" : "light";
                for (const fn of this.listeners) {
                    try {
                        fn(mode);
                    }
                    catch (_) {
                        // Don't let one listener break others
                    }
                }
            });
        }
    }
    async off(event, listener) {
        if (event !== "change")
            return;
        this.listeners.delete(listener);
        if (this.listeners.size === 0 && this.listening) {
            this.listening = false;
            await nativeStopListener();
        }
    }
    async dispose() {
        if (this.listening) {
            this.listening = false;
            await nativeStopListener();
        }
        this.listeners.clear();
        await closeLib();
    }
}
/** Singleton appearance instance */
export const appearance = new AppearanceImpl();
