/*
 * 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 { SDKValidationError } from "./sdkvalidationerror.js";

/**
 * Type of matcher. One of "equals", "startsWith", or "endsWith".
 */
export const CreateCustomEnvironmentType = {
  Equals: "equals",
  StartsWith: "startsWith",
  EndsWith: "endsWith",
} as const;
/**
 * Type of matcher. One of "equals", "startsWith", or "endsWith".
 */
export type CreateCustomEnvironmentType = ClosedEnum<
  typeof CreateCustomEnvironmentType
>;

/**
 * How we want to determine a matching branch. This is optional.
 */
export type CreateCustomEnvironmentBranchMatcher = {
  /**
   * Type of matcher. One of "equals", "startsWith", or "endsWith".
   */
  type: CreateCustomEnvironmentType;
  /**
   * Git branch name or portion thereof.
   */
  pattern: string;
};

export type CreateCustomEnvironmentRequestBody = {
  /**
   * The slug of the custom environment to create.
   */
  slug?: string | undefined;
  /**
   * Description of the custom environment. This is optional.
   */
  description?: string | undefined;
  /**
   * How we want to determine a matching branch. This is optional.
   */
  branchMatcher?: CreateCustomEnvironmentBranchMatcher | undefined;
  /**
   * Where to copy environment variables from. This is optional.
   */
  copyEnvVarsFrom?: string | undefined;
};

export type CreateCustomEnvironmentRequest = {
  /**
   * The unique project identifier or the project name
   */
  idOrName: string;
  /**
   * The Team identifier to perform the request on behalf of.
   */
  teamId?: string | undefined;
  /**
   * The Team slug to perform the request on behalf of.
   */
  slug?: string | undefined;
  requestBody?: CreateCustomEnvironmentRequestBody | undefined;
};

/**
 * The type of environment (production, preview, or development)
 */
export const CreateCustomEnvironmentEnvironmentType = {
  Development: "development",
  Preview: "preview",
  Production: "production",
} as const;
/**
 * The type of environment (production, preview, or development)
 */
export type CreateCustomEnvironmentEnvironmentType = ClosedEnum<
  typeof CreateCustomEnvironmentEnvironmentType
>;

/**
 * The type of matching to perform
 */
export const CreateCustomEnvironmentEnvironmentResponseType = {
  EndsWith: "endsWith",
  Equals: "equals",
  StartsWith: "startsWith",
} as const;
/**
 * The type of matching to perform
 */
export type CreateCustomEnvironmentEnvironmentResponseType = ClosedEnum<
  typeof CreateCustomEnvironmentEnvironmentResponseType
>;

/**
 * Configuration for matching git branches to this environment
 */
export type CreateCustomEnvironmentEnvironmentBranchMatcher = {
  /**
   * The type of matching to perform
   */
  type: CreateCustomEnvironmentEnvironmentResponseType;
  /**
   * The pattern to match against branch names
   */
  pattern: string;
};

/**
 * A list of verification challenges, one of which must be completed to verify the domain for use on the project. After the challenge is complete `POST /projects/:idOrName/domains/:domain/verify` to verify the domain. Possible challenges: - If `verification.type = TXT` the `verification.domain` will be checked for a TXT record matching `verification.value`.
 */
export type CreateCustomEnvironmentVerification = {
  type: string;
  domain: string;
  value: string;
  reason: string;
};

/**
 * List of domains associated with this environment
 */
export type CreateCustomEnvironmentDomains = {
  name: string;
  apexName: string;
  projectId: string;
  redirect?: string | null | undefined;
  redirectStatusCode?: number | null | undefined;
  gitBranch?: string | null | undefined;
  customEnvironmentId?: string | null | undefined;
  updatedAt?: number | undefined;
  createdAt?: number | undefined;
  /**
   * `true` if the domain is verified for use with the project. If `false` it will not be used as an alias on this project until the challenge in `verification` is completed.
   */
  verified: boolean;
  /**
   * A list of verification challenges, one of which must be completed to verify the domain for use on the project. After the challenge is complete `POST /projects/:idOrName/domains/:domain/verify` to verify the domain. Possible challenges: - If `verification.type = TXT` the `verification.domain` will be checked for a TXT record matching `verification.value`.
   */
  verification?: Array<CreateCustomEnvironmentVerification> | undefined;
};

/**
 * Internal representation of a custom environment with all required properties
 */
export type CreateCustomEnvironmentResponseBody = {
  /**
   * Unique identifier for the custom environment (format: env_*)
   */
  id: string;
  /**
   * URL-friendly name of the environment
   */
  slug: string;
  /**
   * The type of environment (production, preview, or development)
   */
  type: CreateCustomEnvironmentEnvironmentType;
  /**
   * Optional description of the environment's purpose
   */
  description?: string | undefined;
  /**
   * Configuration for matching git branches to this environment
   */
  branchMatcher?: CreateCustomEnvironmentEnvironmentBranchMatcher | undefined;
  /**
   * List of domains associated with this environment
   */
  domains?: Array<CreateCustomEnvironmentDomains> | undefined;
  /**
   * List of aliases for the current deployment
   */
  currentDeploymentAliases?: Array<string> | undefined;
  /**
   * Timestamp when the environment was created
   */
  createdAt: number;
  /**
   * Timestamp when the environment was last updated
   */
  updatedAt: number;
};

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

