import leven from 'leven'
import chalk from 'chalk'

const commonMistakes = {get: 'list'}
const levenThreshold = 3
const coreCommands = ['build', 'check', 'config', 'dataset', 'start', 'install', 'uninstall']

const helpText = `
Run the command again within a Sanity project directory, where "@sanity/core"
is installed as a dependency.`

export default (cmdName, parentGroupName, groups) => {
  if (parentGroupName && groups[parentGroupName]) {
    return suggestCommand(cmdName, groups[parentGroupName], parentGroupName)
  }

  const isCoreCommand = coreCommands.indexOf(cmdName) >= 0
  if (isCoreCommand) {
    return `"${cmdName}" is not available outside of a Sanity project context.${helpText}`
  }

  return suggestCommand(cmdName, groups.default)
}

function suggestCommand(cmdName, group, parentGroupName = null) {
  // Try to find something similar
  const closest = group
    .map(command => leven(command.name, cmdName))
    .reduce((current, distance, index) => {
      return distance < current.distance
        ? {index, distance}
        : current
    }, {index: null, distance: +Infinity})

  // Given we are within our target threshold, suggest the command
  let suggestCmd = ''
  if (closest.distance <= levenThreshold) {
    const cmdCandidate = group[closest.index]
    suggestCmd = cmdCandidate.name
  }

  // Is this a common mistake that we can suggest an alias for?
  if (!suggestCmd && commonMistakes[cmdName]) {
    suggestCmd = commonMistakes[cmdName]
  }

  const input = chalk.cyan(`"${cmdName}"`)
  const suggest = chalk.green(`"${suggestCmd}"`)
  const help = chalk.cyan('"sanity --help"')

  const didYouMean = suggestCmd ? `Did you mean ${suggest}? ` : ' '
  return parentGroupName
    ? `${input} is not a subcommand of "sanity ${parentGroupName}". ${didYouMean}See ${help}`
    : `${input} is not a sanity command. ${didYouMean}See ${help}`
}

@@ -134,16 +134,19 @@
  = %5B
+%0A  
 'build',
  'ch
@@ -141,16 +141,18 @@
 'build',
+%0A 
  'check'
@@ -156,16 +156,18 @@
 ck',
+%0A 
  'config
 ', '
@@ -162,18 +162,25 @@
  'config
-',
+check',%0A 
  'datase
@@ -186,28 +186,68 @@
 et',
- 'start', 'install',
+%0A  'deploy',%0A  'documents',%0A  'exec',%0A  'hook',%0A  'start',%0A 
  'un
@@ -254,16 +254,17 @@
 install'
+%0A
 %5D%0A%0Aconst

Add more commands to list of core cmds