export interface ConfigValidationIssue {
  code: string;
  message: string;
  path: string;
  targetId?: string;
  targetName?: string;
}

export function formatConfigIssues(issues: ConfigValidationIssue[]): string {
  return issues.map(formatConfigIssue).join("\n");
}

export function issue(
  code: string,
  path: string,
  message: string,
  targetName?: string,
  targetId?: string,
): ConfigValidationIssue {
  return {
    code,
    message,
    path,
    ...(targetName === undefined ? {} : { targetName }),
    ...(targetId === undefined ? {} : { targetId }),
  };
}

export function joinPath(base: string, key: string): string {
  if (base === "") return key;
  if (key.startsWith("[")) return `${base}${key}`;
  return `${base}.${key}`;
}

export function renderPath(path: string): string {
  return path === "" ? "配置文件" : path;
}

export function throwConfigIssues(issues: ConfigValidationIssue[]): never {
  throw new Error(formatConfigIssues(issues));
}

function formatConfigIssue(issue: ConfigValidationIssue): string {
  if (issue.targetId) {
    const path = issue.path.replace(/^targets\[\d+\]\.?/, "");
    const renderedPath = path === "" ? "配置" : path;
    const label =
      issue.targetName && issue.targetName !== issue.targetId
        ? `target "${issue.targetName}" (id: "${issue.targetId}")`
        : `target id "${issue.targetId}"`;
    return `${label} 的 ${renderedPath} ${issue.message}`;
  }
  if (issue.targetName) {
    const path = issue.path.replace(/^targets\[\d+\]\.?/, "");
    const renderedPath = path === "" ? "配置" : path;
    return `target "${issue.targetName}" 的 ${renderedPath} ${issue.message}`;
  }
  return `${renderPath(issue.path)} ${issue.message}`;
}
