// Class: openfl._internal.renderer.dom.DOMMaskManager

var $global = typeof window != "undefined" ? window : typeof global != "undefined" ? global : typeof self != "undefined" ? self : this

$global.Object.defineProperty(exports, "__esModule", {value: true});

var __map_reserved = {};

// Imports

var $hxClasses = require("./../../../../hxClasses_stub").default;
var $import = require("./../../../../import_stub").default;
var $extend = require("./../../../../extend_stub").default;
function openfl__$internal_renderer_AbstractMaskManager() {return require("./../../../../openfl/_internal/renderer/AbstractMaskManager");}
function openfl_geom_Rectangle() {return require("./../../../../openfl/geom/Rectangle");}
function openfl_geom_Matrix() {return require("./../../../../openfl/geom/Matrix");}

// Constructor

var DOMMaskManager = function(renderSession) {
	(openfl__$internal_renderer_AbstractMaskManager().default).call(this,renderSession);
	this.clipRects = [];
	this.numClipRects = 0;
}

// Meta

DOMMaskManager.__name__ = ["openfl","_internal","renderer","dom","DOMMaskManager"];
DOMMaskManager.__super__ = (openfl__$internal_renderer_AbstractMaskManager().default);
DOMMaskManager.prototype = $extend((openfl__$internal_renderer_AbstractMaskManager().default).prototype, {
	pushMask: function(mask) {
		this.pushRect(mask.getBounds(mask),mask.__getRenderTransform());
	},
	pushObject: function(object,handleScrollRect) {
		if(handleScrollRect == null) {
			handleScrollRect = true;
		}
		if(handleScrollRect && object.__scrollRect != null) {
			this.pushRect(object.__scrollRect,object.__renderTransform);
		}
		if(object.__mask != null) {
			this.pushMask(object.__mask);
		}
	},
	pushRect: function(rect,transform) {
		if(this.numClipRects == this.clipRects.length) {
			this.clipRects[this.numClipRects] = new (openfl_geom_Rectangle().default)();
		}
		var clipRect = this.clipRects[this.numClipRects];
		rect.__transform(clipRect,transform);
		if(this.numClipRects > 0) {
			var parentClipRect = this.clipRects[this.numClipRects - 1];
			clipRect.__contract(parentClipRect.x,parentClipRect.y,parentClipRect.width,parentClipRect.height);
		}
		if(clipRect.height < 0) {
			clipRect.height = 0;
		}
		if(clipRect.width < 0) {
			clipRect.width = 0;
		}
		this.currentClipRect = clipRect;
		this.numClipRects++;
	},
	popMask: function() {
		this.popRect();
	},
	popObject: function(object,handleScrollRect) {
		if(handleScrollRect == null) {
			handleScrollRect = true;
		}
		if(object.__mask != null) {
			this.popMask();
		}
		if(handleScrollRect && object.__scrollRect != null) {
			this.popRect();
		}
	},
	popRect: function() {
		if(this.numClipRects > 0) {
			this.numClipRects--;
			if(this.numClipRects > 0) {
				this.currentClipRect = this.clipRects[this.numClipRects - 1];
			} else {
				this.currentClipRect = null;
			}
		}
	},
	updateClip: function(displayObject) {
		if(this.currentClipRect == null) {
			displayObject.__worldClipChanged = displayObject.__worldClip != null;
			displayObject.__worldClip = null;
		} else {
			if(displayObject.__worldClip == null) {
				displayObject.__worldClip = new (openfl_geom_Rectangle().default)();
			}
			var clip = (openfl_geom_Rectangle().default).__pool.get();
			var matrix = (openfl_geom_Matrix().default).__pool.get();
			matrix.copyFrom(displayObject.__renderTransform);
			matrix.invert();
			this.currentClipRect.__transform(clip,matrix);
			if(clip.equals(displayObject.__worldClip)) {
				displayObject.__worldClipChanged = false;
			} else {
				displayObject.__worldClip.copyFrom(clip);
				displayObject.__worldClipChanged = true;
			}
			(openfl_geom_Rectangle().default).__pool.release(clip);
			(openfl_geom_Matrix().default).__pool.release(matrix);
		}
	}
});
DOMMaskManager.prototype.__class__ = $hxClasses["openfl._internal.renderer.dom.DOMMaskManager"] = DOMMaskManager;

// Init



// Statics




// Export

exports.default = DOMMaskManager;