 # Time-tracking keeps track of the amount of time spent in each subtree.
 # Clones are double-counted. This is a known bug and will not be fixed.
 (() ->
-
-  pad = (val, length, padChar = '0') ->
-    val += ''
-    numPads = length - val.length
-    if (numPads > 0) then new Array(numPads + 1).join(padChar) + val else val
-
-  sum = (a) ->
-    total = 0
-    for x in a
-      total += x
-    total
 
   Plugins.register {
     name: "Time Tracking"

      # Could avoid lookups by knowing exact changes, if needed
      @_rebuildTreeTimes event.ancestorId

    _combineDailyTimes: (dailyTimes...) ->
      combined = {}
      for date in  _.union.apply _, (_.map dailyTimes, _.keys)
        combined[date] = 0
        for dailyTime in dailyTimes
          combined[date] += dailyTime[date] ? 0
      combined

    _addTimeToRow: (row, time, day) ->
      @transformRowData row.id, "rowTotalTime", (current) ->
        (current ? 0) + time
      @transformRowData row.id, "rowDailyTime", (current) =>
        key = @_date day
        current ?= {}
        current[key] = (current[key] ? 0) + time
        current

    _rebuildRowTimes: (row) -> # Unused, but keep for data migration in future versions
      totalTime = 0
      dailyTime = {}
      for period in @getRowData row, "timePeriods" ? []
        totalTime += period.time
        key = @_date day
        dailyTime[key] = (dailyTime[key] ? 0) + period.time
      @setRowData row.id, "rowTotalTime", totalTime
      @setRowData row.id, "rowDailyTime", dailyTime

    _addTimeToAncestors: (row, time, day) ->
      for ancestorId in @api.view.data.allAncestors row.id, { inclusive: true }
        @transformRowData ancestorId, "treeTotalTime", (current) ->
          (current ? 0) + time
        @transformRowData ancestorId, "treeDailyTime", (current) =>
          key = @_date day
          current ?= {}
          current[key] = (current[key] ? 0) + time
          current

    _rebuildTreeTimes: (id) ->
      children = @api.view.data._getChildren id


    onRowPeriod: (period) ->
      period.time ?= period.stop - period.start
      @_addTimeToRow period.row, period.time, period.stop
      @_addTimeToAncestors period.row, period.time, period.stop

