// Class: openfl._internal.renderer.canvas.CanvasTilemap

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 openfl_geom_Rectangle() {return require("./../../../../openfl/geom/Rectangle");}
function openfl_geom_Matrix() {return require("./../../../../openfl/geom/Matrix");}
function lime_graphics_utils_ImageCanvasUtil() {return require("./../../../../lime/graphics/utils/ImageCanvasUtil");}

// Constructor

var CanvasTilemap = function(){}

// Meta

CanvasTilemap.__name__ = ["openfl","_internal","renderer","canvas","CanvasTilemap"];
CanvasTilemap.prototype = {
	
};
CanvasTilemap.prototype.__class__ = $hxClasses["openfl._internal.renderer.canvas.CanvasTilemap"] = CanvasTilemap;

// Init



// Statics

CanvasTilemap.render = function(tilemap,renderer) {
	if(!tilemap.__renderable || tilemap.__group.__tiles.length == 0 || tilemap.__worldAlpha <= 0) {
		return;
	}
	var context = renderer.context;
	renderer.__setBlendMode(tilemap.__worldBlendMode);
	renderer.__pushMaskObject(tilemap);
	var rect = (openfl_geom_Rectangle().default).__pool.get();
	rect.setTo(0,0,tilemap.__width,tilemap.__height);
	renderer.__pushMaskRect(rect,tilemap.__renderTransform);
	if(!renderer.__allowSmoothing || !tilemap.smoothing) {
		context.mozImageSmoothingEnabled = false;
		context.msImageSmoothingEnabled = false;
		context.imageSmoothingEnabled = false;
	}
	CanvasTilemap.renderTileContainer(tilemap.__group,renderer,tilemap.__renderTransform,tilemap.__tileset,renderer.__allowSmoothing && tilemap.smoothing,tilemap.tileAlphaEnabled,tilemap.__worldAlpha,null,null,rect);
	if(!renderer.__allowSmoothing || !tilemap.smoothing) {
		context.mozImageSmoothingEnabled = true;
		context.msImageSmoothingEnabled = true;
		context.imageSmoothingEnabled = true;
	}
	renderer.__popMaskRect();
	renderer.__popMaskObject(tilemap);
	(openfl_geom_Rectangle().default).__pool.release(rect);
}
CanvasTilemap.renderTileContainer = function(group,renderer,parentTransform,defaultTileset,smooth,alphaEnabled,worldAlpha,cacheBitmapData,source,rect) {
	var context = renderer.context;
	var roundPixels = renderer.__roundPixels;
	var tileTransform = (openfl_geom_Matrix().default).__pool.get();
	var tiles = group.__tiles;
	var length = group.__length;
	var tile;
	var tileset;
	var alpha;
	var visible;
	var id;
	var tileData;
	var tileRect;
	var bitmapData;
	var _g1 = 0;
	var _g = length;
	while(_g1 < _g) {
		var i = _g1++;
		tile = tiles[i];
		tileTransform.setTo(1,0,0,1,-tile.get_originX(),-tile.get_originY());
		tileTransform.concat(tile.get_matrix());
		tileTransform.concat(parentTransform);
		if(roundPixels) {
			tileTransform.tx = Math.round(tileTransform.tx);
			tileTransform.ty = Math.round(tileTransform.ty);
		}
		if(tile.get_tileset() != null) {
			tileset = tile.get_tileset();
		} else {
			tileset = defaultTileset;
		}
		alpha = tile.get_alpha() * worldAlpha;
		visible = tile.get_visible();
		if(!visible || alpha <= 0) {
			continue;
		}
		if(!alphaEnabled) {
			alpha = 1;
		}
		if(tile.__length > 0) {
			CanvasTilemap.renderTileContainer(tile,renderer,tileTransform,tileset,smooth,alphaEnabled,alpha,cacheBitmapData,source,rect);
		} else {
			if(tileset == null) {
				continue;
			}
			id = tile.get_id();
			if(id == -1) {
				tileRect = tile.get_rect();
				if(tileRect == null || tileRect.width <= 0 || tileRect.height <= 0) {
					continue;
				}
			} else {
				tileData = tileset.__data[id];
				if(tileData == null) {
					continue;
				}
				rect.setTo(tileData.x,tileData.y,tileData.width,tileData.height);
				tileRect = rect;
			}
			bitmapData = tileset.__bitmapData;
			if(bitmapData == null) {
				continue;
			}
			if(bitmapData != cacheBitmapData) {
				if(bitmapData.image.buffer.__srcImage == null) {
					(lime_graphics_utils_ImageCanvasUtil().default).convertToCanvas(bitmapData.image);
				}
				source = bitmapData.image.get_src();
				cacheBitmapData = bitmapData;
			}
			context.globalAlpha = alpha;
			renderer.setTransform(tileTransform,context);
			context.drawImage(source,tileRect.x,tileRect.y,tileRect.width,tileRect.height,0,0,tileRect.width,tileRect.height);
		}
	}
	(openfl_geom_Matrix().default).__pool.release(tileTransform);
}


// Export

exports.default = CanvasTilemap;