// Use commonJS module format as this may be consumed by NodeJS script
const categoryQuery = require('../queries/categoryQuery')
const eventQuery = require('../queries/eventQuery')
const pageQuery = require('../queries/pageQuery')
const venueQuery = require('../queries/venueQuery')

const getEdges = ({ 
  data: { 
    allMarkdownRemark: { 
      edges 
    } 
  } 
}) => edges

const parseCategory = category => {
  const { node: { frontmatter: { slug, summary, title }, html } } = category
  return {
    html,
    slug,
    summary,
    title,
  }
}

const parseEvent = event => {
  const { 
    node: { 
      frontmatter: { 
        category,
        city,
        date, 
        registration,
        slug,
        summary,
        time, 
        title,
        venue 
      }, 
      html
    } 
  } = event
  return {
    category,
    city,
    html,
    date,
    registration,
    slug,
    summary,
    time,
    title,
    venue
  }
}

const parsePage = page => {
  const {
    node: {
      frontmatter: {
        slug,
        title,
      },
      html,
    }
  } = page
  return {
    html,
    slug,
    title,
  }
}

const parseVenue = venue => {
  const {
    node: {
      frontmatter: {
        address,
        city,
        postalCode,
        province,
        slug,
        title
      }
    }
  } = venue
  return {
    address,
    city,
    postalCode,
    province,
    slug,
    title
  }
}

const fetchAllEvents = (graphql) => new Promise(resolve => {
    Promise.all([
      graphql(categoryQuery),
      graphql(eventQuery),
      graphql(venueQuery),
    ]).then(results => {
      const [rawCategories, rawEvents, rawVenues] = results.map(result => getEdges(result))

      const categories = rawCategories.map(c => parseCategory(c))
      const venues = rawVenues.map(v => parseVenue(v))
      const events = rawEvents.map(e => {
        const event = parseEvent(e)
        const cMatches = categories.filter(({ slug }) => event.category === slug)
        const category = cMatches.length ? cMatches[0] : undefined
        const vMatches = venues.filter(({ slug }) => event.venue === slug)
        const venue = vMatches.length ? vMatches[0] : undefined
        return {
          ...event,
          summary: event.summary || category.summary,
          category,
          venue,
        }
      })

      resolve(events)
    })
  })

const fetchAllPages = (graphql) => new Promise(resolve => {
  graphql(pageQuery).then(results => {
    const pages = getEdges(results).map(p => parsePage(p))
    resolve(pages)
  })
})

module.exports = {
  fetchAllEvents,
  fetchAllPages,
  getEdges,
}

@@ -296,17 +296,16 @@
 ges = (%7B
- 
 %0A  data:
@@ -306,17 +306,16 @@
  data: %7B
- 
 %0A    all
@@ -331,17 +331,16 @@
 emark: %7B
- 
 %0A      e
@@ -347,21 +347,18 @@
 dges
- 
 %0A    %7D
- 
 %0A  %7D
- 
 %0A%7D) 
@@ -581,17 +581,16 @@
  const %7B
- 
 %0A    nod
@@ -593,17 +593,16 @@
  node: %7B
- 
 %0A      f
@@ -614,17 +614,16 @@
 atter: %7B
- 
 %0A       
@@ -660,17 +660,16 @@
    date,
- 
 %0A       
@@ -727,17 +727,16 @@
    time,
- 
 %0A       
@@ -756,17 +756,16 @@
    venue
- 
 %0A      %7D
@@ -765,17 +765,16 @@
       %7D,
- 
 %0A      h
@@ -782,17 +782,16 @@
 ml%0A    %7D
- 
 %0A  %7D = e
@@ -1442,37 +1442,8 @@
 ) =%3E
- new Promise(resolve =%3E %7B%0A   
  Pro
@@ -1453,26 +1453,24 @@
 e.all(%5B%0A    
-  
 graphql(cate
@@ -1473,34 +1473,32 @@
 categoryQuery),%0A
-  
     graphql(even
@@ -1510,18 +1510,16 @@
 y),%0A    
-  
 graphql(
@@ -1537,12 +1537,13 @@
 ,%0A  
- 
- 
 %5D)
+%0A  
 .the
@@ -1557,20 +1557,16 @@
 ts =%3E %7B%0A
-    
   const 
@@ -1646,20 +1646,16 @@
 sult))%0A%0A
-    
   const 
@@ -1708,20 +1708,16 @@
 ory(c))%0A
-    
   const 
@@ -1765,26 +1765,14 @@
 )%0A  
-    const events =
+return
  raw
@@ -1789,20 +1789,16 @@
 (e =%3E %7B%0A
-    
     cons
@@ -1821,28 +1821,24 @@
 vent(e)%0A    
-   
- 
 const cMatch
@@ -1891,36 +1891,32 @@
 egory === slug)%0A
-    
     const catego
@@ -1962,28 +1962,24 @@
 defined%0A    
-  
-  
 const vMatch
@@ -2033,20 +2033,16 @@
 = slug)%0A
-    
     cons
@@ -2097,20 +2097,16 @@
 ned%0A    
- 
-   
 return %7B
@@ -2106,20 +2106,16 @@
 eturn %7B%0A
-    
       ..
@@ -2124,28 +2124,24 @@
 vent,%0A      
-    
 summary: eve
@@ -2174,28 +2174,24 @@
 mary,%0A      
-    
 category,%0A  
@@ -2190,28 +2190,24 @@
 gory,%0A      
- 
-   
 venue,%0A     
@@ -2209,55 +2209,15 @@
     
-  
-  
 %7D%0A  
-    %7D)%0A%0A      resolve(events)%0A    %7D)%0A  
+%7D)%0A
 %7D)%0A%0A
@@ -2254,35 +2254,8 @@
 ) =%3E
- new Promise(resolve =%3E %7B%0A 
  gra
@@ -2269,22 +2269,26 @@
 geQuery)
+%0A
 .then(
+%0A  
 results 
@@ -2293,28 +2293,8 @@
 s =%3E
- %7B%0A    const pages =
  get
@@ -2335,33 +2335,8 @@
 p))%0A
-    resolve(pages)%0A  %7D)%0A%7D
 )%0A%0Am

Simplify Promises in util/data and fix warnings. - Remove extra layer of Promises in fetchAllPages and fetchAllEvents - Fix the Bluebird warnings about values not being returned