import { Exception } from "@adonisjs/core/exceptions";
//#region \0rolldown/runtime.js
var __defProp = Object.defineProperty;
var __exportAll = (all, no_symbols) => {
	let target = {};
	for (var name in all) __defProp(target, name, {
		get: all[name],
		enumerable: true
	});
	if (!no_symbols) __defProp(target, Symbol.toStringTag, { value: "Module" });
	return target;
};
//#endregion
//#region src/errors.ts
var errors_exports = /* @__PURE__ */ __exportAll({
	E_INVALID_CREDENTIALS: () => E_INVALID_CREDENTIALS,
	E_UNAUTHORIZED_ACCESS: () => E_UNAUTHORIZED_ACCESS
});
/**
* The "E_UNAUTHORIZED_ACCESS" exception is raised when unable to
* authenticate an incoming HTTP request.
*
* The "error.guardDriverName" can be used to know the driver which
* raised the error.
*/
const E_UNAUTHORIZED_ACCESS = class extends Exception {
	/**
	* HTTP status code for unauthorized access
	*/
	static status = 401;
	/**
	* Error code identifier
	*/
	static code = "E_UNAUTHORIZED_ACCESS";
	/**
	* Endpoint to redirect to. Only used by "session" driver
	* renderer
	*/
	redirectTo;
	/**
	* Translation identifier. Can be customized
	*/
	identifier = "errors.E_UNAUTHORIZED_ACCESS";
	/**
	* The guard name reference that raised the exception. It allows
	* us to customize the logic of handling the exception.
	*/
	guardDriverName;
	/**
	* A collection of renderers to render the exception to a
	* response.
	*
	* The collection is a key-value pair, where the key is
	* the guard driver name and value is a factory function
	* to respond to the request.
	*/
	renderers = {
		session: (message, error, ctx) => {
			switch (ctx.request.accepts([
				"html",
				"application/vnd.api+json",
				"json"
			])) {
				case "html":
				case null:
					ctx.session.flashExcept(["_csrf"]);
					ctx.session.flash("error", message);
					/**
					* The "flashErrors" call must be removed in the future
					*/
					ctx.session.flashErrors({ [error.code]: message });
					ctx.response.redirect().withIntendedUrl().withQs().toPath(error.redirectTo || "/");
					break;
				case "json":
					ctx.response.status(error.status).send({ errors: [{ message }] });
					break;
				case "application/vnd.api+json":
					ctx.response.status(error.status).send({ errors: [{
						code: error.code,
						title: message
					}] });
					break;
			}
		},
		basic_auth: (message, _, ctx) => {
			ctx.response.status(this.status).header("WWW-Authenticate", `Basic realm="Authenticate", charset="UTF-8"`).send(message);
		},
		access_tokens: (message, error, ctx) => {
			switch (ctx.request.accepts([
				"html",
				"application/vnd.api+json",
				"json"
			])) {
				case "html":
				case null:
					ctx.response.status(error.status).send(message);
					break;
				case "json":
					ctx.response.status(error.status).send({ errors: [{ message }] });
					break;
				case "application/vnd.api+json":
					ctx.response.status(error.status).send({ errors: [{
						code: error.code,
						title: message
					}] });
					break;
			}
		}
	};
	/**
	* Returns the message to be sent in the HTTP response.
	* Feel free to override this method and return a custom
	* response.
	*
	* @param error - The error instance
	* @param ctx - The HTTP context
	*
	* @example
	* const message = error.getResponseMessage(error, ctx)
	* console.log('Error message:', message)
	*/
	getResponseMessage(error, ctx) {
		if ("i18n" in ctx) return ctx.i18n.t(error.identifier, {}, error.message);
		return error.message;
	}
	/**
	* Creates a new E_UNAUTHORIZED_ACCESS exception
	*
	* @param message - The error message
	* @param options - Options including redirectTo and guardDriverName
	*
	* @example
	* throw new E_UNAUTHORIZED_ACCESS('Access denied', {
	*   guardDriverName: 'session',
	*   redirectTo: '/login'
	* })
	*/
	constructor(message, options) {
		super(message, {});
		this.guardDriverName = options.guardDriverName;
		this.redirectTo = options.redirectTo;
	}
	/**
	* Converts exception to an HTTP response
	*
	* @param error - The error instance
	* @param ctx - The HTTP context
	*
	* @example
	* // This method is called automatically by AdonisJS
	* await error.handle(error, ctx)
	*/
	async handle(error, ctx) {
		const renderer = this.renderers[this.guardDriverName];
		const message = error.getResponseMessage(error, ctx);
		if (!renderer) return ctx.response.status(error.status).send(message);
		return renderer(message, error, ctx);
	}
};
/**
* Exception is raised when user credentials are invalid
*
* @example
* throw new E_INVALID_CREDENTIALS('Invalid email or password')
*/
const E_INVALID_CREDENTIALS = class extends Exception {
	/**
	* HTTP status code for invalid credentials
	*/
	static status = 400;
	/**
	* Error code identifier
	*/
	static code = "E_INVALID_CREDENTIALS";
	/**
	* Translation identifier. Can be customized
	*/
	identifier = "errors.E_INVALID_CREDENTIALS";
	/**
	* Returns the message to be sent in the HTTP response.
	* Feel free to override this method and return a custom
	* response.
	*
	* @param error - The error instance
	* @param ctx - The HTTP context
	*
	* @example
	* const message = error.getResponseMessage(error, ctx)
	* console.log('Error message:', message)
	*/
	getResponseMessage(error, ctx) {
		if ("i18n" in ctx) return ctx.i18n.t(error.identifier, {}, error.message);
		return error.message;
	}
	/**
	* Converts exception to an HTTP response
	*
	* @param error - The error instance
	* @param ctx - The HTTP context
	*
	* @example
	* // This method is called automatically by AdonisJS
	* await error.handle(error, ctx)
	*/
	async handle(error, ctx) {
		const message = this.getResponseMessage(error, ctx);
		switch (ctx.request.accepts([
			"html",
			"application/vnd.api+json",
			"json"
		])) {
			case "html":
			case null:
				if (ctx.session) {
					ctx.session.flashExcept([
						"_csrf",
						"_method",
						"password",
						"password_confirmation"
					]);
					ctx.session.flash("error", message);
					/**
					* The "flashErrors" call must be removed in the future
					*/
					ctx.session.flashErrors({ [error.code]: message });
					ctx.response.redirect("back", true);
				} else ctx.response.status(error.status).send(message);
				break;
			case "json":
				ctx.response.status(error.status).send({ errors: [{ message }] });
				break;
			case "application/vnd.api+json":
				ctx.response.status(error.status).send({ errors: [{
					code: error.code,
					title: message
				}] });
				break;
		}
	}
};
//#endregion
export { __exportAll as i, E_UNAUTHORIZED_ACCESS as n, errors_exports as r, E_INVALID_CREDENTIALS as t };
