/*
 * 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 { SDKValidationError } from "../errors/sdkvalidationerror.js";
import {
  Access,
  Access$inboundSchema,
  Access$Outbound,
  Access$outboundSchema,
} from "./access.js";
import {
  BranchRep,
  BranchRep$inboundSchema,
  BranchRep$Outbound,
  BranchRep$outboundSchema,
} from "./branchrep.js";

/**
 * The type of repository
 */
export const RepositoryRepType = {
  Bitbucket: "bitbucket",
  Custom: "custom",
  Github: "github",
  Gitlab: "gitlab",
} as const;
/**
 * The type of repository
 */
export type RepositoryRepType = ClosedEnum<typeof RepositoryRepType>;

export type RepositoryRep = {
  /**
   * The repository name
   */
  name: string;
  /**
   * A URL to access the repository
   */
  sourceLink?: string | undefined;
  /**
   * A template for constructing a valid URL to view the commit
   */
  commitUrlTemplate?: string | undefined;
  /**
   * A template for constructing a valid URL to view the hunk
   */
  hunkUrlTemplate?: string | undefined;
  /**
   * The type of repository
   */
  type: RepositoryRepType;
  /**
   * The repository's default branch
   */
  defaultBranch: string;
  /**
   * Whether or not a repository is enabled for code reference scanning
   */
  enabled: boolean;
  /**
   * The version of the repository's saved information
   */
  version: number;
  /**
   * An array of the repository's branches that have been scanned for code references
   */
  branches?: Array<BranchRep> | undefined;
  links: { [k: string]: any };
  access?: Access | undefined;
};

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

/** @internal */
export const RepositoryRep$inboundSchema: z.ZodType<
  RepositoryRep,
  z.ZodTypeDef,
  unknown
> = z.object({
  name: z.string(),
  sourceLink: z.string().optional(),
  commitUrlTemplate: z.string().optional(),
  hunkUrlTemplate: z.string().optional(),
  type: RepositoryRepType$inboundSchema,
  defaultBranch: z.string(),
  enabled: z.boolean(),
  version: z.number().int(),
  branches: z.array(BranchRep$inboundSchema).optional(),
  _links: z.record(z.any()),
  _access: Access$inboundSchema.optional(),
}).transform((v) => {
  return remap$(v, {
    "_links": "links",
    "_access": "access",
  });
});
/** @internal */
export type RepositoryRep$Outbound = {
  name: string;
  sourceLink?: string | undefined;
  commitUrlTemplate?: string | undefined;
  hunkUrlTemplate?: string | undefined;
  type: string;
  defaultBranch: string;
  enabled: boolean;
  version: number;
  branches?: Array<BranchRep$Outbound> | undefined;
  _links: { [k: string]: any };
  _access?: Access$Outbound | undefined;
};

/** @internal */
export const RepositoryRep$outboundSchema: z.ZodType<
  RepositoryRep$Outbound,
  z.ZodTypeDef,
  RepositoryRep
> = z.object({
  name: z.string(),
  sourceLink: z.string().optional(),
  commitUrlTemplate: z.string().optional(),
  hunkUrlTemplate: z.string().optional(),
  type: RepositoryRepType$outboundSchema,
  defaultBranch: z.string(),
  enabled: z.boolean(),
  version: z.number().int(),
  branches: z.array(BranchRep$outboundSchema).optional(),
  links: z.record(z.any()),
  access: Access$outboundSchema.optional(),
}).transform((v) => {
  return remap$(v, {
    links: "_links",
    access: "_access",
  });
});

export function repositoryRepToJSON(repositoryRep: RepositoryRep): string {
  return JSON.stringify(RepositoryRep$outboundSchema.parse(repositoryRep));
}
export function repositoryRepFromJSON(
  jsonString: string,
): SafeParseResult<RepositoryRep, SDKValidationError> {
  return safeParse(
    jsonString,
    (x) => RepositoryRep$inboundSchema.parse(JSON.parse(x)),
    `Failed to parse 'RepositoryRep' from JSON`,
  );
}
