Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
//#region \0rolldown/runtime.js
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __copyProps = (to, from, except, desc) => {
	if (from && typeof from === "object" || typeof from === "function") {
		for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
			key = keys[i];
			if (!__hasOwnProp.call(to, key) && key !== except) {
				__defProp(to, key, {
					get: ((k) => from[k]).bind(null, key),
					enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
				});
			}
		}
	}
	return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
	value: mod,
	enumerable: true
}) : target, mod));

//#endregion
let lib0_encoding = require("lib0/encoding");
lib0_encoding = __toESM(lib0_encoding);
let lib0_decoding = require("lib0/decoding");
lib0_decoding = __toESM(lib0_decoding);

//#region packages/common/src/auth.ts
let AuthMessageType = /* @__PURE__ */ function(AuthMessageType) {
	AuthMessageType[AuthMessageType["Token"] = 0] = "Token";
	AuthMessageType[AuthMessageType["PermissionDenied"] = 1] = "PermissionDenied";
	AuthMessageType[AuthMessageType["Authenticated"] = 2] = "Authenticated";
	return AuthMessageType;
}({});
const writeAuthentication = (encoder, auth) => {
	lib0_encoding.writeVarUint(encoder, AuthMessageType.Token);
	lib0_encoding.writeVarString(encoder, auth);
};
const writePermissionDenied = (encoder, reason) => {
	lib0_encoding.writeVarUint(encoder, AuthMessageType.PermissionDenied);
	lib0_encoding.writeVarString(encoder, reason);
};
const writeAuthenticated = (encoder, scope) => {
	lib0_encoding.writeVarUint(encoder, AuthMessageType.Authenticated);
	lib0_encoding.writeVarString(encoder, scope);
};
const writeTokenSyncRequest = (encoder) => {
	lib0_encoding.writeVarUint(encoder, AuthMessageType.Token);
};
const readAuthMessage = (decoder, sendToken, permissionDeniedHandler, authenticatedHandler) => {
	switch (lib0_decoding.readVarUint(decoder)) {
		case AuthMessageType.Token:
			sendToken();
			break;
		case AuthMessageType.PermissionDenied:
			permissionDeniedHandler(lib0_decoding.readVarString(decoder));
			break;
		case AuthMessageType.Authenticated:
			authenticatedHandler(lib0_decoding.readVarString(decoder));
			break;
		default:
	}
};

//#endregion
//#region packages/common/src/CloseEvents.ts
/**
* The server is terminating the connection because a data frame was received
* that is too large.
* See: https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent/code
*/
const MessageTooBig = {
	code: 1009,
	reason: "Message Too Big"
};
/**
* The server successfully processed the request, asks that the requester reset
* its document view, and is not returning any content.
*/
const ResetConnection = {
	code: 4205,
	reason: "Reset Connection"
};
/**
* Similar to Forbidden, but specifically for use when authentication is required and has
* failed or has not yet been provided.
*/
const Unauthorized = {
	code: 4401,
	reason: "Unauthorized"
};
/**
* The request contained valid data and was understood by the server, but the server
* is refusing action.
*/
const Forbidden = {
	code: 4403,
	reason: "Forbidden"
};
/**
* The server timed out waiting for the request.
*/
const ConnectionTimeout = {
	code: 4408,
	reason: "Connection Timeout"
};

//#endregion
//#region packages/common/src/awarenessStatesToArray.ts
const awarenessStatesToArray = (states) => {
	return Array.from(states.entries()).map(([key, value]) => {
		return {
			clientId: key,
			...value
		};
	});
};

//#endregion
//#region packages/common/src/routingKey.ts
const SEPARATOR = "\0";
function makeRoutingKey(documentName, sessionId) {
	return documentName + SEPARATOR + sessionId;
}
function parseRoutingKey(key) {
	const idx = key.indexOf(SEPARATOR);
	if (idx === -1) return {
		documentName: key,
		sessionId: null
	};
	return {
		documentName: key.substring(0, idx),
		sessionId: key.substring(idx + 1)
	};
}

//#endregion
//#region packages/common/src/SkipFurtherHooksError.ts
var SkipFurtherHooksError = class extends Error {
	constructor(message) {
		super(message ?? "Further hooks skipped");
		this.name = "SkipFurtherHooksError";
	}
};

//#endregion
//#region packages/common/src/types.ts
/**
* State of the WebSocket connection.
* https://developer.mozilla.org/de/docs/Web/API/WebSocket/readyState
*/
let WsReadyStates = /* @__PURE__ */ function(WsReadyStates) {
	WsReadyStates[WsReadyStates["Connecting"] = 0] = "Connecting";
	WsReadyStates[WsReadyStates["Open"] = 1] = "Open";
	WsReadyStates[WsReadyStates["Closing"] = 2] = "Closing";
	WsReadyStates[WsReadyStates["Closed"] = 3] = "Closed";
	return WsReadyStates;
}({});

//#endregion
exports.AuthMessageType = AuthMessageType;
exports.ConnectionTimeout = ConnectionTimeout;
exports.Forbidden = Forbidden;
exports.MessageTooBig = MessageTooBig;
exports.ResetConnection = ResetConnection;
exports.SkipFurtherHooksError = SkipFurtherHooksError;
exports.Unauthorized = Unauthorized;
exports.WsReadyStates = WsReadyStates;
exports.awarenessStatesToArray = awarenessStatesToArray;
exports.makeRoutingKey = makeRoutingKey;
exports.parseRoutingKey = parseRoutingKey;
exports.readAuthMessage = readAuthMessage;
exports.writeAuthenticated = writeAuthenticated;
exports.writeAuthentication = writeAuthentication;
exports.writePermissionDenied = writePermissionDenied;
exports.writeTokenSyncRequest = writeTokenSyncRequest;
//# sourceMappingURL=hocuspocus-common.cjs.map