/**
 * The bookmarklet to enable recommended debugging is:
 *
 * ```
 * javascript:(() => {
 *   globalThis.devvit ??= {};
 *   globalThis.devvit.debug ??= {};
 *   globalThis.devvit.devvitDebug ??= [];
 *   globalThis.devvit.debug.runtime =  true;
 *   globalThis.devvit.devvitDebug.push('runtime');
 *   const url = new URL(location.href);
 *   url.searchParams.set('devvitdebug', 'runtime');
 *   history.replaceState(undefined, '', url);
 *   console.log(`devvit debug enabled`, globalThis.devvit);
 * })()
 * ```
 */
export function initDevvitGlobal() {
    var _a, _b, _c;
    // `globalThis.location` check allows Node.js usage.
    const url = new URL(globalThis.location ? location.href : 'file://');
    // Initialize defensively to honor any initializations that might occur in a
    // temporary hack elsewhere.
    globalThis.devvit ?? (globalThis.devvit = {});
    if (globalThis.devvit.debug)
        return;
    // Roughly aligns to getContextFromMetadata().
    (_a = globalThis.devvit).debug ?? (_a.debug = {});
    // All known AppDebug keys.
    const keyset = {
        blocks: undefined,
        emitSnapshots: undefined,
        emitState: undefined,
        realtime: undefined,
        runtime: undefined,
        surface: undefined,
        useAsync: undefined,
        payments: undefined,
        bootstrap: undefined,
    };
    // {[key: Lowercase<AppDebug>]: AppDebug}
    const lowerKeyToKey = {};
    for (const key in keyset)
        lowerKeyToKey[key.toLowerCase()] = key;
    let devvitDebug = [...url.searchParams].find(([name]) => name.toLowerCase() === 'devvitdebug')?.[1];
    if (devvitDebug == null)
        return;
    // empty means enable almost everything.
    if (devvitDebug === '')
        devvitDebug = Object.keys(keyset)
            .filter((key) => key !== 'emitSnapshots' && key !== 'emitState')
            .join();
    for (const kv of devvitDebug.split(',') ?? []) {
        let [key, val] = kv.split('='); // eslint-disable-line prefer-const
        if (!key)
            continue;
        if (key.toLowerCase() in lowerKeyToKey)
            key = lowerKeyToKey[key.toLowerCase()];
        else
            console.warn(`[devvit] unknown devvitdebug parameter "${key}"`);
        (_b = globalThis.devvit.debug)[_c = key] ?? (_b[_c] = val || `${true}`);
        console.info(`[devvit] set devvitdebug ${key}=${globalThis.devvit.debug[key]}`);
    }
}
//# sourceMappingURL=devvit-global.js.map