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

import * as z from "zod/v3";
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 * as types from "../types/primitives.js";
import { smartUnion } from "../types/smartUnion.js";
import { SDKValidationError } from "./sdkvalidationerror.js";

export type TypeOther = {
  type: string;
  additionalProperties?: { [k: string]: any } | undefined;
};

export const GetConnectorTokenSubjectConnectRequestType = {
  JwtBearer: "jwt-bearer",
} as const;
export type GetConnectorTokenSubjectConnectRequestType = ClosedEnum<
  typeof GetConnectorTokenSubjectConnectRequestType
>;

export type TypeJwtBearer = {
  type: GetConnectorTokenSubjectConnectRequestType;
  sub?: string | undefined;
  iss?: string | undefined;
  aud?: string | undefined;
  additionalClaims?: { [k: string]: any } | undefined;
  additionalProperties?: { [k: string]: any } | undefined;
};

export const GetConnectorTokenSubjectConnectType = {
  User: "user",
} as const;
export type GetConnectorTokenSubjectConnectType = ClosedEnum<
  typeof GetConnectorTokenSubjectConnectType
>;

export type TypeUser = {
  type: GetConnectorTokenSubjectConnectType;
  id?: string | undefined;
  issuer?: string | undefined;
  additionalProperties?: { [k: string]: any } | undefined;
};

export const GetConnectorTokenSubjectType = {
  App: "app",
} as const;
export type GetConnectorTokenSubjectType = ClosedEnum<
  typeof GetConnectorTokenSubjectType
>;

export type TypeApp = {
  type: GetConnectorTokenSubjectType;
  additionalProperties?: { [k: string]: any } | undefined;
};

export type Subject = TypeApp | TypeUser | TypeJwtBearer | TypeOther;

export type AuthorizationDetails = {
  type?: string | undefined;
  additionalProperties?: { [k: string]: any } | undefined;
};

export type GetConnectorTokenRequestBody = {
  subject?: TypeApp | TypeUser | TypeJwtBearer | TypeOther | undefined;
  installationId?: string | undefined;
  audience?: Array<string> | undefined;
  scopes?: Array<string> | undefined;
  resources?: Array<string> | undefined;
  authorizationDetails?: Array<AuthorizationDetails> | undefined;
  validityBufferMs?: number | undefined;
};

export type GetConnectorTokenRequest = {
  connector: string;
  requestBody?: GetConnectorTokenRequestBody | undefined;
};

export type Connector = {
  id: string;
  uid: string;
  type: string;
};

export type GetConnectorTokenResponseBody = {
  token: string;
  expiresAt: number;
  connector: Connector;
  name?: string | undefined;
  installationId?: string | undefined;
  tenantId?: string | undefined;
  externalSubject?: string | undefined;
  /**
   * Driver-specific metadata (e.g., botUserId for Slack).
   */
  metadata?: { [k: string]: any } | undefined;
};

/** @internal */
export type TypeOther$Outbound = {
  type: string;
  [additionalProperties: string]: unknown;
};

/** @internal */
export const TypeOther$outboundSchema: z.ZodType<
  TypeOther$Outbound,
  z.ZodTypeDef,
  TypeOther
> = z.object({
  type: z.string(),
  additionalProperties: z.record(z.any()).optional(),
}).transform((v) => {
  return {
    ...v.additionalProperties,
    ...remap$(v, {
      additionalProperties: null,
    }),
  };
});

export function typeOtherToJSON(typeOther: TypeOther): string {
  return JSON.stringify(TypeOther$outboundSchema.parse(typeOther));
}

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

/** @internal */
export type TypeJwtBearer$Outbound = {
  type: string;
  sub?: string | undefined;
  iss?: string | undefined;
  aud?: string | undefined;
  additionalClaims?: { [k: string]: any } | undefined;
  [additionalProperties: string]: unknown;
};

/** @internal */
export const TypeJwtBearer$outboundSchema: z.ZodType<
  TypeJwtBearer$Outbound,
  z.ZodTypeDef,
  TypeJwtBearer
> = z.object({
  type: GetConnectorTokenSubjectConnectRequestType$outboundSchema,
  sub: z.string().optional(),
  iss: z.string().optional(),
  aud: z.string().optional(),
  additionalClaims: z.record(z.any()).optional(),
  additionalProperties: z.record(z.any()).optional(),
}).transform((v) => {
  return {
    ...v.additionalProperties,
    ...remap$(v, {
      additionalProperties: null,
    }),
  };
});

export function typeJwtBearerToJSON(typeJwtBearer: TypeJwtBearer): string {
  return JSON.stringify(TypeJwtBearer$outboundSchema.parse(typeJwtBearer));
}

/** @internal */
export const GetConnectorTokenSubjectConnectType$outboundSchema:
  z.ZodNativeEnum<typeof GetConnectorTokenSubjectConnectType> = z.nativeEnum(
    GetConnectorTokenSubjectConnectType,
  );

/** @internal */
export type TypeUser$Outbound = {
  type: string;
  id?: string | undefined;
  issuer?: string | undefined;
  [additionalProperties: string]: unknown;
};

/** @internal */
export const TypeUser$outboundSchema: z.ZodType<
  TypeUser$Outbound,
  z.ZodTypeDef,
  TypeUser
> = z.object({
  type: GetConnectorTokenSubjectConnectType$outboundSchema,
  id: z.string().optional(),
  issuer: z.string().optional(),
  additionalProperties: z.record(z.any()).optional(),
}).transform((v) => {
  return {
    ...v.additionalProperties,
    ...remap$(v, {
      additionalProperties: null,
    }),
  };
});

