import { __commonJSMin } from "../../../../_virtual/rolldown_runtime.js";
import { require_api } from "../common/api.js";
var require_ril = /* @__PURE__ */ __commonJSMin(((e) => {
	Object.defineProperty(e, "__esModule", { value: !0 });
	var n = require_api(), r = class e extends n.AbstractMessageBuffer {
		constructor(e = "utf-8") {
			super(e), this.asciiDecoder = new TextDecoder("ascii");
		}
		emptyBuffer() {
			return e.emptyBuffer;
		}
		fromString(e, t) {
			return new TextEncoder().encode(e);
		}
		toString(e, t) {
			return t === "ascii" ? this.asciiDecoder.decode(e) : new TextDecoder(t).decode(e);
		}
		asNative(e, t) {
			return t === void 0 ? e : e.slice(0, t);
		}
		allocNative(e) {
			return new Uint8Array(e);
		}
	};
	r.emptyBuffer = new Uint8Array();
	var i = class {
		constructor(e) {
			this.socket = e, this._onData = new n.Emitter(), this._messageListener = (e) => {
				e.data.arrayBuffer().then((e) => {
					this._onData.fire(new Uint8Array(e));
				}, () => {
					(0, n.RAL)().console.error("Converting blob to array buffer failed.");
				});
			}, this.socket.addEventListener("message", this._messageListener);
		}
		onClose(e) {
			return this.socket.addEventListener("close", e), n.Disposable.create(() => this.socket.removeEventListener("close", e));
		}
		onError(e) {
			return this.socket.addEventListener("error", e), n.Disposable.create(() => this.socket.removeEventListener("error", e));
		}
		onEnd(e) {
			return this.socket.addEventListener("end", e), n.Disposable.create(() => this.socket.removeEventListener("end", e));
		}
		onData(e) {
			return this._onData.event(e);
		}
	}, a = class {
		constructor(e) {
			this.socket = e;
		}
		onClose(e) {
			return this.socket.addEventListener("close", e), n.Disposable.create(() => this.socket.removeEventListener("close", e));
		}
		onError(e) {
			return this.socket.addEventListener("error", e), n.Disposable.create(() => this.socket.removeEventListener("error", e));
		}
		onEnd(e) {
			return this.socket.addEventListener("end", e), n.Disposable.create(() => this.socket.removeEventListener("end", e));
		}
		write(e, t) {
			if (typeof e == "string") {
				if (t !== void 0 && t !== "utf-8") throw Error(`In a Browser environments only utf-8 text encoding is supported. But got encoding: ${t}`);
				this.socket.send(e);
			} else this.socket.send(e);
			return Promise.resolve();
		}
		end() {
			this.socket.close();
		}
	}, o = new TextEncoder(), s = Object.freeze({
		messageBuffer: Object.freeze({ create: (e) => new r(e) }),
		applicationJson: Object.freeze({
			encoder: Object.freeze({
				name: "application/json",
				encode: (e, t) => {
					if (t.charset !== "utf-8") throw Error(`In a Browser environments only utf-8 text encoding is supported. But got encoding: ${t.charset}`);
					return Promise.resolve(o.encode(JSON.stringify(e, void 0, 0)));
				}
			}),
			decoder: Object.freeze({
				name: "application/json",
				decode: (e, t) => {
					if (!(e instanceof Uint8Array)) throw Error("In a Browser environments only Uint8Arrays are supported.");
					return Promise.resolve(JSON.parse(new TextDecoder(t.charset).decode(e)));
				}
			})
		}),
		stream: Object.freeze({
			asReadableStream: (e) => new i(e),
			asWritableStream: (e) => new a(e)
		}),
		console,
		timer: Object.freeze({
			setTimeout(e, t, ...n) {
				let r = setTimeout(e, t, ...n);
				return { dispose: () => clearTimeout(r) };
			},
			setImmediate(e, ...t) {
				let n = setTimeout(e, 0, ...t);
				return { dispose: () => clearTimeout(n) };
			},
			setInterval(e, t, ...n) {
				let r = setInterval(e, t, ...n);
				return { dispose: () => clearInterval(r) };
			}
		})
	});
	function c() {
		return s;
	}
	(function(e) {
		function t() {
			n.RAL.install(s);
		}
		e.install = t;
	})(c ||= {}), e.default = c;
}));
export default require_ril();
export { require_ril };
