"use strict";
/* Socket Lib - Built with rolldown */
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const require_runtime = require('../_virtual/_rolldown/runtime.js');
const require_primordials_string = require('../primordials/string.js');
const require_env_debug = require('../env/debug.js');
const require_env_socket = require('../env/socket.js');
const require_debug__internal = require('./_internal.js');
let src_external_debug = require("../external/debug");
src_external_debug = require_runtime.__toESM(src_external_debug);

//#region src/debug/namespace.ts
/**
* @file Namespace-handling helpers — `extractOptions` normalises the
*   polymorphic first argument that every `*Ns` function takes,
*   `getDebugJsInstance` per-namespace caches the underlying `debug-js`
*   instance with `customLog` patched in, and `isEnabled` / `isDebug` /
*   `isDebugNs` are the gate predicates every output function consults before
*   writing.
*/
/**
* Extract options from namespaces parameter.
*
* @private
*/
/*@__NO_SIDE_EFFECTS__*/
function extractOptions(namespaces) {
	return namespaces !== null && typeof namespaces === "object" ? {
		__proto__: null,
		...namespaces
	} : {
		__proto__: null,
		namespaces
	};
}
/**
* Get or create a debug instance for a namespace.
*
* @private
*/
/*@__NO_SIDE_EFFECTS__*/
function getDebugJsInstance(namespace) {
	let inst = require_debug__internal.debugByNamespace.get(namespace);
	/* c8 ignore start */
	if (inst) return inst;
	if (!/* @__PURE__ */ require_env_debug.getDebug() && /* @__PURE__ */ require_env_socket.getSocketDebug() && (namespace === "error" || namespace === "notice")) src_external_debug.default.enable(namespace);
	/* c8 ignore stop */
	/* c8 ignore next - External debug library call */
	inst = (0, src_external_debug.default)(namespace);
	inst.log = require_debug__internal.customLog;
	require_debug__internal.debugByNamespace.set(namespace, inst);
	return inst;
}
/**
* Check if debug mode is enabled.
*/
/*@__NO_SIDE_EFFECTS__*/
function isDebug() {
	return !!/* @__PURE__ */ require_env_socket.getSocketDebug();
}
/**
* Check if debug mode is enabled.
*/
/*@__NO_SIDE_EFFECTS__*/
function isDebugNs(namespaces) {
	return !!/* @__PURE__ */ require_env_socket.getSocketDebug() && /* @__PURE__ */ isEnabled(namespaces);
}
/**
* Check if debug is enabled for given namespaces.
*
* @private
*/
/*@__NO_SIDE_EFFECTS__*/
function isEnabled(namespaces) {
	if (!/* @__PURE__ */ require_env_socket.getSocketDebug()) return false;
	if (typeof namespaces !== "string" || !namespaces || namespaces === "*") return true;
	const split = namespaces.trim().replace(/\s+/g, ",").split(",").filter(Boolean);
	const names = [];
	const skips = [];
	for (const ns of split) if (require_primordials_string.StringPrototypeStartsWith(ns, "-")) skips.push(ns.slice(1));
	else names.push(ns);
	if (names.length && !names.some((ns) => (/* @__PURE__ */ getDebugJsInstance(ns)).enabled)) return false;
	return skips.every((ns) => !(/* @__PURE__ */ getDebugJsInstance(ns)).enabled);
}

//#endregion
exports.extractOptions = extractOptions;
exports.getDebugJsInstance = getDebugJsInstance;
exports.isDebug = isDebug;
exports.isDebugNs = isDebugNs;
exports.isEnabled = isEnabled;