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

import * as z from "zod/v3";
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 const QueryParamProvider = {
  Github: "github",
  GithubLimited: "github-limited",
  GithubCustomHost: "github-custom-host",
  Gitlab: "gitlab",
  Bitbucket: "bitbucket",
} as const;
export type QueryParamProvider = ClosedEnum<typeof QueryParamProvider>;

export type GitNamespacesRequest = {
  /**
   * The custom Git host if using a custom Git provider, like GitHub Enterprise Server
   */
  host?: string | undefined;
  provider?: QueryParamProvider | undefined;
  /**
   * When true, includes the viewer object for each namespace.
   */
  viewerMetadata?: boolean | undefined;
};

export type GitNamespacesId = string | number;

export type GitNamespacesRole = string | number;

export type Viewer = {
  canCreateApp?: boolean | undefined;
  role?: string | number | undefined;
};

export type GitNamespacesResponseBody = {
  provider: string;
  slug: string;
  id: string | number;
  ownerType: string;
  name?: string | undefined;
  isAccessRestricted?: boolean | undefined;
  installationId?: number | undefined;
  requireReauth?: boolean | undefined;
  viewer?: Viewer | undefined;
};

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

/** @internal */
export type GitNamespacesRequest$Outbound = {
  host?: string | undefined;
  provider?: string | undefined;
  viewerMetadata?: boolean | undefined;
};

/** @internal */
export const GitNamespacesRequest$outboundSchema: z.ZodType<
  GitNamespacesRequest$Outbound,
  z.ZodTypeDef,
  GitNamespacesRequest
> = z.object({
  host: z.string().optional(),
  provider: QueryParamProvider$outboundSchema.optional(),
  viewerMetadata: z.boolean().optional(),
});

export function gitNamespacesRequestToJSON(
  gitNamespacesRequest: GitNamespacesRequest,
): string {
  return JSON.stringify(
    GitNamespacesRequest$outboundSchema.parse(gitNamespacesRequest),
  );
}

/** @internal */
export const GitNamespacesId$inboundSchema: z.ZodType<
  GitNamespacesId,
  z.ZodTypeDef,
  unknown
> = smartUnion([types.string(), types.number()]);

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

/** @internal */
export const GitNamespacesRole$inboundSchema: z.ZodType<
  GitNamespacesRole,
  z.ZodTypeDef,
  unknown
> = smartUnion([types.string(), types.number()]);

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

/** @internal */
export const Viewer$inboundSchema: z.ZodType<Viewer, z.ZodTypeDef, unknown> = z
  .object({
    canCreateApp: types.optional(types.boolean()),
    role: types.optional(smartUnion([types.string(), types.number()])),
  });

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

/** @internal */
export const GitNamespacesResponseBody$inboundSchema: z.ZodType<
  GitNamespacesResponseBody,
  z.ZodTypeDef,
  unknown
> = z.object({
  provider: types.string(),
  slug: types.string(),
  id: smartUnion([types.string(), types.number()]),
  ownerType: types.string(),
  name: types.optional(types.string()),
  isAccessRestricted: types.optional(types.boolean()),
  installationId: types.optional(types.number()),
  requireReauth: types.optional(types.boolean()),
  viewer: types.optional(z.lazy(() => Viewer$inboundSchema)),
});

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