// Share a webapp with the public internet.

import { apps } from "@/webapp"
import { connectSneaker, sneakers, sneakerUrl } from "@/sneaker"

export default async function (app: string) {
  if (!app) {
    let out = `usage: share &lt;app&gt; [subdomain]`
    const apps = sneakers()
    if (apps.length) {
      out += "\n\nUse `unshare` to stop sharing an app."
      out += "\n\nsharing\n" + apps.map(app => {
        const url = sneakerUrl(app)
        return `${app}: <a href="${url}">${url}</a>`
      }).join("\n")
    }
    return { html: out }
  }

  if (!apps().includes(app)) {
    return { error: `${app} not found` }
  }

  const url = sneakerUrl(await connectSneaker(app))
  return { html: `<a href="${url}">${url}</a>` }
}