import * as encoding from "lib0/encoding";
import * as decoding from "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) => {
	encoding.writeVarUint(encoder, AuthMessageType.Token);
	encoding.writeVarString(encoder, auth);
};
const writePermissionDenied = (encoder, reason) => {
	encoding.writeVarUint(encoder, AuthMessageType.PermissionDenied);
	encoding.writeVarString(encoder, reason);
};
const writeAuthenticated = (encoder, scope) => {
	encoding.writeVarUint(encoder, AuthMessageType.Authenticated);
	encoding.writeVarString(encoder, scope);
};
const writeTokenSyncRequest = (encoder) => {
	encoding.writeVarUint(encoder, AuthMessageType.Token);
};
const readAuthMessage = (decoder, sendToken, permissionDeniedHandler, authenticatedHandler) => {
	switch (decoding.readVarUint(decoder)) {
		case AuthMessageType.Token:
			sendToken();
			break;
		case AuthMessageType.PermissionDenied:
			permissionDeniedHandler(decoding.readVarString(decoder));
			break;
		case AuthMessageType.Authenticated:
			authenticatedHandler(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
export { AuthMessageType, ConnectionTimeout, Forbidden, MessageTooBig, ResetConnection, SkipFurtherHooksError, Unauthorized, WsReadyStates, awarenessStatesToArray, makeRoutingKey, parseRoutingKey, readAuthMessage, writeAuthenticated, writeAuthentication, writePermissionDenied, writeTokenSyncRequest };
//# sourceMappingURL=hocuspocus-common.esm.js.map