/** @internal */
export type CreateCustomEnvironmentBranchMatcher$Outbound = {
  type: string;
  pattern: string;
};

/** @internal */
export const CreateCustomEnvironmentBranchMatcher$outboundSchema: z.ZodType<
  CreateCustomEnvironmentBranchMatcher$Outbound,
  z.ZodTypeDef,
  CreateCustomEnvironmentBranchMatcher
> = z.object({
  type: CreateCustomEnvironmentType$outboundSchema,
  pattern: z.string(),
});

export function createCustomEnvironmentBranchMatcherToJSON(
  createCustomEnvironmentBranchMatcher: CreateCustomEnvironmentBranchMatcher,
): string {
  return JSON.stringify(
    CreateCustomEnvironmentBranchMatcher$outboundSchema.parse(
      createCustomEnvironmentBranchMatcher,
    ),
  );
}

/** @internal */
export type CreateCustomEnvironmentRequestBody$Outbound = {
  slug?: string | undefined;
  description?: string | undefined;
  branchMatcher?: CreateCustomEnvironmentBranchMatcher$Outbound | undefined;
  copyEnvVarsFrom?: string | undefined;
};

/** @internal */
export const CreateCustomEnvironmentRequestBody$outboundSchema: z.ZodType<
  CreateCustomEnvironmentRequestBody$Outbound,
  z.ZodTypeDef,
  CreateCustomEnvironmentRequestBody
> = z.object({
  slug: z.string().optional(),
  description: z.string().optional(),
  branchMatcher: z.lazy(() =>
    CreateCustomEnvironmentBranchMatcher$outboundSchema
  ).optional(),
  copyEnvVarsFrom: z.string().optional(),
});

export function createCustomEnvironmentRequestBodyToJSON(
  createCustomEnvironmentRequestBody: CreateCustomEnvironmentRequestBody,
): string {
  return JSON.stringify(
    CreateCustomEnvironmentRequestBody$outboundSchema.parse(
      createCustomEnvironmentRequestBody,
    ),
  );
}

/** @internal */
export type CreateCustomEnvironmentRequest$Outbound = {
  idOrName: string;
  teamId?: string | undefined;
  slug?: string | undefined;
  RequestBody?: CreateCustomEnvironmentRequestBody$Outbound | undefined;
};

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

export function createCustomEnvironmentRequestToJSON(
  createCustomEnvironmentRequest: CreateCustomEnvironmentRequest,
): string {
  return JSON.stringify(
    CreateCustomEnvironmentRequest$outboundSchema.parse(
      createCustomEnvironmentRequest,
    ),
  );
}

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

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

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

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

/** @internal */
export const CreateCustomEnvironmentVerification$inboundSchema: z.ZodType<
  CreateCustomEnvironmentVerification,
  z.ZodTypeDef,
  unknown
> = z.object({
  type: types.string(),
  domain: types.string(),
  value: types.string(),
  reason: types.string(),
});

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

/** @internal */
export const CreateCustomEnvironmentDomains$inboundSchema: z.ZodType<
  CreateCustomEnvironmentDomains,
  z.ZodTypeDef,
  unknown
> = z.object({
  name: types.string(),
  apexName: types.string(),
  projectId: types.string(),
  redirect: z.nullable(types.string()).optional(),
  redirectStatusCode: z.nullable(types.number()).optional(),
  gitBranch: z.nullable(types.string()).optional(),
  customEnvironmentId: z.nullable(types.string()).optional(),
  updatedAt: types.optional(types.number()),
  createdAt: types.optional(types.number()),
  verified: types.boolean(),
  verification: types.optional(
    z.array(z.lazy(() => CreateCustomEnvironmentVerification$inboundSchema)),
  ),
});

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

/** @internal */
export const CreateCustomEnvironmentResponseBody$inboundSchema: z.ZodType<
  CreateCustomEnvironmentResponseBody,
  z.ZodTypeDef,
  unknown
> = z.object({
  id: types.string(),
  slug: types.string(),
  type: CreateCustomEnvironmentEnvironmentType$inboundSchema,
  description: types.optional(types.string()),
  branchMatcher: types.optional(
    z.lazy(() => CreateCustomEnvironmentEnvironmentBranchMatcher$inboundSchema),
  ),
  domains: types.optional(
    z.array(z.lazy(() => CreateCustomEnvironmentDomains$inboundSchema)),
  ),
  currentDeploymentAliases: types.optional(z.array(types.string())),
  createdAt: types.number(),
  updatedAt: types.number(),
});

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