import { window } from 'vscode'
import { DefinitionSources } from './definitionSources'

export async function removeWorktree(panel: Panel, relPath: string, issueNumber: number): Promise<void> {
  const state = await panel.readIssueState(issueNumber)
  const branch = typeof state.branch === 'string' ? state.branch : ''
  const choice = await window.showWarningMessage(
    branch ? `确认删除 worktree ${relPath} 及其分支 ${branch}?` : `确认删除 worktree ${relPath}?`,
    { modal: true },
    '删除',
  )
  if (choice !== '删除')
    return
}

class Preview {
  constructor(document: TextDocument, owner: Owner) {
    this.definitions = new DefinitionSources({
      rule: async reference => ruleDefinitions(await owner.resolveReference(reference)),
      language: offset => languageDefinitions(document.uri, document.positionAt(offset)),
    })
  }
}

export function commentsUrl(opts: Options): URL {
  const url = new URL(
    `${baseUrl(opts.host)}/repos/${opts.owner}/${opts.repo}/issues/${opts.index}/comments`,
  )
  return url
}

export function merged(base: string[], extra: string[]): string[] {
  return concatAll(
    base,
    ...extra,
  )
}
