#!/usr/bin/env node
// ┌───────────────┐
// │ Deploy Script │
// └───────────────┘
// Builds a json file with all site content

var fs = require('fs')
var jf = require('jsonfile')
var path = require('path')
var yaml = require('js-yaml')
var mark = require('marked')
var now = Date.now()
var version = require('../package.json').version
var response = {
  elements: [],
  colors: []
}

function constructItem(content, meta) {
  var item = {
    _id: content.id,
    name: content.title,
    slug: content.link,
    tags: ['calcite-web', 'web', content.title, meta.group, meta.page, meta.page_slug],
    sort_order: meta.order,
    LastModified: now
  }

  var markdownPath = path.join('docs', 'source', meta.page_slug, '_' + item.slug + '.md')
  var markdown = fs.readFileSync(markdownPath, 'utf8')

  item.description = JSON.stringify(mark(markdown))

  if (content.modifiers && typeof content.modifiers !== 'boolean') {
    item.modifiers = content.modifiers
    var samplePath = path.join('docs', 'source', meta.page_slug, 'sample-code', '_' + item.slug + '.html')
    var sample = fs.readFileSync(samplePath, 'utf8')
    item.sample_code = JSON.stringify(sample)
  }

  if (meta.group == 'Palette') {
    response.colors.push(item)
  } else {
    response.elements.push(item)
  }
}

var contents = yaml.safeLoad(fs.readFileSync('docs/source/table_of_contents.yml', 'utf8'))
var counter = 0

for (var key in contents) {
  var meta = {
    page: contents[key].title,
    page_slug: contents[key].base,
    page_order: counter
  }
  contents[key].navigation.forEach(function (group, index){
    meta.group = group.group
    meta.group_order = index

    var orderArray = []

    for (var i = group.pages.length; i > 0; i--) {
      orderArray.push(i)
    }

    group.pages.forEach(function (element, i){
      meta.order = orderArray[i]
      if (element.title == 'Overview') {
        meta.order == 100
      }
      constructItem(element, meta)
    })
  })
  counter++
}

jf.writeFileSync('dist/latest.json', response)
jf.writeFileSync(path.join('dist/', 'v' + version + '.json'), response)

process.exit(0)

@@ -1378,45 +1378,127 @@
 var 
+pages = Object.keys(
 co
-u
 nte
-r = 0%0A%0Afor (var key in contents
+nts).map(function (page) %7B%0A  return contents%5Bpage%5D%0A%7D)%0A%0Apages.forEach(function (page, pageIndex
 ) %7B%0A
@@ -1522,29 +1522,20 @@
   page: 
-contents%5Bkey%5D
+page
 .title,%0A
@@ -1553,71 +1553,28 @@
 ug: 
-contents%5Bkey%5D
+page
 .base
-,
 %0A  
+%7D%0A
   page
-_order: counter%0A  %7D%0A  contents%5Bkey%5D
 .nav
@@ -1606,17 +1606,22 @@
 (group, 
-i
+groupI
 ndex)%7B%0A 
@@ -1675,126 +1675,42 @@
 r = 
-index%0A%0A    var orderArray = %5B%5D%0A%0A    for (var i = group.pages.length; i %3E 0; i--) %7B%0A      orderArray.push(i)%0A    %7D%0A%0A   
+groupIndex * 100%0A    group.pages =
  gro
@@ -1714,31 +1714,27 @@
 group.pages.
-forEach
+map
 (function (e
@@ -1736,134 +1736,96 @@
 on (
-element, i)%7B%0A      meta.order = orderArray%5Bi%5D%0A      if (element.title == 'Overview') %7B%0A        meta.order == 100%0A      %7D%0A     
+section, sectionIndex) %7B%0A      meta.order = groupIndex * 100 + sectionIndex%0A      return
  con
@@ -1835,23 +1835,23 @@
 uctItem(
-element
+section
 , meta)%0A
@@ -1866,21 +1866,10 @@
  %7D)%0A
-  counter++%0A
 %7D
+)
 %0A%0Ajf

change ordering of JSON sections to be low-to-hi, grouped by 100