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

import * as z from "zod/v4-mini";
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";

export type TokenResponse = {
  accessToken: string;
  tokenType: "Bearer";
  expiresIn: number;
  refreshToken?: string | null | undefined;
  scope: string;
  idToken?: string | null | undefined;
};

/** @internal */
export const TokenResponse$inboundSchema: z.ZodMiniType<
  TokenResponse,
  unknown
> = z.pipe(
  z.object({
    access_token: z.string(),
    token_type: z.literal("Bearer"),
    expires_in: z.int(),
    refresh_token: z.optional(z.nullable(z.string())),
    scope: z.string(),
    id_token: z.optional(z.nullable(z.string())),
  }),
  z.transform((v) => {
    return remap$(v, {
      "access_token": "accessToken",
      "token_type": "tokenType",
      "expires_in": "expiresIn",
      "refresh_token": "refreshToken",
      "id_token": "idToken",
    });
  }),
);

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