export function typeUserToJSON(typeUser: TypeUser): string {
  return JSON.stringify(TypeUser$outboundSchema.parse(typeUser));
}

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

/** @internal */
export type TypeApp$Outbound = {
  type: string;
  [additionalProperties: string]: unknown;
};

/** @internal */
export const TypeApp$outboundSchema: z.ZodType<
  TypeApp$Outbound,
  z.ZodTypeDef,
  TypeApp
> = z.object({
  type: GetConnectorTokenSubjectType$outboundSchema,
  additionalProperties: z.record(z.any()).optional(),
}).transform((v) => {
  return {
    ...v.additionalProperties,
    ...remap$(v, {
      additionalProperties: null,
    }),
  };
});

export function typeAppToJSON(typeApp: TypeApp): string {
  return JSON.stringify(TypeApp$outboundSchema.parse(typeApp));
}

/** @internal */
export type Subject$Outbound =
  | TypeApp$Outbound
  | TypeUser$Outbound
  | TypeJwtBearer$Outbound
  | TypeOther$Outbound;

/** @internal */
export const Subject$outboundSchema: z.ZodType<
  Subject$Outbound,
  z.ZodTypeDef,
  Subject
> = smartUnion([
  z.lazy(() => TypeApp$outboundSchema),
  z.lazy(() => TypeUser$outboundSchema),
  z.lazy(() => TypeJwtBearer$outboundSchema),
  z.lazy(() => TypeOther$outboundSchema),
]);

export function subjectToJSON(subject: Subject): string {
  return JSON.stringify(Subject$outboundSchema.parse(subject));
}

/** @internal */
export type AuthorizationDetails$Outbound = {
  type?: string | undefined;
  [additionalProperties: string]: unknown;
};

/** @internal */
export const AuthorizationDetails$outboundSchema: z.ZodType<
  AuthorizationDetails$Outbound,
  z.ZodTypeDef,
  AuthorizationDetails
> = z.object({
  type: z.string().optional(),
  additionalProperties: z.record(z.any()).optional(),
}).transform((v) => {
  return {
    ...v.additionalProperties,
    ...remap$(v, {
      additionalProperties: null,
    }),
  };
});

export function authorizationDetailsToJSON(
  authorizationDetails: AuthorizationDetails,
): string {
  return JSON.stringify(
    AuthorizationDetails$outboundSchema.parse(authorizationDetails),
  );
}

/** @internal */
export type GetConnectorTokenRequestBody$Outbound = {
  subject?:
    | TypeApp$Outbound
    | TypeUser$Outbound
    | TypeJwtBearer$Outbound
    | TypeOther$Outbound
    | undefined;
  installationId?: string | undefined;
  audience?: Array<string> | undefined;
  scopes?: Array<string> | undefined;
  resources?: Array<string> | undefined;
  authorizationDetails?: Array<AuthorizationDetails$Outbound> | undefined;
  validityBufferMs?: number | undefined;
};

/** @internal */
export const GetConnectorTokenRequestBody$outboundSchema: z.ZodType<
  GetConnectorTokenRequestBody$Outbound,
  z.ZodTypeDef,
  GetConnectorTokenRequestBody
> = z.object({
  subject: smartUnion([
    z.lazy(() => TypeApp$outboundSchema),
    z.lazy(() => TypeUser$outboundSchema),
    z.lazy(() => TypeJwtBearer$outboundSchema),
    z.lazy(() => TypeOther$outboundSchema),
  ]).optional(),
  installationId: z.string().optional(),
  audience: z.array(z.string()).optional(),
  scopes: z.array(z.string()).optional(),
  resources: z.array(z.string()).optional(),
  authorizationDetails: z.array(
    z.lazy(() => AuthorizationDetails$outboundSchema),
  ).optional(),
  validityBufferMs: z.number().optional(),
});

export function getConnectorTokenRequestBodyToJSON(
  getConnectorTokenRequestBody: GetConnectorTokenRequestBody,
): string {
  return JSON.stringify(
    GetConnectorTokenRequestBody$outboundSchema.parse(
      getConnectorTokenRequestBody,
    ),
  );
}

/** @internal */
export type GetConnectorTokenRequest$Outbound = {
  connector: string;
  RequestBody?: GetConnectorTokenRequestBody$Outbound | undefined;
};

/** @internal */
export const GetConnectorTokenRequest$outboundSchema: z.ZodType<
  GetConnectorTokenRequest$Outbound,
  z.ZodTypeDef,
  GetConnectorTokenRequest
> = z.object({
  connector: z.string(),
  requestBody: z.lazy(() => GetConnectorTokenRequestBody$outboundSchema)
    .optional(),
}).transform((v) => {
  return remap$(v, {
    requestBody: "RequestBody",
  });
});

export function getConnectorTokenRequestToJSON(
  getConnectorTokenRequest: GetConnectorTokenRequest,
): string {
  return JSON.stringify(
    GetConnectorTokenRequest$outboundSchema.parse(getConnectorTokenRequest),
  );
}

/** @internal */
export const Connector$inboundSchema: z.ZodType<
  Connector,
  z.ZodTypeDef,
  unknown
> = z.object({
  id: types.string(),
  uid: types.string(),
  type: types.string(),
});

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

/** @internal */
export const GetConnectorTokenResponseBody$inboundSchema: z.ZodType<
  GetConnectorTokenResponseBody,
  z.ZodTypeDef,
  unknown
> = z.object({
  token: types.string(),
  expiresAt: types.number(),
  connector: z.lazy(() => Connector$inboundSchema),
  name: types.optional(types.string()),
  installationId: types.optional(types.string()),
  tenantId: types.optional(types.string()),
  externalSubject: types.optional(types.string()),
  metadata: types.optional(z.record(z.any())),
});

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