

import { ZodIssueCode } from "zod";

//#region src/utils/zod-error.ts
/**
* Some code comes from `zod-validation-error` package (MIT License) and
* was slightly simplified to fit our needs.
*/
const constants = {
	identifierRegex: /[$_\p{ID_Start}][$\u200c\u200d\p{ID_Continue}]*/u,
	unionSeparator: ", or ",
	issueSeparator: "\n	- "
};
function escapeQuotes(str) {
	return str.replace(/"/g, "\\\"");
}
function joinPath(path) {
	if (path.length === 1) return path[0].toString();
	return path.reduce((acc, item) => {
		if (typeof item === "number") return `${acc}[${item.toString()}]`;
		if (item.includes("\"")) return `${acc}["${escapeQuotes(item)}"]`;
		if (!constants.identifierRegex.test(item)) return `${acc}["${item}"]`;
		return acc + (acc.length === 0 ? "" : ".") + item;
	}, "");
}
function getMessageFromZodIssue(issue) {
	if (issue.code === ZodIssueCode.invalid_union) return getMessageFromUnionErrors(issue.unionErrors);
	if (issue.code === ZodIssueCode.invalid_arguments) return [issue.message, ...issue.argumentsError.issues.map((issue$1) => getMessageFromZodIssue(issue$1))].join(constants.issueSeparator);
	if (issue.code === ZodIssueCode.invalid_return_type) return [issue.message, ...issue.returnTypeError.issues.map((issue$1) => getMessageFromZodIssue(issue$1))].join(constants.issueSeparator);
	if (issue.path.length !== 0) {
		if (issue.path.length === 1) {
			const identifier = issue.path[0];
			if (typeof identifier === "number") return `${issue.message} at index ${identifier}`;
		}
		return `${issue.message} at "${joinPath(issue.path)}"`;
	}
	return issue.message;
}
function getMessageFromUnionErrors(unionErrors) {
	return unionErrors.reduce((acc, zodError) => {
		const newIssues = zodError.issues.map((issue) => getMessageFromZodIssue(issue)).join(constants.issueSeparator);
		if (!acc.includes(newIssues)) acc.push(newIssues);
		return acc;
	}, []).join(constants.unionSeparator);
}
function formatZodError(error) {
	if (!error) return "";
	return `\t- ${error?.issues.map((issue) => getMessageFromZodIssue(issue)).join(constants.issueSeparator)}`;
}

//#endregion
export { formatZodError };
//# sourceMappingURL=zod-error.mjs.map