import { Color } from './colors.js'

function getColorForString(value: string) {
  const c = ['neutral', 'green', 'blue', 'orange', 'red'] as const
  let hash = 0
  for (let i = 0; i < value.length; i++) {
    hash = value.charCodeAt(i) + hash * 31
  }
  hash = Math.abs(hash)

  return c[hash % c.length]
}

function getChartColorForString(value: string, mono: boolean) {
  let hash = 0
  for (let i = 0; i < value.length; i++) {
    hash = value.charCodeAt(i) + hash * 31
  }
  hash = Math.abs(hash)

  return (
    mono ? `chartMono${(hash % 6) + 1}` : `chart${(hash % 11) + 1}`
  ) as Color
}

const expectNever = (value: never): never => {
  throw new TypeError('Unexpected value: ' + value)
}

export { getColorForString, getChartColorForString, expectNever }
