import { literalUnion, valita_exports } from "../../shared/src/valita.js";
import { jsonSchema } from "../../shared/src/json-schema.js";
import { mutationIDSchema } from "./mutation-id.js";
import { AuthInvalidated, ClientNotFound, Internal, InvalidConnectionRequest, InvalidConnectionRequestBaseCookie, InvalidConnectionRequestClientDeleted, InvalidConnectionRequestLastMutationID, InvalidMessage, InvalidPush, MutationFailed, MutationRateLimited, PushFailed, Rebalance, Rehome, SchemaVersionNotSupported, ServerOverloaded, TransformFailed, Unauthorized, VersionNotSupported } from "./error-kind-enum.js";
import { Server, ZeroCache } from "./error-origin-enum.js";
import { Database, HTTP, Internal as Internal$1, OutOfOrderMutation, Parse, Timeout, UnsupportedPushVersion } from "./error-reason-enum.js";
//#region ../zero-protocol/src/error.ts
var basicErrorKindSchema = literalUnion(AuthInvalidated, ClientNotFound, InvalidConnectionRequest, InvalidConnectionRequestBaseCookie, InvalidConnectionRequestLastMutationID, InvalidConnectionRequestClientDeleted, InvalidMessage, InvalidPush, MutationRateLimited, MutationFailed, Unauthorized, VersionNotSupported, SchemaVersionNotSupported, Internal);
var basicErrorBodySchema = valita_exports.object({
	kind: basicErrorKindSchema,
	message: valita_exports.string(),
	origin: literalUnion(Server, ZeroCache).optional()
});
var backoffErrorKindSchema = literalUnion(Rebalance, Rehome, ServerOverloaded);
var backoffBodySchema = valita_exports.object({
	kind: backoffErrorKindSchema,
	message: valita_exports.string(),
	minBackoffMs: valita_exports.number().optional(),
	maxBackoffMs: valita_exports.number().optional(),
	reconnectParams: valita_exports.record(valita_exports.string()).optional(),
	origin: valita_exports.literal(ZeroCache).optional()
});
var pushFailedErrorKindSchema = valita_exports.literal(PushFailed);
var transformFailedErrorKindSchema = valita_exports.literal(TransformFailed);
var errorKindSchema = valita_exports.union(basicErrorKindSchema, backoffErrorKindSchema, pushFailedErrorKindSchema, transformFailedErrorKindSchema);
var pushFailedBaseSchema = valita_exports.object({
	kind: pushFailedErrorKindSchema,
	details: jsonSchema.optional(),
	mutationIDs: valita_exports.array(mutationIDSchema),
	message: valita_exports.string()
});
var pushFailedBodySchema = valita_exports.union(pushFailedBaseSchema.extend({
	origin: valita_exports.literal(Server),
	reason: literalUnion(Database, Parse, OutOfOrderMutation, UnsupportedPushVersion, Internal$1)
}), pushFailedBaseSchema.extend({
	origin: valita_exports.literal(ZeroCache),
	reason: valita_exports.literal(HTTP),
	status: valita_exports.number(),
	bodyPreview: valita_exports.string().optional()
}), pushFailedBaseSchema.extend({
	origin: valita_exports.literal(ZeroCache),
	reason: literalUnion(Timeout, Parse, Internal$1)
}));
var transformFailedBaseSchema = valita_exports.object({
	kind: transformFailedErrorKindSchema,
	details: jsonSchema.optional(),
	queryIDs: valita_exports.array(valita_exports.string()),
	message: valita_exports.string()
});
var transformFailedBodySchema = valita_exports.union(transformFailedBaseSchema.extend({
	origin: valita_exports.literal(Server),
	reason: literalUnion(Database, Parse, Internal$1)
}), transformFailedBaseSchema.extend({
	origin: valita_exports.literal(ZeroCache),
	reason: valita_exports.literal(HTTP),
	status: valita_exports.number(),
	bodyPreview: valita_exports.string().optional()
}), transformFailedBaseSchema.extend({
	origin: valita_exports.literal(ZeroCache),
	reason: literalUnion(Timeout, Parse, Internal$1)
}));
var errorBodySchema = valita_exports.union(basicErrorBodySchema, backoffBodySchema, pushFailedBodySchema, transformFailedBodySchema);
var errorMessageSchema = valita_exports.tuple([valita_exports.literal("error"), errorBodySchema]);
/**
* Represents an error used across zero-client, zero-cache, and zero-server.
*/
var ProtocolError = class extends Error {
	errorBody;
	constructor(errorBody, options) {
		super(errorBody.message, options);
		this.name = "ProtocolError";
		this.errorBody = errorBody;
	}
	get kind() {
		return this.errorBody.kind;
	}
};
function isProtocolError(error) {
	return error instanceof ProtocolError;
}
//#endregion
export { ProtocolError, errorKindSchema, errorMessageSchema, isProtocolError, pushFailedBodySchema, transformFailedBodySchema };

//# sourceMappingURL=error.js.map