import { encode } from 'proquint'
import { randomBytes } from 'crypto'

import { get_all_threads, create_thread } from '$lib/db'

export async function load({ fetch, data, setHeaders, parent, depends}) {
	console.log("thread")
	let threads = await get_all_threads()
	for( let thread of threads ){
		thread.name = encode(Buffer.from(thread.id, "hex"))
	}

	return {
		threads,
		uid: 42
	}
}
export const actions = {
  default: async({ request }) => {
    console.log("create thread")
    let data = await request.formData()
    console.log(data)
    await create_thread(data.get("title"), randomBytes(4), data.get("uid"))
  }
}