import titleCase from 'title-case'
import primerColors from 'primer-colors'

import colorSystemSCSS from '!!raw-loader?module!../src/support/variables/color-system.scss'
import colorVariablesSCSS from '!!raw-loader?module!../src/support/variables/colors.scss'

const variables = {}

parseSCSSVariables(colorSystemSCSS, variables)
parseSCSSVariables(colorVariablesSCSS, variables)

// XXX we don't necessarily define them in this order in primer-colors,
// so we define an array here just to be safe
const gradientHues = ['gray', 'blue', 'green', 'purple', 'yellow', 'orange', 'red', 'pink']

const colors = {
  ...primerColors,
  pink: Object.keys(variables)
    .filter(key => key.startsWith('pink-'))
    .sort()
    .map(key => variables[key])
}

const aliases = {}

const palettes = gradientHues.map(name => {
  const bgClass = `bg-${name}`
  const textClass = `text-${name}`
  return {
    name,
    title: titleCase(name),
    value: colors[name][5],
    bg: variables[bgClass] ? {className: bgClass, value: variables[bgClass]} : null,
    fg: variables[textClass] ? {className: textClass, value: variables[textClass]} : null,
    values: colors[name].map((value, index) => ({
      value,
      index,
      variable: `${name}-${index}00`,
      slug: `${name}-${index}`,
      aliases: (aliases[value] = {})
    }))
  }
})

for (const key of Object.keys(variables)) {
  const match = key.match(/^(bg|text|border)-(\w+)(-(dark|light))?$/)
  const value = variables[key]
  if (match && aliases[value]) {
    // eslint-disable-next-line no-unused-vars
    const [_, type, name, suffix] = match
    aliases[value][type] = key
  }
}

export {colors, gradientHues, palettes, getPaletteByName, variables}

function getPaletteByName(name) {
  return palettes.find(palette => palette.name === name)
}

function parseSCSSVariables(scssString, variables = {}) {
  const variablePattern = /\$([-\w]+):\s*(.+)( !default);/g
  let match
  do {
    match = variablePattern.exec(scssString)
    if (match) {
      // eslint-disable-next-line no-unused-vars
      const [_, name, value] = match
      variables[name] = value.startsWith('$') ? variables[value.substr(1)] : value
    }
  } while (match)
  return variables
}

import titleCase from 'title-case'
import primerColors from 'primer-colors'

import colorSystemSCSS from '!!raw-loader?module!../src/support/variables/color-system.scss'
import colorVariablesSCSS from '!!raw-loader?module!../src/support/variables/colors.scss'

const variables = {}

parseSCSSVariables(colorSystemSCSS, variables)
parseSCSSVariables(colorVariablesSCSS, variables)

// XXX we don't necessarily define them in this order in primer-colors,
// so we define an array here just to be safe
const gradientHues = ['gray', 'blue', 'green', 'purple', 'yellow', 'orange', 'red', 'pink']

const colors = {
  ...primerColors,
  pink: Object.keys(variables)
    .filter(key => key.startsWith('pink-'))
    .sort()
    .map(key => variables[key])
}

const aliases = {}

const palettes = gradientHues.map(name => {
  return {
    name,
    title: titleCase(name),
    value: variables[name] || colors[name][5],
    values: colors[name].map((value, index) => ({
      value,
      index,
      variable: `${name}-${index}00`,
      slug: `${name}-${index}`,
      aliases: (aliases[value] = {})
    }))
  }
})

for (const key of Object.keys(variables)) {
  const match = key.match(/^(bg|text|border)-(\w+)(-(dark|light))?$/)
  const value = variables[key]
  if (match && aliases[value]) {
    // eslint-disable-next-line no-unused-vars
    const [_, type, name, suffix] = match
    aliases[value][type] = key
  }
}

export {colors, gradientHues, palettes, getPaletteByName, variables}

export const borders = Object.keys(variables)
  .filter(key => key.startsWith('border-') && !variables[key].includes('$'))
  .sort()
  .map(key => ({
    variable: key,
    value: variables[key],
    slug: key,
    aliases: {border: key}
  }))

function getPaletteByName(name) {
  return palettes.find(palette => palette.name === name)
}

function parseSCSSVariables(scssString, variables = {}) {
  const variablePattern = /\$([-\w]+):\s*(.+)( !default);/g
  let match
  do {
    match = variablePattern.exec(scssString)
    if (match) {
      // eslint-disable-next-line no-unused-vars
      const [_, name, value] = match
      variables[name] = value.startsWith('$') ? variables[value.substr(1)] : value
    }
  } while (match)
  return variables
}

add borders export in docs/color-variables