/*
 * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
 * @generated-id: 0b8a72dd07c0
 */

import * as z from "zod/v4";
import { remap as remap$ } from "../../lib/primitives.js";
import { safeParse } from "../../lib/schemas.js";
import { Result as SafeParseResult } from "../../types/fp.js";
import { SDKValidationError } from "../errors/sdkvalidationerror.js";
import {
  OCRConfidenceScore,
  OCRConfidenceScore$inboundSchema,
} from "./ocrconfidencescore.js";

/**
 * Confidence scores for an OCR page at various granularities.
 *
 * @remarks
 *
 * Note on page-level stats:
 * - For 'page' granularity: average/minimum are computed from per-token exp(logprob).
 * - For 'word' granularity: average/minimum are computed from per-word confidence,
 *   where each word's confidence is exp(mean(token_logprobs)) — a geometric mean
 *   over the word's subword tokens.
 */
export type OCRPageConfidenceScores = {
  /**
   * Word-level confidence scores (populated only for 'word' granularity)
   */
  wordConfidenceScores?: Array<OCRConfidenceScore> | undefined;
  /**
   * Average confidence score for the page
   */
  averagePageConfidenceScore: number;
  /**
   * Minimum confidence score for the page
   */
  minimumPageConfidenceScore: number;
};

/** @internal */
export const OCRPageConfidenceScores$inboundSchema: z.ZodType<
  OCRPageConfidenceScores,
  unknown
> = z.object({
  word_confidence_scores: z.array(OCRConfidenceScore$inboundSchema).optional(),
  average_page_confidence_score: z.number(),
  minimum_page_confidence_score: z.number(),
}).transform((v) => {
  return remap$(v, {
    "word_confidence_scores": "wordConfidenceScores",
    "average_page_confidence_score": "averagePageConfidenceScore",
    "minimum_page_confidence_score": "minimumPageConfidenceScore",
  });
});

export function ocrPageConfidenceScoresFromJSON(
  jsonString: string,
): SafeParseResult<OCRPageConfidenceScores, SDKValidationError> {
  return safeParse(
    jsonString,
    (x) => OCRPageConfidenceScores$inboundSchema.parse(JSON.parse(x)),
    `Failed to parse 'OCRPageConfidenceScores' from JSON`,
  );
}
