const { ArrayIsArray: _p_ArrayIsArray } = require('../../../../primordials/array.js')
const { BufferIsBuffer: _p_BufferIsBuffer } = require('../../../../primordials/buffer.js')

const { TypeErrorCtor: _p_TypeErrorCtor } = require('../../../../primordials/error.js')

const { JSONParse: _p_JSONParse } = require('../../../../primordials/json.js')

const { ObjectAssign: _p_ObjectAssign } = require('../../../../primordials/object.js')

const { StringPrototypeCharCodeAt: _p_StringPrototypeCharCodeAt } = require('../../../../primordials/string.js')

"use strict";
/**
 * Bundled from @npmcli/package-json/lib/read-package.js
 * This is a zero-dependency bundle created by rolldown.
 */
'use strict';

//#region \0rolldown/runtime.js
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", {
	value,
	configurable: true
});
var __commonJSMin = (cb, mod) => () => (mod || (cb((mod = { exports: {} }).exports, mod), cb = null), mod.exports);

//#endregion

//#region node_modules/.pnpm/json-parse-even-better-errors@5.0.0/node_modules/json-parse-even-better-errors/lib/index.js
var require_lib = /* @__PURE__ */ __commonJSMin(((exports, module) => {
	const INDENT = Symbol.for("indent");
	const NEWLINE = Symbol.for("newline");
	const DEFAULT_NEWLINE = "\n";
	const DEFAULT_INDENT = "  ";
	const BOM = /^\uFEFF/;
	const FORMAT = /^\s*[{[]((?:\r?\n)+)([\s\t]*)/;
	const EMPTY = /^(?:\{\}|\[\])((?:\r?\n)+)?$/;
	const UNEXPECTED_TOKEN = /^Unexpected token '?(.)'?(,)? /i;
	const hexify = (char) => {
		const h = _p_StringPrototypeCharCodeAt(char, 0).toString(16).toUpperCase();
		return `0x${h.length % 2 ? "0" : ""}${h}`;
	};
	const stripBOM = (txt) => String(txt).replace(BOM, "");
	const makeParsedError = (msg, parsing, position = 0) => ({
		message: `${msg} while parsing ${parsing}`,
		position
	});
	const parseError = (e, txt, context = 20) => {
		let msg = e.message;
		if (!txt) return makeParsedError(msg, "empty string");
		const badTokenMatch = msg.match(UNEXPECTED_TOKEN);
		const badIndexMatch = msg.match(/ position\s+(\d+)/i);
		if (badTokenMatch) msg = msg.replace(UNEXPECTED_TOKEN, `Unexpected token ${JSON.stringify(badTokenMatch[1])} (${hexify(badTokenMatch[1])})$2 `);
		let errIdx;
		if (badIndexMatch) errIdx = +badIndexMatch[1];
		else if (msg.match(/^Unexpected end of JSON.*/i)) errIdx = txt.length - 1;
		if (errIdx == null) return makeParsedError(msg, `'${txt.slice(0, context * 2)}'`);
		const start = errIdx <= context ? 0 : errIdx - context;
		const end = errIdx + context >= txt.length ? txt.length : errIdx + context;
		const slice = `${start ? "..." : ""}${txt.slice(start, end)}${end === txt.length ? "" : "..."}`;
		return makeParsedError(msg, `${txt === slice ? "" : "near "}${JSON.stringify(slice)}`, errIdx);
	};
	var JSONParseError = class extends SyntaxError {
		constructor(er, txt, context, caller) {
			const metadata = parseError(er, txt, context);
			super(metadata.message);
			_p_ObjectAssign(this, metadata);
			this.code = "EJSONPARSE";
			this.systemError = er;
			Error.captureStackTrace(this, caller || this.constructor);
		}
		get name() {
			return this.constructor.name;
		}
		set name(n) {}
		get [Symbol.toStringTag]() {
			return this.constructor.name;
		}
	};
	const parseJson = (txt, reviver) => {
		const result = _p_JSONParse(txt, reviver);
		if (result && typeof result === "object") {
			const match = txt.match(EMPTY) || txt.match(FORMAT) || [
				null,
				"",
				""
			];
			result[NEWLINE] = match[1] ?? DEFAULT_NEWLINE;
			result[INDENT] = match[2] ?? DEFAULT_INDENT;
		}
		return result;
	};
	const parseJsonError = (raw, reviver, context) => {
		const txt = stripBOM(raw);
		try {
			return parseJson(txt, reviver);
		} catch (e) {
			if (typeof raw !== "string" && !_p_BufferIsBuffer(raw)) {
				const msg = _p_ArrayIsArray(raw) && raw.length === 0 ? "an empty array" : String(raw);
				throw _p_ObjectAssign(/* @__PURE__ */ new _p_TypeErrorCtor(`Cannot parse ${msg}`), {
					code: "EJSONPARSE",
					systemError: e
				});
			}
			throw new JSONParseError(e, txt, context, parseJsonError);
		}
	};
	module.exports = parseJsonError;
	parseJsonError.JSONParseError = JSONParseError;
	parseJsonError.noExceptions = (raw, reviver) => {
		try {
			return parseJson(stripBOM(raw), reviver);
		} catch {}
	};
}));

//#endregion
//#region node_modules/.pnpm/@npmcli+package-json@7.0.0/node_modules/@npmcli/package-json/lib/read-package.js
var require_read_package = /* @__PURE__ */ __commonJSMin(((exports, module) => {
	const { readFile } = require("fs/promises");
	const parseJSON = require_lib();
	async function read(filename) {
		try {
			return await readFile(filename, "utf8");
		} catch (err) {
			err.message = `Could not read package.json: ${err}`;
			throw err;
		}
	}
	function parse(data) {
		try {
			return parseJSON(data);
		} catch (err) {
			err.message = `Invalid package.json: ${err}`;
			throw err;
		}
	}
	async function readPackage(filename) {
		return parse(await read(filename));
	}
	module.exports = {
		read,
		parse,
		readPackage
	};
}));

//#endregion
//#region src/external/@npmcli/package-json/lib/read-package.js
module.exports = require_read_package();

//#endregion