import { __commonJSMin as e } from "../../../../_virtual/_rolldown/runtime.js";
import { require_ral as t } from "./ral.js";
import { require_is as n } from "./is.js";
import { require_events as r } from "./events.js";
import { require_semaphore as i } from "./semaphore.js";
//#region ../../node_modules/vscode-jsonrpc/lib/common/messageReader.js
var a = /* @__PURE__ */ e(((e) => {
	Object.defineProperty(e, "__esModule", { value: !0 }), e.ReadableStreamMessageReader = e.AbstractMessageReader = e.MessageReader = void 0;
	var a = t(), o = n(), s = r(), c = i(), l;
	(function(e) {
		function t(e) {
			let t = e;
			return t && o.func(t.listen) && o.func(t.dispose) && o.func(t.onError) && o.func(t.onClose) && o.func(t.onPartialMessage);
		}
		e.is = t;
	})(l || (e.MessageReader = l = {}));
	var u = class {
		constructor() {
			this.errorEmitter = new s.Emitter(), this.closeEmitter = new s.Emitter(), this.partialMessageEmitter = new s.Emitter();
		}
		dispose() {
			this.errorEmitter.dispose(), this.closeEmitter.dispose();
		}
		get onError() {
			return this.errorEmitter.event;
		}
		fireError(e) {
			this.errorEmitter.fire(this.asError(e));
		}
		get onClose() {
			return this.closeEmitter.event;
		}
		fireClose() {
			this.closeEmitter.fire(void 0);
		}
		get onPartialMessage() {
			return this.partialMessageEmitter.event;
		}
		firePartialMessage(e) {
			this.partialMessageEmitter.fire(e);
		}
		asError(e) {
			return e instanceof Error ? e : /* @__PURE__ */ Error(`Reader received error. Reason: ${o.string(e.message) ? e.message : "unknown"}`);
		}
	};
	e.AbstractMessageReader = u;
	var d;
	(function(e) {
		function t(e) {
			let t, n, r = /* @__PURE__ */ new Map(), i, o = /* @__PURE__ */ new Map();
			if (e === void 0 || typeof e == "string") t = e ?? "utf-8";
			else {
				if (t = e.charset ?? "utf-8", e.contentDecoder !== void 0 && (n = e.contentDecoder, r.set(n.name, n)), e.contentDecoders !== void 0) for (let t of e.contentDecoders) r.set(t.name, t);
				if (e.contentTypeDecoder !== void 0 && (i = e.contentTypeDecoder, o.set(i.name, i)), e.contentTypeDecoders !== void 0) for (let t of e.contentTypeDecoders) o.set(t.name, t);
			}
			return i === void 0 && (i = (0, a.default)().applicationJson.decoder, o.set(i.name, i)), {
				charset: t,
				contentDecoder: n,
				contentDecoders: r,
				contentTypeDecoder: i,
				contentTypeDecoders: o
			};
		}
		e.fromOptions = t;
	})(d ||= {}), e.ReadableStreamMessageReader = class extends u {
		constructor(e, t) {
			super(), this.readable = e, this.options = d.fromOptions(t), this.buffer = (0, a.default)().messageBuffer.create(this.options.charset), this._partialMessageTimeout = 1e4, this.nextMessageLength = -1, this.messageToken = 0, this.readSemaphore = new c.Semaphore(1);
		}
		set partialMessageTimeout(e) {
			this._partialMessageTimeout = e;
		}
		get partialMessageTimeout() {
			return this._partialMessageTimeout;
		}
		listen(e) {
			this.nextMessageLength = -1, this.messageToken = 0, this.partialMessageTimer = void 0, this.callback = e;
			let t = this.readable.onData((e) => {
				this.onData(e);
			});
			return this.readable.onError((e) => this.fireError(e)), this.readable.onClose(() => this.fireClose()), t;
		}
		onData(e) {
			try {
				for (this.buffer.append(e);;) {
					if (this.nextMessageLength === -1) {
						let e = this.buffer.tryReadHeaders(!0);
						if (!e) return;
						let t = e.get("content-length");
						if (!t) {
							this.fireError(/* @__PURE__ */ Error(`Header must provide a Content-Length property.\n${JSON.stringify(Object.fromEntries(e))}`));
							return;
						}
						let n = parseInt(t);
						if (isNaN(n)) {
							this.fireError(/* @__PURE__ */ Error(`Content-Length value must be a number. Got ${t}`));
							return;
						}
						this.nextMessageLength = n;
					}
					let e = this.buffer.tryReadBody(this.nextMessageLength);
					if (e === void 0) {
						this.setPartialMessageTimer();
						return;
					}
					this.clearPartialMessageTimer(), this.nextMessageLength = -1, this.readSemaphore.lock(async () => {
						let t = this.options.contentDecoder === void 0 ? e : await this.options.contentDecoder.decode(e), n = await this.options.contentTypeDecoder.decode(t, this.options);
						this.callback(n);
					}).catch((e) => {
						this.fireError(e);
					});
				}
			} catch (e) {
				this.fireError(e);
			}
		}
		clearPartialMessageTimer() {
			this.partialMessageTimer &&= (this.partialMessageTimer.dispose(), void 0);
		}
		setPartialMessageTimer() {
			this.clearPartialMessageTimer(), !(this._partialMessageTimeout <= 0) && (this.partialMessageTimer = (0, a.default)().timer.setTimeout((e, t) => {
				this.partialMessageTimer = void 0, e === this.messageToken && (this.firePartialMessage({
					messageToken: e,
					waitingTime: t
				}), this.setPartialMessageTimer());
			}, this._partialMessageTimeout, this.messageToken, this._partialMessageTimeout));
		}
	};
}));
//#endregion
export default a();
export { a as require_messageReader };
