////
// NOSE webapps

import { sessionStore } from "./session"

export const apps: string[] = []

export function cacheApps(a: string[]) {
  apps.length = 0
  apps.unshift(...a)
  apps.sort()

  window.dispatchEvent(new CustomEvent("apps:change"))
}

export function currentAppUrl(): string | undefined {
  const project = sessionStore.get("project") || "root"

  if (!apps.includes(project)) return

  const hostname = sessionStore.get("hostname") || "localhost"
  const s = hostname.startsWith("localhost") ? "" : "s"
  return `http${s}://${project}.${hostname}`
}