import titleCase from 'title-case'
import primerColors from 'primer-colors'
import colorSystemSCSS from '!!raw-loader?modules!../src/support/variables/color-system.scss'
import colorVariablesSCSS from '!!raw-loader?modules!../src/support/variables/colors.scss'

export 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
export const gradientHues = ['gray', 'blue', 'green', 'purple', 'yellow', 'orange', 'red', 'pink']

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

export const gradientPalettes = gradientHues.map(name => ({
  name,
  title: titleCase(name),
  values: colors[name].map((value, index) => ({
    value,
    index,
    variable: `${name}-${index}00`,
    slug: `${name}-${index}`
  }))
}))

export const backgroundPalettes = gradientPalettes.map(({name, values, ...paletteRest}) => ({
  name,
  ...paletteRest,
  value: variables[`bg-${name}`] || '',
  values: values.map(value => ({...value, className: `bg-${value.slug}`}))
}))

export const foregroundPalettes = gradientPalettes.map(({name, values, ...paletteRest}) => ({
  name,
  ...paletteRest,
  value: variables[`text-${name}`] || '',
  values: values.map(value => ({...value, className: `color-${value.slug}`}))
}))

export function getBackgroundPalette(name) {
  return backgroundPalettes.find(palette => palette.name === name)
}

export function getForegroundPalette(name) {
  return foregroundPalettes.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
}

@@ -69,16 +69,46 @@
 colors'%0A
+%0Aexport const variables = %7B%7D%0A%0A
 import c
@@ -139,33 +139,32 @@
 aw-loader?module
-s
 !../src/support/
@@ -243,17 +243,16 @@
 r?module
-s
 !../src/
@@ -287,37 +287,8 @@
 s'%0A%0A
-export const variables = %7B%7D%0A%0A
 pars
@@ -623,16 +623,35 @@
 ors = %7B%0A
+  ...primerColors,%0A
   pink: 
@@ -764,27 +764,8 @@
 ey%5D)
-,%0A  ...primerColors
 %0A%7D%0A%0A
@@ -821,27 +821,105 @@
 name =%3E 
-(
 %7B%0A
+  const bgClass = %60bg-$%7Bname%7D%60%0A  const textClass = %60text-$%7Bname%7D%60%0A  return %7B%0A  
   name,%0A
   title:
@@ -910,16 +910,18 @@
   name,%0A
+  
   title:
@@ -938,16 +938,194 @@
 (name),%0A
+    bg: variables%5BbgClass%5D ? %7BclassName: bgClass, value: variables%5BbgClass%5D%7D : null,%0A    fg: variables%5BtextClass%5D ? %7BclassName: textClass, value: variables%5BtextClass%5D%7D : null,%0A  
   values
@@ -1164,24 +1164,26 @@
 ) =%3E (%7B%0A    
+  
 value,%0A    i
@@ -1185,15 +1185,19 @@
     
+ 
+ 
 index,%0A
+  
     
@@ -1228,16 +1228,18 @@
 ex%7D00%60,%0A
+  
     slug
@@ -1263,16 +1263,21 @@
 x%7D%60%0A
+  
   %7D))%0A
+  %7D%0A
 %7D
-)
 )%0A%0Ae
@@ -1317,32 +1317,68 @@
 gradientPalettes
+%0A  .filter(palette =%3E palette.bg)%0A  
 .map((%7Bname, val
@@ -1373,32 +1373,36 @@
 ((%7Bname, values,
+ bg,
  ...paletteRest%7D
@@ -1403,38 +1403,42 @@
 eRest%7D) =%3E (%7B%0A  
+ 
+ 
 name,%0A
+  
   ...paletteRest
@@ -1445,46 +1445,58 @@
 ,%0A  
-value: variables%5B%60bg-$%7Bname%7D%60%5D %7C%7C '',%0A
+  className: bg.className,%0A    value: bg.value,%0A  
   va
@@ -1558,32 +1558,34 @@
 value.slug%7D%60%7D))%0A
+  
 %7D))%0A%0Aexport cons
@@ -1623,16 +1623,52 @@
 Palettes
+%0A  .filter(palette =%3E palette.fg)%0A  
 .map((%7Bn
@@ -1679,16 +1679,20 @@
  values,
+ fg,
  ...pale
@@ -1707,16 +1707,18 @@
 ) =%3E (%7B%0A
+  
   name,%0A
@@ -1719,16 +1719,18 @@
 name,%0A  
+ 
+ 
 ...palet
@@ -1743,48 +1743,58 @@
 ,%0A  
-value: variables%5B%60text-$%7Bname%7D%60%5D %7C%7C '',%0A
+  className: fg.className,%0A    value: fg.value,%0A  
   va
@@ -1867,16 +1867,18 @@
 ug%7D%60%7D))%0A
+  
 %7D))%0A%0Aexp

fix raw-loader SCSS imports, rework palette objects