import { safeJsonStringify } from './utils/htmlEscape'
import type { One } from './vite/types'

export const isWebClient =
  process.env.TAMAGUI_TARGET !== 'native' && typeof window !== 'undefined'

export const isWebServer =
  process.env.TAMAGUI_TARGET !== 'native' && typeof window === 'undefined'

export const isNative = process.env.TAMAGUI_TARGET === 'native'

/**
 * True only in a browser main-thread context with a navigable history —
 * i.e. `window` AND `window.history` are available. Excludes native,
 * SSR, web workers / service workers (no `window`), and exotic sandboxed
 * environments where `window` exists but history is stripped. Use this
 * for guarding any `window.history` / `window.location` access so
 * intercept routes, URL masking, etc. don't assume a full browser.
 */
export const hasWebHistory =
  isWebClient &&
  typeof window.history !== 'undefined' &&
  typeof window.location !== 'undefined'

export const CACHE_KEY = `${process.env.ONE_CACHE_KEY ?? Math.round(Math.random() * 100_000_000)}`

export const LOADER_JS_POSTFIX_UNCACHED = `_vxrn_loader.js`
export const LOADER_JS_POSTFIX_REGEX_STRING = `_\\d+${LOADER_JS_POSTFIX_UNCACHED}$`
export const LOADER_JS_POSTFIX_REGEX = new RegExp(LOADER_JS_POSTFIX_REGEX_STRING)
export const LOADER_JS_POSTFIX = `_${CACHE_KEY}${LOADER_JS_POSTFIX_UNCACHED}`

export const PRELOAD_JS_POSTFIX = `_${CACHE_KEY}_preload.js`
export const CSS_PRELOAD_JS_POSTFIX = `_${CACHE_KEY}_preload_css.js`

// safari insanely aggressive caching
export const VIRTUAL_SSR_CSS_ENTRY = `virtual:ssr-css.css`
export const VIRTUAL_SSR_CSS_HREF = `/@id/__x00__${VIRTUAL_SSR_CSS_ENTRY}`

export const SERVER_CONTEXT_KEY = '__one_server_context__'

export const getSpaHeaderElements = ({
  serverContext = {},
}: {
  serverContext?: One.ServerContext
} = {}) => `
  <script>globalThis['global'] = globalThis</script>
  <script>globalThis['__vxrnIsSPA'] = true</script>
  <script>globalThis["${SERVER_CONTEXT_KEY}"] = ${safeJsonStringify(serverContext)}</script>
  <script>globalThis.__oneLoadedCSS = new Set(${safeJsonStringify(serverContext.css || [])})</script>
`
