
//#region ../@intlayer/config/dist/esm/utils/extractErrorMessage.mjs
const extractErrorMessage = (error) => {
	const trimToSingleLine = (text) => text.split(/\r?\n/).map((s) => s.trim()).filter(Boolean)[0] ?? text.trim();
	const looksLikeJson = (value) => {
		const s = value.trim();
		if (!s) return false;
		const first = s[0];
		return first === "{" || first === "[" || first === "\"";
	};
	const sanitizeUnexpectedTokenMessage = (text) => {
		const t = text.trim();
		if (/Unexpected token/i.test(t) && /not valid JSON/i.test(t)) {
			const quoted = t.match(/"([^"]+)"/);
			if (quoted?.[1]) return quoted[1];
			const afterColon = t.split(":").slice(1).join(":").trim();
			if (afterColon) return afterColon;
		}
		return t;
	};
	const pickFieldsFromObject = (value, seen) => {
		if (!value || typeof value !== "object") return void 0;
		if (seen.has(value)) return void 0;
		seen.add(value);
		const obj = value;
		if (typeof obj.message === "string" && obj.message.trim()) return obj.message;
		if (typeof obj.error_description === "string" && obj.error_description.trim()) return obj.error_description;
		if (typeof obj.error === "string" && obj.error.trim()) return obj.error;
		const title = typeof obj.title === "string" ? obj.title.trim() : "";
		const code = typeof obj.code === "string" ? obj.code.trim() : "";
		if (title && code) return `${title} (${code})`;
		if (title) return title;
		if (code) return code;
		if (typeof obj.statusText === "string" && obj.statusText.trim()) return obj.statusText;
		const response = obj.response;
		if (response && typeof response === "object") {
			const data$1 = response.data;
			const fromData$1 = pickFieldsFromObject(data$1, seen);
			if (fromData$1) return fromData$1;
		}
		const data = obj.data;
		const fromData = pickFieldsFromObject(data, seen);
		if (fromData) return fromData;
		const cause = obj.cause;
		const fromCause = pickFieldsFromObject(cause, seen) ?? (typeof cause?.message === "string" ? cause.message : void 0);
		if (fromCause) return fromCause;
		const errors = obj.errors;
		if (Array.isArray(errors)) for (const item of errors) {
			const fromItem = pickFieldsFromObject(item, seen);
			if (fromItem) return fromItem;
			if (typeof item?.message === "string") return item.message;
		}
	};
	const tryParseJsonString = (maybeJson) => {
		if (!looksLikeJson(maybeJson)) return void 0;
		try {
			const parsed = JSON.parse(maybeJson);
			const picked = pickFieldsFromObject(parsed, /* @__PURE__ */ new Set());
			if (picked) return picked;
			if (typeof parsed === "string") return parsed;
			return;
		} catch {
			return;
		}
	};
	if (typeof error === "string") {
		const cleaned = sanitizeUnexpectedTokenMessage(error);
		return tryParseJsonString(cleaned) ?? trimToSingleLine(cleaned);
	}
	if (error && typeof error === "object") {
		if (error instanceof Error) {
			const cleaned = sanitizeUnexpectedTokenMessage(error.message);
			const fromMessage = tryParseJsonString(cleaned);
			if (fromMessage) return trimToSingleLine(fromMessage);
			const fromCause = extractErrorMessage(error.cause);
			if (fromCause && fromCause !== "An unknown error occurred") return trimToSingleLine(fromCause);
			return trimToSingleLine(cleaned);
		}
		const fromObject = pickFieldsFromObject(error, /* @__PURE__ */ new Set());
		if (fromObject) {
			const cleaned = sanitizeUnexpectedTokenMessage(fromObject);
			return tryParseJsonString(cleaned) ?? trimToSingleLine(cleaned);
		}
		try {
			return trimToSingleLine(JSON.stringify(error));
		} catch {
			return trimToSingleLine(String(error));
		}
	}
	return "An unknown error occurred";
};

//#endregion
exports.extractErrorMessage = extractErrorMessage;
//# sourceMappingURL=extractErrorMessage.cjs.map