import { ParseResult } from "effect";
function isDiscriminatorMiss(issue) {
  const last = issue.path[issue.path.length - 1];
  return (last === "type" || last === "compatibilityDate") && issue.message.startsWith('Expected "');
}
function blockIndexOf(path) {
  if (path[0] === "slot" && path[1] === "blocks" && typeof path[2] === "number") return path[2];
  return null;
}
function fallback(err) {
  if (err instanceof Error) return err.message;
  try {
    return JSON.stringify(err);
  } catch {
    return String(err);
  }
}
export function formatValidationError(err, blockLabelAt) {
  if (!ParseResult.isParseError(err)) return fallback(err);
  try {
    const issues = ParseResult.ArrayFormatter.formatErrorSync(err);
    const real = issues.filter((i) => !isDiscriminatorMiss(i));
    const picked = real.length > 0 ? real : issues;
    const first = picked[0];
    if (!first) return fallback(err);
    const index = blockIndexOf(first.path);
    const label = index === null ? void 0 : blockLabelAt(index);
    const prefix = label ? `\u300C${label}\u300D\uFF1A` : "";
    const suffix = picked.length > 1 ? `\uFF08\u8FD8\u6709 ${picked.length - 1} \u4E2A\u95EE\u9898\uFF09` : "";
    return `${prefix}${first.message}${suffix}`;
  } catch {
    return fallback(err);
  }
}
