import { isLocalNetwork } from "../engine_networking_utils.js";
import { getParam } from "../engine_utils.js";
import { addLog, BalloonOptions, clearMessages, LogType, setAllowBalloonMessages, setAllowOverlayMessages } from "./debug_overlay.js";

export {
    clearMessages as clearBalloonMessages,
    /** @deprecated use clearBalloonMessages instead */
    clearMessages as clearOverlayMessages,
    LogType,
    setAllowBalloonMessages,
    // eslint-disable-next-line @typescript-eslint/no-deprecated
    setAllowOverlayMessages,
};
export { enableSpatialConsole } from "./debug_spatial_console.js";

const noDevLogs = getParam("nodevlogs");


/** Displays a debug message on screen for a certain amount of time */
export function showBalloonMessage(text: string, options?: Partial<BalloonOptions>): void {
    addLog(options?.type ?? LogType.Log, text, options);
}

/** Displays a warning message on screen for a certain amount of time */
export function showBalloonWarning(text: string, options?: Partial<BalloonOptions>): void {
    showBalloonMessage(text, { ...options, type: LogType.Warn });
}
/** Displays an error message on screen for a certain amount of time */
export function showBalloonError(text: string, options?: Partial<BalloonOptions>): void {
    showBalloonMessage(text, { ...options, type: LogType.Error });
}

let _manuallySetDevEnvironment: boolean | undefined;
let _cachedDevEnvironment: boolean | undefined;

/** True when the application runs on a local url */
export function isDevEnvironment(): boolean {
    if (noDevLogs) return false;
    if (_manuallySetDevEnvironment !== undefined) return _manuallySetDevEnvironment;
    if (_cachedDevEnvironment !== undefined) return _cachedDevEnvironment;

    let res = isLocalNetwork();
    if (!res) {
        // is stackblitz?
        res = window.location.hostname.endsWith(".local-credentialless.webcontainer.io");
    }
    _cachedDevEnvironment = res;
    return res;
}
/** Enforce the dev environment flag to be true or false */
export function setDevEnvironment(val: boolean): void {
    _manuallySetDevEnvironment = val;
}