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

import * as z from "zod/v3";
import { remap as remap$ } from "../../lib/primitives.js";
import {
  collectExtraKeys as collectExtraKeys$,
  safeParse,
} from "../../lib/schemas.js";
import { Result as SafeParseResult } from "../../types/fp.js";
import { SDKValidationError } from "../errors/sdkvalidationerror.js";
import { Annotations, Annotations$inboundSchema } from "./annotations.js";
import {
  BlobResourceContents,
  BlobResourceContents$inboundSchema,
} from "./blobresourcecontents.js";
import {
  TextResourceContents,
  TextResourceContents$inboundSchema,
} from "./textresourcecontents.js";

export type Resource = TextResourceContents | BlobResourceContents;

/**
 * The contents of a resource, embedded into a prompt or tool call result.
 *
 * @remarks
 *
 * It is up to the client how best to render embedded resources for the benefit
 * of the LLM and/or the user.
 */
export type EmbeddedResource = {
  type?: "resource" | undefined;
  resource: TextResourceContents | BlobResourceContents;
  annotations?: Annotations | null | undefined;
  meta?: { [k: string]: any } | null | undefined;
  additionalProperties?: { [k: string]: any } | undefined;
};

/** @internal */
export const Resource$inboundSchema: z.ZodType<
  Resource,
  z.ZodTypeDef,
  unknown
> = z.union([
  TextResourceContents$inboundSchema,
  BlobResourceContents$inboundSchema,
]);

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

/** @internal */
export const EmbeddedResource$inboundSchema: z.ZodType<
  EmbeddedResource,
  z.ZodTypeDef,
  unknown
> = collectExtraKeys$(
  z.object({
    type: z.literal("resource").default("resource").optional(),
    resource: z.union([
      TextResourceContents$inboundSchema,
      BlobResourceContents$inboundSchema,
    ]),
    annotations: z.nullable(Annotations$inboundSchema).optional(),
    _meta: z.nullable(z.record(z.any())).optional(),
  }).catchall(z.any()),
  "additionalProperties",
  true,
).transform((v) => {
  return remap$(v, {
    "_meta": "meta",
  });
});

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