import { mkdir, writeFile } from 'node:fs/promises'
import { join } from 'node:path'

const root = new URL('../_ref/', import.meta.url)
await mkdir(root, { recursive: true })

const files = [
  ['tasks-Tasks.vue', 'https://raw.githubusercontent.com/engigu/taskpool/main/web/src/views/tasks/Tasks.vue'],
  ['tasks-TaskDialog.vue', 'https://raw.githubusercontent.com/engigu/taskpool/main/web/src/views/tasks/TaskDialog.vue'],
  ['tasks-RepoDialog.vue', 'https://raw.githubusercontent.com/engigu/taskpool/main/web/src/views/tasks/RepoDialog.vue'],
  ['tasks-TaskAdvancedConfig.vue', 'https://raw.githubusercontent.com/engigu/taskpool/main/web/src/views/tasks/components/TaskAdvancedConfig.vue'],
  ['tasks-TaskCronConfig.vue', 'https://raw.githubusercontent.com/engigu/taskpool/main/web/src/views/tasks/components/TaskCronConfig.vue'],
  ['tasks-TaskLangConfig.vue', 'https://raw.githubusercontent.com/engigu/taskpool/main/web/src/views/tasks/components/TaskLangConfig.vue'],
  ['tasks-TaskNotificationConfig.vue', 'https://raw.githubusercontent.com/engigu/taskpool/main/web/src/views/tasks/components/TaskNotificationConfig.vue'],
  ['history-History.vue', 'https://raw.githubusercontent.com/engigu/taskpool/main/web/src/views/history/History.vue'],
  ['agents-Agents.vue', 'https://raw.githubusercontent.com/engigu/taskpool/main/web/src/views/agents/Agents.vue'],
  ['editor-Editor.vue', 'https://raw.githubusercontent.com/engigu/taskpool/main/web/src/views/editor/Editor.vue'],
  ['env-Environments.vue', 'https://raw.githubusercontent.com/engigu/taskpool/main/web/src/views/environments/Environments.vue'],
  ['notify-Notify.vue', 'https://raw.githubusercontent.com/engigu/taskpool/main/web/src/views/notify/Notify.vue'],
  ['settings-Settings.vue', 'https://raw.githubusercontent.com/engigu/taskpool/main/web/src/views/settings/Settings.vue'],
  ['dashboard-Dashboard.vue', 'https://raw.githubusercontent.com/engigu/taskpool/main/web/src/views/dashboard/Dashboard.vue'],
  ['monitor-Monitor.vue', 'https://raw.githubusercontent.com/engigu/taskpool/main/web/src/views/monitor/Monitor.vue'],
  ['languages-Languages.vue', 'https://raw.githubusercontent.com/engigu/taskpool/main/web/src/views/languages/Languages.vue'],
  ['deps-Dependencies.vue', 'https://raw.githubusercontent.com/engigu/taskpool/main/web/src/views/dependencies/Dependencies.vue'],
  ['interconnect-index.vue', 'https://raw.githubusercontent.com/engigu/taskpool/main/web/src/views/interconnect/index.vue'],
  ['logs-MessageLogs.vue', 'https://raw.githubusercontent.com/engigu/taskpool/main/web/src/views/logs/MessageLogs.vue'],
  ['terminal-Terminal.vue', 'https://raw.githubusercontent.com/engigu/taskpool/main/web/src/views/terminal/Terminal.vue'],
  ['MainLayout.vue', 'https://raw.githubusercontent.com/engigu/taskpool/main/web/src/layouts/MainLayout.vue'],
  ['LogDetailCard.vue', 'https://raw.githubusercontent.com/engigu/taskpool/main/web/src/components/LogDetailCard.vue'],
  ['NodeSwitcher.vue', 'https://raw.githubusercontent.com/engigu/taskpool/main/web/src/components/NodeSwitcher.vue'],
  ['api-index.ts', 'https://raw.githubusercontent.com/engigu/taskpool/main/web/src/api/index.ts'],
]

for (const [name, url] of files) {
  try {
    const res = await fetch(url)
    if (!res.ok) throw new Error(`${res.status} ${res.statusText}`)
    const text = await res.text()
    await writeFile(new URL(name, root), text, 'utf8')
    console.log('OK', name, text.length)
  } catch (e) {
    console.error('FAIL', name, e.message)
  }
}
