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

import * as z from "zod";
import { remap as remap$ } from "../../lib/primitives.js";
import { safeParse } from "../../lib/schemas.js";
import { ClosedEnum } from "../../types/enums.js";
import { Result as SafeParseResult } from "../../types/fp.js";
import { SDKValidationError } from "../errors/sdkvalidationerror.js";

/**
 * The type of token, always Bearer for tokens acquired via the OAuth 2.0 flow.
 */
export const TokenType = {
  Bearer: "Bearer",
} as const;
/**
 * The type of token, always Bearer for tokens acquired via the OAuth 2.0 flow.
 */
export type TokenType = ClosedEnum<typeof TokenType>;

/**
 * The issuer of the token. Always attio.com
 */
export const Iss = {
  AttioCom: "attio.com",
} as const;
/**
 * The issuer of the token. Always attio.com
 */
export type Iss = ClosedEnum<typeof Iss>;

export type Bearer = {
  /**
   * Whether the token is currently active and usable.
   */
  active: boolean;
  /**
   * A space-separated list of scopes associated with this token
   */
  scope: string;
  /**
   * The app ID of the OAuth application that requested this token
   */
  clientId: string;
  /**
   * The type of token, always Bearer for tokens acquired via the OAuth 2.0 flow.
   */
  tokenType: TokenType;
  /**
   * The time at which this token will expire, if set, as a number of seconds since January 1 1970 UTC.
   */
  exp: number | null;
  /**
   * The time at which this token was issued, as a number of seconds since January 1 1970 UTC.
   */
  iat: number;
  /**
   * Since Bearer tokens grant Workspace-level permissions, this property contains the workspace_id.
   */
  sub: string;
  /**
   * The intended audience for this token, for Bearer tokens this is the same as the client_id.
   */
  aud: string;
  /**
   * The issuer of the token. Always attio.com
   */
  iss: Iss;
  /**
   * The ID of the workspace member who authorised this token initially, if known
   */
  authorizedByWorkspaceMemberId: string | null;
  /**
   * The ID of the workspace the token is scoped to.
   */
  workspaceId: string;
  /**
   * The name of the workspace the token is scoped to.
   */
  workspaceName: string;
  /**
   * The slug of the workspace the token is scoped to.
   */
  workspaceSlug: string;
  /**
   * The logo URL of the workspace the token is scoped to.
   */
  workspaceLogoUrl: string | null;
};

export type ResponseBody = {
  active: boolean;
};

/**
 * Success
 */
export type GetV2SelfResponse = ResponseBody | Bearer;

/** @internal */
export const TokenType$inboundSchema: z.ZodNativeEnum<typeof TokenType> = z
  .nativeEnum(TokenType);

/** @internal */
export const TokenType$outboundSchema: z.ZodNativeEnum<typeof TokenType> =
  TokenType$inboundSchema;

/**
 * @internal
 * @deprecated This namespace will be removed in future versions. Use schemas and types that are exported directly from this module.
 */
export namespace TokenType$ {
  /** @deprecated use `TokenType$inboundSchema` instead. */
  export const inboundSchema = TokenType$inboundSchema;
  /** @deprecated use `TokenType$outboundSchema` instead. */
  export const outboundSchema = TokenType$outboundSchema;
}

/** @internal */
export const Iss$inboundSchema: z.ZodNativeEnum<typeof Iss> = z.nativeEnum(Iss);

/** @internal */
export const Iss$outboundSchema: z.ZodNativeEnum<typeof Iss> =
  Iss$inboundSchema;

/**
 * @internal
 * @deprecated This namespace will be removed in future versions. Use schemas and types that are exported directly from this module.
 */
export namespace Iss$ {
  /** @deprecated use `Iss$inboundSchema` instead. */
  export const inboundSchema = Iss$inboundSchema;
  /** @deprecated use `Iss$outboundSchema` instead. */
  export const outboundSchema = Iss$outboundSchema;
}

/** @internal */
export const Bearer$inboundSchema: z.ZodType<Bearer, z.ZodTypeDef, unknown> = z
  .object({
    active: z.boolean(),
    scope: z.string(),
    client_id: z.string(),
    token_type: TokenType$inboundSchema,
    exp: z.nullable(z.number()),
    iat: z.number(),
    sub: z.string(),
    aud: z.string(),
    iss: Iss$inboundSchema,
    authorized_by_workspace_member_id: z.nullable(z.string()),
    workspace_id: z.string(),
    workspace_name: z.string(),
    workspace_slug: z.string(),
    workspace_logo_url: z.nullable(z.string()),
  }).transform((v) => {
    return remap$(v, {
      "client_id": "clientId",
      "token_type": "tokenType",
      "authorized_by_workspace_member_id": "authorizedByWorkspaceMemberId",
      "workspace_id": "workspaceId",
      "workspace_name": "workspaceName",
      "workspace_slug": "workspaceSlug",
      "workspace_logo_url": "workspaceLogoUrl",
    });
  });

