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

import * as z from "zod/v4";
import { safeParse } from "../../lib/schemas.js";
import * as discriminatedUnionTypes from "../../types/discriminatedUnion.js";
import { discriminatedUnion } from "../../types/discriminatedUnion.js";
import { Result as SafeParseResult } from "../../types/fp.js";
import { SDKValidationError } from "../errors/sdkvalidationerror.js";
import { JSONPatchAdd, JSONPatchAdd$inboundSchema } from "./jsonpatchadd.js";
import {
  JSONPatchAppend,
  JSONPatchAppend$inboundSchema,
} from "./jsonpatchappend.js";
import {
  JSONPatchRemove,
  JSONPatchRemove$inboundSchema,
} from "./jsonpatchremove.js";
import {
  JSONPatchReplace,
  JSONPatchReplace$inboundSchema,
} from "./jsonpatchreplace.js";

export type JSONPatch =
  | JSONPatchAdd
  | JSONPatchAppend
  | JSONPatchRemove
  | JSONPatchReplace
  | discriminatedUnionTypes.Unknown<"op">;

/** @internal */
export const JSONPatch$inboundSchema: z.ZodType<JSONPatch, unknown> =
  discriminatedUnion("op", {
    add: JSONPatchAdd$inboundSchema,
    append: JSONPatchAppend$inboundSchema,
    remove: JSONPatchRemove$inboundSchema,
    replace: JSONPatchReplace$inboundSchema,
  });

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