const chroma = require('chroma-js')
const toKebabCase = require('lodash/kebabCase')
const PALETTE = require('../dist/colors.meta.json')

const colors = PALETTE.colors.map(colorArray => {
  return orderByVariableEntry(colorArray).map(formatVariableEntry)
})

printStylesheet(colors)

function orderByVariableEntry(colorArray) {
  const source = colorArray.filter(colorObject => !colorObject._meta.alias)
  const aliases = colorArray.filter(colorObject => colorObject._meta.alias)

  return source.concat(aliases)
}

function formatVariableEntry(colorObject) {
  return `${formatVariableName(colorObject)}: ${formatVariableValue(colorObject)};`
}

function formatVariableName(colorObject) {
  return `$studio-${toKebabCase(colorObject.name.toLowerCase())}-rgb`
}

function formatVariableValue(colorObject) {
  if (!colorObject._meta.alias) {
    return formatRGB(colorObject.value)
  }

  const name = `${colorObject._meta.baseName} ${colorObject._meta.index}`
  return formatVariableName({ name })
}

function formatRGB(colorValue) {
  const chromaObject = chroma(colorValue)
  return ['r', 'g', 'b'].map(c => chromaObject.get(`rgb.${c}`)).join(', ')
}

function printStylesheet(colorArrays) {
  const blocks = colorArrays.map(colorArray => {
    return colorArray.join('\n')
  })

  blocks.unshift([`// Generated by Color Studio v${PALETTE.version}`])
  console.log(blocks.join('\n\n'))
}
