import { TOUR_TARGET_WAIT_MS } from '@/lib/application-tour/constants';

export async function waitForTarget(
    selector: string,
    timeoutMs = TOUR_TARGET_WAIT_MS,
): Promise<HTMLElement | null> {
    const startedAt = Date.now();

    while (Date.now() - startedAt < timeoutMs) {
        const element = document.querySelector<HTMLElement>(selector);

        if (element) {
            return element;
        }

        await new Promise((resolve) => window.setTimeout(resolve, 100));
    }

    return null;
}

export function isOnRoute(route?: string): boolean {
    if (!route) {
        return true;
    }

    const currentPath = window.location.pathname;
    const targetPath = new URL(route, window.location.origin).pathname;

    return currentPath === targetPath;
}
