-ReactiveRunner = require('reactive-runner')
-TextLoader = require('text-loader')
-TextParser = require('text-parser')
+Freesheet = require 'freesheet'
 CoreFunctions = require('core-functions')
 PageFunctions = require('page-functions')
 TimeFunctions = require('time-functions')

    if @loading then existingRow or @_newRow(name) else existingRow

  _newRow: (name) ->
    def = (d for d in @loader.functionDefinitions() when d.name == name)[0]
    row = {name: def?.name, formula: def?.expr.text, value: null}
    @data.push row
    row

      console.log 'beforeRemoveRow', index, numberOfRows, self.data
      for row in self.data[index...index + numberOfRows]
        console.log 'Removing row', row
        self.sheet.remove row.name

  updateFormula: (name, formula, oldName, nextName) ->
    console.log 'updateFormula', name, formula, oldName, nextName
    if name and formula then @sheet.update name, formula, oldName, nextName
    if name and not formula then @sheet.remove name
    if not name and oldName then @sheet.remove oldName


  asText: -> @sheet.text()

  loadText: (text) ->
    @loading = true
    @sheet.clear()
    @data.length = 0
    @sheet.load text
    @_newRow null
    @loading = false

