import { calculateColumnLayout } from './calculate-column-layout'
import { compose } from 'underscore'
import { createBody } from './body'
import { createEnsureColumns } from './ensure-columns'
import { createLayOutBodyAndOverlays } from './lay-out-body-and-overlays'
import { createMarkRowIndices } from './mark-row-indices'
import { createMeasureGridArea }  from './measure-grid-area'
import { createProcessRowData } from './process-row-data'
import { createProcessSizeAndClipping } from './process-size-and-clipping'
import { createScrollers } from './scrollers'
import { createSetupGridTemplate } from './setup-grid-template'
import { ensureData } from './ensure-data'
import { ensureId } from './ensure-id'
import { rebind, call, each } from '@zambezi/d3-utils'

import './grid.css'

export function createGrid() {

  const setupTemplate = createSetupGridTemplate()
      , ensureColumns = createEnsureColumns()
      , processRowData = createProcessRowData()
      , processSizeAndClipping = createProcessSizeAndClipping()
      , body = createBody()
      , grid = compose(
          each(console.log.bind(console, 'grid drawn'))
        , call(createScrollers())
        , call(body)
        , each(createLayOutBodyAndOverlays())
        , call(processSizeAndClipping)
        , call(createMeasureGridArea())
        , call(createMarkRowIndices())
        , call(processRowData)
        , call(setupTemplate)
        , each(calculateColumnLayout)
        , call(ensureColumns)
        , each(ensureData)
        , each(ensureId)
        )
      , api = rebind()
            .from(setupTemplate, 'template')
            .from(ensureColumns, 'columns')
            .from(processRowData, 'filters', 'filtersUse', 'skipRowLocking')
            .from(processSizeAndClipping, 'scroll')

  return api(grid)
}

@@ -75,16 +75,22 @@
  compose
+, wrap
  %7D from 
@@ -102,16 +102,16 @@
 rscore'%0A
-
 import %7B
@@ -738,16 +738,34 @@
 ll, each
+, redraw, throttle
  %7D from 
@@ -1117,16 +1117,22 @@
 ach(
+() =%3E 
 console.
 log.
@@ -1131,37 +1131,22 @@
 ole.
-log.bind(console, 'grid 
+groupEnd('
 draw
-n
 '))%0A
@@ -1545,16 +1545,60 @@
 sureId)%0A
+        , each(() =%3E console.group('draw'))%0A
         
@@ -1858,12 +1858,30 @@
 api(
+redraw(throttle(
 grid)
+))
 %0A%7D%0A

implement throttle and redraw.