/**
 * This script downloads the Deno types from the GitHub release page and saves them to the lib directory.
 * It is used to provide the Deno types to the Monaco editor in Studio for the Edge Functions AI editor.
 *
 * Deno Releases: https://github.com/denoland/deno/releases
 */

import fs from 'fs/promises'
import path from 'path'

const DENO_VERSION = 'v1.45.0'
const BRIVEN_FUNCTIONS_JS_VERSION = '2.4.4'

const DENO_TYPES_URL = `https://github.com/denoland/deno/releases/download/${DENO_VERSION}/lib.deno.d.ts`
const BRIVEN_FUNCTIONS_JS_TYPES_URL = `https://jsr.io/@supabase/functions-js/${BRIVEN_FUNCTIONS_JS_VERSION}/src/edge-runtime.d.ts`

const OUTPUT_FILE = path.join(path.dirname(__dirname), 'public', 'deno', 'lib.deno.d.ts')
const BRIVEN_FUNCTIONS_JS_OUTPUT_FILE = path.join(
  path.dirname(__dirname),
  'public',
  'deno',
  'edge-runtime.d.ts'
)
const OUTPUT_VERSION_FILE = path.join(path.dirname(__dirname), 'public', 'deno', 'deno-version.txt')
const BRIVEN_FUNCTIONS_JS_OUTPUT_VERSION_FILE = path.join(
  path.dirname(__dirname),
  'public',
  'deno',
  'briven-functions-js-version.txt'
)

async function downloadTypes() {
  console.log('Downloading Deno types')

  try {
    const response = await fetch(DENO_TYPES_URL)
    const data = await response.text()

    await fs.writeFile(OUTPUT_FILE, data)
    await fs.writeFile(OUTPUT_VERSION_FILE, DENO_VERSION)

    console.log('Deno types downloaded successfully')
  } catch (error) {
    console.error('Error downloading Deno types', error)
    process.exit(1)
  }
}

async function downloadBrivenFunctionsJsTypes() {
  console.log('Downloading Briven Functions JS types')

  try {
    const response = await fetch(BRIVEN_FUNCTIONS_JS_TYPES_URL)
    const data = await response.text()

    await fs.writeFile(BRIVEN_FUNCTIONS_JS_OUTPUT_FILE, data)
    await fs.writeFile(BRIVEN_FUNCTIONS_JS_OUTPUT_VERSION_FILE, BRIVEN_FUNCTIONS_JS_VERSION)

    console.log('Briven Functions JS types downloaded successfully')
  } catch (error) {
    console.error('Error downloading Briven Functions JS types', error)
    process.exit(1)
  }
}

Promise.all([downloadTypes(), downloadBrivenFunctionsJsTypes()])
