// Class: lime.math.Rectangle

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;
function lime_math_Vector2() {return require("./../../lime/math/Vector2");}

// Constructor

var Rectangle = function(x,y,width,height) {
	if(height == null) {
		height = 0;
	}
	if(width == null) {
		width = 0;
	}
	if(y == null) {
		y = 0;
	}
	if(x == null) {
		x = 0;
	}
	this.x = x;
	this.y = y;
	this.width = width;
	this.height = height;
}

// Meta

Rectangle.__name__ = ["lime","math","Rectangle"];
Rectangle.prototype = {
	clone: function() {
		return new Rectangle(this.x,this.y,this.width,this.height);
	},
	contains: function(x,y) {
		if(x >= this.x && y >= this.y && x < this.get_right()) {
			return y < this.get_bottom();
		} else {
			return false;
		}
	},
	containsPoint: function(point) {
		return this.contains(point.x,point.y);
	},
	containsRect: function(rect) {
		if(rect.width <= 0 || rect.height <= 0) {
			if(rect.x > this.x && rect.y > this.y && rect.get_right() < this.get_right()) {
				return rect.get_bottom() < this.get_bottom();
			} else {
				return false;
			}
		} else if(rect.x >= this.x && rect.y >= this.y && rect.get_right() <= this.get_right()) {
			return rect.get_bottom() <= this.get_bottom();
		} else {
			return false;
		}
	},
	copyFrom: function(sourceRect) {
		this.x = sourceRect.x;
		this.y = sourceRect.y;
		this.width = sourceRect.width;
		this.height = sourceRect.height;
	},
	equals: function(toCompare) {
		if(toCompare != null && this.x == toCompare.x && this.y == toCompare.y && this.width == toCompare.width) {
			return this.height == toCompare.height;
		} else {
			return false;
		}
	},
	inflate: function(dx,dy) {
		this.x -= dx;
		this.width += dx * 2;
		this.y -= dy;
		this.height += dy * 2;
	},
	inflatePoint: function(point) {
		this.inflate(point.x,point.y);
	},
	intersection: function(toIntersect) {
		var x0 = this.x < toIntersect.x ? toIntersect.x : this.x;
		var x1 = this.get_right() > toIntersect.get_right() ? toIntersect.get_right() : this.get_right();
		if(x1 <= x0) {
			return new Rectangle();
		}
		var y0 = this.y < toIntersect.y ? toIntersect.y : this.y;
		var y1 = this.get_bottom() > toIntersect.get_bottom() ? toIntersect.get_bottom() : this.get_bottom();
		if(y1 <= y0) {
			return new Rectangle();
		}
		return new Rectangle(x0,y0,x1 - x0,y1 - y0);
	},
	intersects: function(toIntersect) {
		var x0 = this.x < toIntersect.x ? toIntersect.x : this.x;
		var x1 = this.get_right() > toIntersect.get_right() ? toIntersect.get_right() : this.get_right();
		if(x1 <= x0) {
			return false;
		}
		var y0 = this.y < toIntersect.y ? toIntersect.y : this.y;
		var y1 = this.get_bottom() > toIntersect.get_bottom() ? toIntersect.get_bottom() : this.get_bottom();
		return y1 > y0;
	},
	isEmpty: function() {
		if(!(this.width <= 0)) {
			return this.height <= 0;
		} else {
			return true;
		}
	},
	offset: function(dx,dy) {
		this.x += dx;
		this.y += dy;
	},
	offsetPoint: function(point) {
		this.x += point.x;
		this.y += point.y;
	},
	setEmpty: function() {
		this.x = this.y = this.width = this.height = 0;
	},
	setTo: function(xa,ya,widtha,heighta) {
		this.x = xa;
		this.y = ya;
		this.width = widtha;
		this.height = heighta;
	},
	transform: function(m) {
		var tx0 = m.a * this.x + m.c * this.y;
		var tx1 = tx0;
		var ty0 = m.b * this.x + m.d * this.y;
		var ty1 = ty0;
		var tx = m.a * (this.x + this.width) + m.c * this.y;
		var ty = m.b * (this.x + this.width) + m.d * this.y;
		if(tx < tx0) {
			tx0 = tx;
		}
		if(ty < ty0) {
			ty0 = ty;
		}
		if(tx > tx1) {
			tx1 = tx;
		}
		if(ty > ty1) {
			ty1 = ty;
		}
		tx = m.a * (this.x + this.width) + m.c * (this.y + this.height);
		ty = m.b * (this.x + this.width) + m.d * (this.y + this.height);
		if(tx < tx0) {
			tx0 = tx;
		}
		if(ty < ty0) {
			ty0 = ty;
		}
		if(tx > tx1) {
			tx1 = tx;
		}
		if(ty > ty1) {
			ty1 = ty;
		}
		tx = m.a * this.x + m.c * (this.y + this.height);
		ty = m.b * this.x + m.d * (this.y + this.height);
		if(tx < tx0) {
			tx0 = tx;
		}
		if(ty < ty0) {
			ty0 = ty;
		}
		if(tx > tx1) {
			tx1 = tx;
		}
		if(ty > ty1) {
			ty1 = ty;
		}
		return new Rectangle(tx0 + m.tx,ty0 + m.ty,tx1 - tx0,ty1 - ty0);
	},
	union: function(toUnion) {
		if(this.width == 0 || this.height == 0) {
			return toUnion.clone();
		} else if(toUnion.width == 0 || toUnion.height == 0) {
			return this.clone();
		}
		var x0 = this.x > toUnion.x ? toUnion.x : this.x;
		var x1 = this.get_right() < toUnion.get_right() ? toUnion.get_right() : this.get_right();
		var y0 = this.y > toUnion.y ? toUnion.y : this.y;
		var y1 = this.get_bottom() < toUnion.get_bottom() ? toUnion.get_bottom() : this.get_bottom();
		return new Rectangle(x0,y0,x1 - x0,y1 - y0);
	},
	__contract: function(x,y,width,height) {
		if(this.width == 0 && this.height == 0) {
			return;
		}
		if(this.x < x) {
			this.x = x;
		}
		if(this.y < y) {
			this.y = y;
		}
		if(this.get_right() > x + width) {
			this.width = x + width - this.x;
		}
		if(this.get_bottom() > y + height) {
			this.height = y + height - this.y;
		}
	},
	__expand: function(x,y,width,height) {
		if(this.width == 0 && this.height == 0) {
			this.x = x;
			this.y = y;
			this.width = width;
			this.height = height;
			return;
		}
		var cacheRight = this.get_right();
		var cacheBottom = this.get_bottom();
		if(this.x > x) {
			this.x = x;
		}
		if(this.y > y) {
			this.y = y;
		}
		if(cacheRight < x + width) {
			this.width = x + width - this.x;
		}
		if(cacheBottom < y + height) {
			this.height = y + height - this.y;
		}
	},
	__toFlashRectangle: function() {
		return null;
	},
	get_bottom: function() {
		return this.y + this.height;
	},
	set_bottom: function(b) {
		this.height = b - this.y;
		return b;
	},
	get_bottomRight: function() {
		return new (lime_math_Vector2().default)(this.x + this.width,this.y + this.height);
	},
	set_bottomRight: function(p) {
		this.width = p.x - this.x;
		this.height = p.y - this.y;
		return p.clone();
	},
	get_left: function() {
		return this.x;
	},
	set_left: function(l) {
		this.width -= l - this.x;
		this.x = l;
		return l;
	},
	get_right: function() {
		return this.x + this.width;
	},
	set_right: function(r) {
		this.width = r - this.x;
		return r;
	},
	get_size: function() {
		return new (lime_math_Vector2().default)(this.width,this.height);
	},
	set_size: function(p) {
		this.width = p.x;
		this.height = p.y;
		return p.clone();
	},
	get_top: function() {
		return this.y;
	},
	set_top: function(t) {
		this.height -= t - this.y;
		this.y = t;
		return t;
	},
	get_topLeft: function() {
		return new (lime_math_Vector2().default)(this.x,this.y);
	},
	set_topLeft: function(p) {
		this.x = p.x;
		this.y = p.y;
		return p.clone();
	}
};
Rectangle.prototype.__class__ = $hxClasses["lime.math.Rectangle"] = Rectangle;

// Init



// Statics




// Export

exports.default = Rectangle;