// Class: lime._internal.unifill.InternalEncoding

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 StringTools() {return require("./../../../StringTools");}
function lime__$internal_unifill__$Utf16_Utf16_$Impl_$() {return require("./../../../lime/_internal/unifill/_Utf16/Utf16_Impl_");}
function HxOverrides() {return require("./../../../HxOverrides");}
function lime__$internal_unifill__$Utf16_Utf16Impl() {return require("./../../../lime/_internal/unifill/_Utf16/Utf16Impl");}
function lime__$internal_unifill_Unicode() {return require("./../../../lime/_internal/unifill/Unicode");}
function StringBuf() {return require("./../../../StringBuf");}
function js__$Boot_HaxeError() {return require("./../../../js/_Boot/HaxeError");}
function js_Boot() {return require("./../../../js/Boot");}
function lime__$internal_unifill_InvalidCodeUnitSequence() {return require("./../../../lime/_internal/unifill/InvalidCodeUnitSequence");}

// Constructor

var InternalEncoding = function(){}

// Meta

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

// Init



// Statics

InternalEncoding.get_internalEncoding = function() {
	return "UTF-16";
}
InternalEncoding.codeUnitAt = function(s,index) {
	var this1 = s;
	var this2 = this1;
	return (StringTools().default).fastCodeAt(this2,index);
}
InternalEncoding.codePointAt = function(s,index) {
	var this1 = s;
	var this2 = this1;
	return (lime__$internal_unifill__$Utf16_Utf16_$Impl_$().default).codePointAt(this2,index);
}
InternalEncoding.charAt = function(s,index) {
	var this1 = s;
	var this2 = this1;
	var this3 = this2;
	var c = (StringTools().default).fastCodeAt(this3,index);
	var this4 = (HxOverrides().default).substr(this3,index,(lime__$internal_unifill__$Utf16_Utf16Impl().default).code_point_width(c));
	var this5 = this4;
	return this5;
}
InternalEncoding.codePointCount = function(s,beginIndex,endIndex) {
	var this1 = s;
	var this2 = this1;
	return (lime__$internal_unifill__$Utf16_Utf16_$Impl_$().default).codePointCount(this2,beginIndex,endIndex);
}
InternalEncoding.codePointWidthAt = function(s,index) {
	var this1 = s;
	var this2 = this1;
	var c = (StringTools().default).fastCodeAt(this2,index);
	return (lime__$internal_unifill__$Utf16_Utf16Impl().default).code_point_width(c);
}
InternalEncoding.codePointWidthBefore = function(s,index) {
	var this1 = s;
	var this2 = this1;
	var this3 = this2;
	return (lime__$internal_unifill__$Utf16_Utf16Impl().default).find_prev_code_point(function(i) {
		return (StringTools().default).fastCodeAt(this3,i);
	},index);
}
InternalEncoding.offsetByCodePoints = function(s,index,codePointOffset) {
	var this1 = s;
	var this2 = this1;
	var this3 = this2;
	if(codePointOffset >= 0) {
		var index1 = index;
		var len = this3.length;
		var i = 0;
		while(i < codePointOffset && index1 < len) {
			var c = (StringTools().default).fastCodeAt(this3,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(this4) {
				return function(i1) {
					return (StringTools().default).fastCodeAt(this4[0],i1);
				};
			})([this3]),index2);
			++count;
		}
		return index2;
	}
}
InternalEncoding.backwardOffsetByCodePoints = function(s,index,codePointOffset) {
	var this1 = s;
	var this2 = this1;
	var this3 = this2;
	var codePointOffset1 = -codePointOffset;
	if(codePointOffset1 >= 0) {
		var index1 = index;
		var len = this3.length;
		var i = 0;
		while(i < codePointOffset1 && index1 < len) {
			var c = (StringTools().default).fastCodeAt(this3,index1);
			index1 += (lime__$internal_unifill__$Utf16_Utf16Impl().default).code_point_width(c);
			++i;
		}
		return index1;
	} else {
		var index2 = index;
		var count = 0;
		while(count < -codePointOffset1 && 0 < index2) {
			index2 -= (lime__$internal_unifill__$Utf16_Utf16Impl().default).find_prev_code_point((function(this4) {
				return function(i1) {
					return (StringTools().default).fastCodeAt(this4[0],i1);
				};
			})([this3]),index2);
			++count;
		}
		return index2;
	}
}
InternalEncoding.fromCodePoint = function(codePoint) {
	var this1;
	if(codePoint <= 65535) {
		var this2 = String.fromCharCode(codePoint);
		var this3 = this2;
		this1 = this3;
	} else {
		var u0 = (lime__$internal_unifill_Unicode().default).encodeHighSurrogate(codePoint);
		var u1 = (lime__$internal_unifill_Unicode().default).encodeLowSurrogate(codePoint);
		var this4 = String.fromCharCode(u0) + String.fromCharCode(u1);
		var this5 = this4;
		this1 = this5;
	}
	return this1;
}
InternalEncoding.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;
}
InternalEncoding.validate = function(s) {
	var this1 = s;
	var this2 = this1;
	(lime__$internal_unifill__$Utf16_Utf16_$Impl_$().default).validate(this2);
}
InternalEncoding.isValidString = function(s) {
	try {
		InternalEncoding.validate(s);
		return true;
	} catch( e ) {
		if (e instanceof (js__$Boot_HaxeError().default)) e = e.val;
		if( (js_Boot().default).__instanceof(e,(lime__$internal_unifill_InvalidCodeUnitSequence().default)) ) {
			return false;
		} else throw(e);
	}
}
InternalEncoding.encodeWith = function(f,c) {
	(lime__$internal_unifill__$Utf16_Utf16Impl().default).encode_code_point(f,c);
}


// Export

exports.default = InternalEncoding;