 'use strict';
-{defineModule, log, min, max} = require 'art-standard-lib'
+{clone, defineModule, log, min, max} = require 'art-standard-lib'
 {BaseClass} = require 'art-class-system'
-{rect} = require 'art-atomic'
+{rect, Rectangle} = require 'art-atomic'
+{compositeModeMap} = require 'art-canvas'
 
 defineModule module, ->
 

    closeClipping: (oldClipArea) ->
      @clippingArea = oldClipArea

    drawDrawable: (child, elementToTargetMatrix) ->

      targetSpaceChildDrawArea = elementToTargetMatrix.transformBoundingRect child.elementSpaceDrawArea
      # {left, top} = @padding
      # if left != 0 || top != 0
      #   targetSpaceChildDrawArea = @padding.translate targetSpaceChildDrawArea

      switch child.compositeMode
        when "alphaMask"
          # technically this is more accurate:
          #   elementSpaceDrawArea.intersection targetSpaceChildDrawArea
          # However, usually if there is a mask, it is "full", which makes "intersection" a no-op.
          # Further, we'd rather this value be more "stable" so changes in drawAreas don't
          # propgate any higher than they need to.
          # This way, if only children below a mask change, there is no need to propogate up.
          @totalDrawArea = targetSpaceChildDrawArea.intersectInto @totalDrawArea

        when "sourceIn", "targetAlphaMask", "inverseAlphaMask"
          null # doesn't change drawArea

        when "normal", "add", "replace", "destOver"
          @addDrawArea targetSpaceChildDrawArea

        else throw new Error "unknown compositeMode:#{child.compositeMode}"

      @reset()

    reset: ->
      @targetDrawArea = null
      @clippingArea = null

    @getter
      drawArea: -> @targetDrawArea || new Rectangle
      inspectedObjects: -> DrawAreaCollector: clone @targetDrawArea

    clipDrawArea: (drawArea) ->
      if @clippingArea
        drawAreaBefore = drawArea
        drawAreaBeforeString = drawArea.toString()
        drawArea.intersection @clippingArea
      else
        drawArea

    # returns an existing-value
    openClipping: (area, drawMatrix, pathSize, areaArg2) ->