/** @internal */
export type Bearer$Outbound = {
  active: boolean;
  scope: string;
  client_id: string;
  token_type: string;
  exp: number | null;
  iat: number;
  sub: string;
  aud: string;
  iss: string;
  authorized_by_workspace_member_id: string | null;
  workspace_id: string;
  workspace_name: string;
  workspace_slug: string;
  workspace_logo_url: string | null;
};

/** @internal */
export const Bearer$outboundSchema: z.ZodType<
  Bearer$Outbound,
  z.ZodTypeDef,
  Bearer
> = z.object({
  active: z.boolean(),
  scope: z.string(),
  clientId: z.string(),
  tokenType: TokenType$outboundSchema,
  exp: z.nullable(z.number()),
  iat: z.number(),
  sub: z.string(),
  aud: z.string(),
  iss: Iss$outboundSchema,
  authorizedByWorkspaceMemberId: z.nullable(z.string()),
  workspaceId: z.string(),
  workspaceName: z.string(),
  workspaceSlug: z.string(),
  workspaceLogoUrl: z.nullable(z.string()),
}).transform((v) => {
  return remap$(v, {
    clientId: "client_id",
    tokenType: "token_type",
    authorizedByWorkspaceMemberId: "authorized_by_workspace_member_id",
    workspaceId: "workspace_id",
    workspaceName: "workspace_name",
    workspaceSlug: "workspace_slug",
    workspaceLogoUrl: "workspace_logo_url",
  });
});

/**
 * @internal
 * @deprecated This namespace will be removed in future versions. Use schemas and types that are exported directly from this module.
 */
export namespace Bearer$ {
  /** @deprecated use `Bearer$inboundSchema` instead. */
  export const inboundSchema = Bearer$inboundSchema;
  /** @deprecated use `Bearer$outboundSchema` instead. */
  export const outboundSchema = Bearer$outboundSchema;
  /** @deprecated use `Bearer$Outbound` instead. */
  export type Outbound = Bearer$Outbound;
}

export function bearerToJSON(bearer: Bearer): string {
  return JSON.stringify(Bearer$outboundSchema.parse(bearer));
}

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

/** @internal */
export const ResponseBody$inboundSchema: z.ZodType<
  ResponseBody,
  z.ZodTypeDef,
  unknown
> = z.object({
  active: z.boolean(),
});

/** @internal */
export type ResponseBody$Outbound = {
  active: boolean;
};

/** @internal */
export const ResponseBody$outboundSchema: z.ZodType<
  ResponseBody$Outbound,
  z.ZodTypeDef,
  ResponseBody
> = z.object({
  active: z.boolean(),
});

/**
 * @internal
 * @deprecated This namespace will be removed in future versions. Use schemas and types that are exported directly from this module.
 */
export namespace ResponseBody$ {
  /** @deprecated use `ResponseBody$inboundSchema` instead. */
  export const inboundSchema = ResponseBody$inboundSchema;
  /** @deprecated use `ResponseBody$outboundSchema` instead. */
  export const outboundSchema = ResponseBody$outboundSchema;
  /** @deprecated use `ResponseBody$Outbound` instead. */
  export type Outbound = ResponseBody$Outbound;
}

export function responseBodyToJSON(responseBody: ResponseBody): string {
  return JSON.stringify(ResponseBody$outboundSchema.parse(responseBody));
}

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

/** @internal */
export const GetV2SelfResponse$inboundSchema: z.ZodType<
  GetV2SelfResponse,
  z.ZodTypeDef,
  unknown
> = z.union([
  z.lazy(() => ResponseBody$inboundSchema),
  z.lazy(() => Bearer$inboundSchema),
]);

/** @internal */
export type GetV2SelfResponse$Outbound =
  | ResponseBody$Outbound
  | Bearer$Outbound;

/** @internal */
export const GetV2SelfResponse$outboundSchema: z.ZodType<
  GetV2SelfResponse$Outbound,
  z.ZodTypeDef,
  GetV2SelfResponse
> = z.union([
  z.lazy(() => ResponseBody$outboundSchema),
  z.lazy(() => Bearer$outboundSchema),
]);

/**
 * @internal
 * @deprecated This namespace will be removed in future versions. Use schemas and types that are exported directly from this module.
 */
export namespace GetV2SelfResponse$ {
  /** @deprecated use `GetV2SelfResponse$inboundSchema` instead. */
  export const inboundSchema = GetV2SelfResponse$inboundSchema;
  /** @deprecated use `GetV2SelfResponse$outboundSchema` instead. */
  export const outboundSchema = GetV2SelfResponse$outboundSchema;
  /** @deprecated use `GetV2SelfResponse$Outbound` instead. */
  export type Outbound = GetV2SelfResponse$Outbound;
}

export function getV2SelfResponseToJSON(
  getV2SelfResponse: GetV2SelfResponse,
): string {
  return JSON.stringify(
    GetV2SelfResponse$outboundSchema.parse(getV2SelfResponse),
  );
}

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