import { t as __exportAll } from "./rolldown-runtime-BBjsoOtd.js";
import { inspect } from "node:util";
import { Exception } from "@poppinss/utils/exception";
import useColors from "@poppinss/colors";
import { dump } from "@poppinss/dumper/console";
import { createScript, createStyleSheet, dump as dump$1 } from "@poppinss/dumper/html";
import { parse } from "error-stack-parser-es";
//#region modules/dumper/errors.ts
var errors_exports = /* @__PURE__ */ __exportAll({ E_DUMP_DIE_EXCEPTION: () => E_DUMP_DIE_EXCEPTION });
/**
* DumpDie exception raised by the "dd" (dump and die) function.
* This special exception terminates execution while dumping the provided
* value as HTML (during HTTP requests) or ANSI (in console/CLI).
*
* @example
* ```ts
* // This will dump the user object and terminate
* dumper.dd(user)
*
* // In HTTP context: sends HTML dump to browser
* // In CLI context: prints ANSI dump to console
* ```
*/
var DumpDieException = class extends Exception {
	static status = 500;
	static code = "E_DUMP_DIE_EXCEPTION";
	#dumper;
	#traceSourceIndex = 1;
	value;
	constructor(value, dumper) {
		super("Dump and Die exception");
		this.#dumper = dumper;
		this.value = value;
	}
	/**
	* Returns the source file and line number location for the error
	*/
	#getErrorSource() {
		if (this.fileName && this.lineNumber) return {
			location: this.fileName,
			line: this.lineNumber
		};
		const source = parse(this)[this.#traceSourceIndex];
		if (!source.fileName || !source.lineNumber) return;
		return {
			location: source.fileName,
			line: source.lineNumber
		};
	}
	/**
	* Set the stack trace index for determining the source location.
	* This is useful when building nested helpers on top of dump/die functionality.
	*
	* @param index - Stack trace index (0 = current function, 1 = caller, etc.)
	*/
	setTraceSourceIndex(index) {
		this.#traceSourceIndex = index;
		return this;
	}
	/**
	* Preventing itself from getting reported by the
	* AdonisJS exception reporter
	*/
	report() {}
	/**
	* HTTP exception handler that renders the dump as HTML output.
	* This method is called automatically by AdonisJS when a DumpDieException
	* is thrown during an HTTP request.
	*
	* @param error - The DumpDieException instance
	* @param ctx - HTTP context for the current request
	*/
	async handle(error, ctx) {
		const source = this.#getErrorSource();
		/**
		* Comes from the shield package
		*/
		const cspNonce = "nonce" in ctx.response ? ctx.response.nonce : void 0;
		ctx.response.status(500).send(`<!DOCTYPE html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width">${this.#dumper.getHeadElements(cspNonce)}</head><body>${this.#dumper.dumpToHtml(error.value, {
			cspNonce,
			source,
			title: "DUMP DIE"
		})}</body></html>`);
	}
	/**
	* Ace command exception handler that renders the dump as ANSI output.
	* This method is called automatically by the Ace kernel when a DumpDieException
	* is thrown during command execution.
	*
	* @param error - The DumpDieException instance
	* @param kernel - Ace kernel instance
	*/
	async render(error, kernel) {
		const source = this.#getErrorSource();
		kernel.ui.logger.log(this.#dumper.dumpToAnsi(error.value, {
			source,
			title: "DUMP DIE"
		}));
	}
	/**
	* Custom output for the Node.js util inspect
	*/
	[inspect.custom]() {
		const source = this.#getErrorSource();
		return this.#dumper.dumpToAnsi(this.value, {
			source,
			title: "DUMP DIE"
		});
	}
};
const E_DUMP_DIE_EXCEPTION = DumpDieException;
//#endregion
//#region modules/dumper/dumper.ts
const colors = useColors.ansi();
const DUMP_TITLE_STYLES = `
.adonisjs-dump-header {
  font-family: JetBrains Mono, monaspace argon, Menlo, Monaco, Consolas, monospace;
  background: #ff1639;
  border-radius: 4px;
  color: #fff;
  border-bottom-left-radius: 0;
  border-bottom-right-radius: 0;
  padding: 0.4rem 1.2rem;
  font-size: 1em;
  display: flex;
  justify-content: space-between;
}
.adonisjs-dump-header .adonisjs-dump-header-title {
  font-weight: bold;
  text-transform: uppercase;
}
.adonisjs-dump-header .adonisjs-dump-header-source {
  font-weight: bold;
  color: inherit;
  text-decoration: underline;
}
.dumper-dump pre {
  border-radius: 4px;
  border-top-left-radius: 0;
  border-top-right-radius: 0;
}`;
const IDE = process.env.ADONIS_IDE ?? process.env.EDITOR ?? "";
/**
* Dumper exposes the API to dump or die/dump values in your
* AdonisJS application. An singleton instance of the Dumper
* is shared as a service and may use it follows.
*
* ```ts
* const dumper = container.make('dumper')
*
* dumper.configureHtmlOutput({
*   // parser + html formatter config
* })
*
* dumper.configureAnsiOutput({
*   // parser + console formatter config
* })
*
* const html = dumper.dumpToHtml(value)
* const ansi = dumper.dumpToAnsi(value)
*
* // Returns style and script tags that must be
* // injeted to the head of the HTML document
*
* const head = dumper.getHeadElements()
* ```
*/
var Dumper = class {
	#app;
	/**
	* Configuration for the HTML formatter
	*/
	#htmlConfig = {};
	/**
	* Configuration for the Console formatter
	*/
	#consoleConfig = { collapse: ["DateTime", "Date"] };
	/**
	* A collections of known editors to create URLs to open
	* them
	*/
	#editors = {
		textmate: "txmt://open?url=file://%f&line=%l",
		macvim: "mvim://open?url=file://%f&line=%l",
		emacs: "emacs://open?url=file://%f&line=%l",
		sublime: "subl://open?url=file://%f&line=%l",
		phpstorm: "phpstorm://open?file=%f&line=%l",
		atom: "atom://core/open/file?filename=%f&line=%l",
		vscode: "vscode://file/%f:%l"
	};
	/**
	* Creates a new Dumper instance
	*
	* @param app - The AdonisJS application instance
	*/
	constructor(app) {
		this.#app = app;
	}
	/**
	* Returns the link to open the file using dd inside one
	* of the known code editors. Constructs a URL that can be used
	* to open the file at a specific line in supported editors.
	*
	* @param source - Optional source file information
	* @param source.location - The file path to open
	* @param source.line - The line number to jump to
	*/
	#getEditorLink(source) {
		const editorURL = this.#editors[IDE] || IDE;
		if (!editorURL || !source) return;
		return {
			href: editorURL.replace("%f", source.location).replace("%l", String(source.line)),
			text: `${this.#app.relativePath(source.location)}:${source.line}`
		};
	}
	/**
	* Configure the HTML formatter output options
	*
	* @param config - Configuration options for HTML dump formatting
	*
	* @example
	* ```ts
	* dumper.configureHtmlOutput({
	*   showHidden: true,
	*   depth: 5,
	*   colors: false
	* })
	* ```
	*/
	configureHtmlOutput(config) {
		this.#htmlConfig = config;
		return this;
	}
	/**
	* Configure the ANSI formatter output options for console display
	*
	* @param config - Configuration options for ANSI console formatting
	*
	* @example
	* ```ts
	* dumper.configureAnsiOutput({
	*   showHidden: true,
	*   depth: 3,
	*   collapse: ['Date', 'DateTime']
	* })
	* ```
	*/
	configureAnsiOutput(config) {
		this.#consoleConfig = config;
		return this;
	}
	/**
	* Returns the style and script elements that need to be injected into
	* the HTML document head for proper dump visualization
	*
	* @param cspNonce - Optional Content Security Policy nonce for inline scripts
	*
	* @example
	* ```ts
	* const headElements = dumper.getHeadElements('abc123')
	* // Insert into your HTML head section
	* ```
	*/
	getHeadElements(cspNonce) {
		return `<style id="dumper-styles">` + createStyleSheet() + DUMP_TITLE_STYLES + `</style><script id="dumper-script"${cspNonce ? ` nonce="${cspNonce}"` : ""}>` + createScript() + "<\/script>";
	}
	/**
	* Dump a value to formatted HTML output
	*
	* @param value - The value to dump and inspect
	* @param options - Options for HTML output formatting
	* @param options.cspNonce - Optional Content Security Policy nonce
	* @param options.title - Optional title to display in the dump header
	* @param options.source - Optional source file information for editor links
	*
	* @example
	* ```ts
	* const htmlOutput = dumper.dumpToHtml(user, {
	*   title: 'User Object',
	*   source: { location: '/app/controllers/user.ts', line: 42 }
	* })
	* ```
	*/
	dumpToHtml(value, options = {}) {
		const link = this.#getEditorLink(options.source) ?? null;
		return `<div class="adonisjs-dump-header"><span class="adonisjs-dump-header-title">${options.title || "DUMP"}</span>` + (link ? `<a href="${link.href}" class="adonisjs-dump-header-source">${link.text}</a>` : "") + "</div>" + dump$1(value, {
			cspNonce: options.cspNonce,
			...this.#htmlConfig
		});
	}
	/**
	* Dump a value to formatted ANSI output for console display
	*
	* @param value - The value to dump and inspect
	* @param options - Options for ANSI output formatting
	* @param options.title - Optional title to display in the dump header
	* @param options.source - Optional source file information for editor links
	*
	* @example
	* ```ts
	* const ansiOutput = dumper.dumpToAnsi(user, {
	*   title: 'User Debug',
	*   source: { location: '/app/controllers/user.ts', line: 42 }
	* })
	* console.log(ansiOutput)
	* ```
	*/
	dumpToAnsi(value, options = {}) {
		const columns = process.stdout.columns;
		/**
		* Link to the source file
		*/
		const link = `${this.#getEditorLink(options.source)?.text ?? ""} `;
		/**
		* Dump title
		*/
		const title = ` ${options.title || "DUMP"}`;
		/**
		* Whitespace between the title and the link to align them
		* on each side of x axis
		*/
		const whiteSpaceLength = columns ? columns - link.length - title.length - 4 : 2;
		const whiteSpace = new Array(whiteSpaceLength <= 0 ? 2 : whiteSpaceLength).join(" ");
		return `${colors.bgRed().bold(`${title}${whiteSpace}${link}`)}\n${dump(value, this.#consoleConfig)}`;
	}
	/**
	* Dump values and die. This method dumps the provided value and then
	* terminates the application. The output format is automatically chosen
	* based on the execution context.
	*
	* - During an HTTP request, HTML output will be sent to the browser
	* - Otherwise the value will be logged to the console in ANSI format
	*
	* @param value - The value to dump before terminating
	* @param traceSourceIndex - Stack trace index for source location (default: 1)
	*
	* @example
	* ```ts
	* // This will dump the user object and terminate the application
	* dumper.dd(user)
	*
	* // This will never execute
	* console.log('This line will not run')
	* ```
	*/
	dd(value, traceSourceIndex = 1) {
		const error = new E_DUMP_DIE_EXCEPTION(value, this);
		error.setTraceSourceIndex(traceSourceIndex);
		throw error;
	}
};
//#endregion
export { errors_exports as n, Dumper as t };
