// Class: lime._internal.unifill._Utf16.Utf16_Impl_

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 $iterator = require("./../../../../iterator_stub").default;
function lime__$internal_unifill_Unicode() {return require("./../../../../lime/_internal/unifill/Unicode");}
function StringBuf() {return require("./../../../../StringBuf");}
function lime__$internal_unifill__$Utf16_Utf16Impl() {return require("./../../../../lime/_internal/unifill/_Utf16/Utf16Impl");}
function StringTools() {return require("./../../../../StringTools");}
function HxOverrides() {return require("./../../../../HxOverrides");}

// Constructor

var Utf16_Impl_ = function(){}

// Meta

Utf16_Impl_.__name__ = ["lime","_internal","unifill","_Utf16","Utf16_Impl_"];
Utf16_Impl_.prototype = {
	
};
Utf16_Impl_.prototype.__class__ = Utf16_Impl_.prototype.constructor = $hxClasses["lime._internal.unifill._Utf16.Utf16_Impl_"] = Utf16_Impl_;

// Init



// Statics

Utf16_Impl_.fromCodePoint = function(codePoint) {
	if(codePoint <= 65535) {
		var this1 = String.fromCharCode(codePoint);
		var this2 = this1;
		return this2;
	} else {
		var u0 = (lime__$internal_unifill_Unicode().default).encodeHighSurrogate(codePoint);
		var u1 = (lime__$internal_unifill_Unicode().default).encodeLowSurrogate(codePoint);
		var this3 = String.fromCharCode(u0) + String.fromCharCode(u1);
		var this4 = this3;
		return this4;
	}
}
Utf16_Impl_.fromCodePoints = function(codePoints) {
	var this1 = new (StringBuf().default)();
	var buf = this1;
	var c = $iterator(codePoints)();
	while(c.hasNext()) {
		var c1 = c.next();
		(lime__$internal_unifill__$Utf16_Utf16Impl().default).encode_code_point(function(x) {
			buf.addChar(x);
		},c1);
	}
	var this2 = buf.toString();
	var this3 = this2;
	return this3;
}
Utf16_Impl_.fromString = function(s) {
	var this1 = s;
	var this2 = this1;
	return this2;
}
Utf16_Impl_.fromArray = function(a) {
	var buf = new (StringBuf().default)();
	var _g = 0;
	while(_g < a.length) {
		var x = a[_g];
		++_g;
		buf.addChar(x);
	}
	var this1 = buf.toString();
	var this2 = this1;
	return this2;
}
Utf16_Impl_.encodeWith = function(f,c) {
	(lime__$internal_unifill__$Utf16_Utf16Impl().default).encode_code_point(f,c);
}
Utf16_Impl_.codeUnitAt = function(this1,index) {
	return (StringTools().default).fastCodeAt(this1,index);
}
Utf16_Impl_.codePointAt = function(this1,index) {
	return (lime__$internal_unifill__$Utf16_Utf16Impl().default).decode_code_point(this1.length,function(i) {
		return (StringTools().default).fastCodeAt(this1,i);
	},index);
}
Utf16_Impl_.charAt = function(this1,index) {
	var c = (StringTools().default).fastCodeAt(this1,index);
	var this2 = (HxOverrides().default).substr(this1,index,(lime__$internal_unifill__$Utf16_Utf16Impl().default).code_point_width(c));
	var this3 = this2;
	return this3;
}
Utf16_Impl_.codePointCount = function(this1,beginIndex,endIndex) {
	var index = beginIndex;
	var i = 0;
	while(index < endIndex) {
		var c = (StringTools().default).fastCodeAt(this1,index);
		index += (lime__$internal_unifill__$Utf16_Utf16Impl().default).code_point_width(c);
		++i;
	}
	return i;
}
Utf16_Impl_.codePointWidthAt = function(this1,index) {
	var c = (StringTools().default).fastCodeAt(this1,index);
	return (lime__$internal_unifill__$Utf16_Utf16Impl().default).code_point_width(c);
}
Utf16_Impl_.codePointWidthBefore = function(this1,index) {
	return (lime__$internal_unifill__$Utf16_Utf16Impl().default).find_prev_code_point(function(i) {
		return (StringTools().default).fastCodeAt(this1,i);
	},index);
}
Utf16_Impl_.offsetByCodePoints = function(this1,index,codePointOffset) {
	if(codePointOffset >= 0) {
		var index1 = index;
		var len = this1.length;
		var i = 0;
		while(i < codePointOffset && index1 < len) {
			var c = (StringTools().default).fastCodeAt(this1,index1);
			index1 += (lime__$internal_unifill__$Utf16_Utf16Impl().default).code_point_width(c);
			++i;
		}
		return index1;
	} else {
		var index2 = index;
		var count = 0;
		while(count < -codePointOffset && 0 < index2) {
			index2 -= (lime__$internal_unifill__$Utf16_Utf16Impl().default).find_prev_code_point((function(this2) {
				return function(i1) {
					return (StringTools().default).fastCodeAt(this2[0],i1);
				};
			})([this1]),index2);
			++count;
		}
		return index2;
	}
}
Utf16_Impl_.substr = function(this1,index,len) {
	var this2 = (HxOverrides().default).substr(this1,index,len);
	var this3 = this2;
	return this3;
}
Utf16_Impl_.validate = function(this1) {
	var len = this1.length;
	var accessor = function(i) {
		return (StringTools().default).fastCodeAt(this1,i);
	};
	var i1 = 0;
	while(i1 < len) {
		(lime__$internal_unifill__$Utf16_Utf16Impl().default).decode_code_point(len,accessor,i1);
		var c = (StringTools().default).fastCodeAt(this1,i1);
		i1 += (lime__$internal_unifill__$Utf16_Utf16Impl().default).code_point_width(c);
	}
}
Utf16_Impl_.toString = function(this1) {
	return this1;
}
Utf16_Impl_.toArray = function(this1) {
	var i = 0;
	var len = this1.length;
	var _g = [];
	while(i < len) _g.push((StringTools().default).fastCodeAt(this1,i++));
	return _g;
}
Utf16_Impl_._new = function(s) {
	var this1 = s;
	return this1;
}
Utf16_Impl_.get_length = function(this1) {
	return this1.length;
}
Utf16_Impl_.forward_offset_by_code_points = function(this1,index,codePointOffset) {
	var len = this1.length;
	var i = 0;
	while(i < codePointOffset && index < len) {
		var c = (StringTools().default).fastCodeAt(this1,index);
		index += (lime__$internal_unifill__$Utf16_Utf16Impl().default).code_point_width(c);
		++i;
	}
	return index;
}
Utf16_Impl_.backward_offset_by_code_points = function(this1,index,codePointOffset) {
	var count = 0;
	while(count < codePointOffset && 0 < index) {
		index -= (lime__$internal_unifill__$Utf16_Utf16Impl().default).find_prev_code_point((function(this2) {
			return function(i) {
				return (StringTools().default).fastCodeAt(this2[0],i);
			};
		})([this1]),index);
		++count;
	}
	return index;
}


// Export

exports.default = Utf16_Impl_;