import { Tty } from "./Tty.ts";

/**
 * @copyright Martin Flogaus 2026
 */
export class Terminal implements FrontendInterface {
    constructor() {
        this.registerCommand("init", function (this: Tty) {
            this.isRunningACommand = false;
            this.init();
            return 0;
        });
        setTimeout(async () => {
            await import("./Commands/index.ts")
            let observer = new IntersectionObserver(function (entries) {
                for (let entry of entries) {
                    const target = entry.target as HTMLDivElement & { terminal: Tty };
                    if (!target.terminal) target.terminal = new Tty(target, JSON.parse(target.dataset.options));
                    entry.isIntersecting && target.terminal.init();
                }
            });
            for (let terminal of this.terminals) {
                observer.observe(terminal);
            }
        });
    }
    isVisibleInViewport(element) {
        var rect = element.getBoundingClientRect();

        return rect.top >= 0 && rect.left >= 0 && rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) /*or $(window).height() */ && rect.right <= (window.innerWidth || document.documentElement.clientWidth) /*or $(window).width() */;
    }

    commands: Map<string, Function> = new Map();

    get terminals(): NodeListOf<HTMLDivElement> {
        return document.querySelectorAll(".terminal-body");
    }

    registerCommand(commandName: string, executor: Function) {
        this.commands.set(commandName, executor);
    }

    fs = {
        bin: {},
        boot: {},
        dev: {},
        etc: {},
        home: {
            visitor: {},
        },
        lib: {},
        lib64: {},
        media: {},
        mnt: {},
        opt: {},
        proc: {},
        usr: {},
        var: {
            www: {
                httpdocs: {
                    public: {
                        "index.php": "file",
                    },
                },
            },
        },
    };
}
