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

import * as z from "zod/v3";
import { safeParse } from "../lib/schemas.js";
import { Result as SafeParseResult } from "../types/fp.js";
import * as types from "../types/primitives.js";
import { smartUnion } from "../types/smartUnion.js";
import {
  EdgeConfigItemValue,
  EdgeConfigItemValue$inboundSchema,
} from "./edgeconfigitemvalue.js";
import { SDKValidationError } from "./sdkvalidationerror.js";

export type GetEdgeConfigBackupRequest = {
  edgeConfigId: string;
  edgeConfigBackupVersionId: 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;
};

export type ResponseBodyUser = {
  id: string;
  username: string;
  email: string;
  name?: string | undefined;
  avatar?: string | undefined;
};

export type GetEdgeConfigBackupResponseBodyItems = {
  createdAt: number;
  updatedAt: number;
  value: EdgeConfigItemValue | null;
  description?: string | undefined;
};

export type ResponseBodyBackup = {
  /**
   * Name for the Edge Config Names are not unique. Must start with an alphabetic character and can contain only alphanumeric characters and underscores).
   */
  slug: string;
  updatedAt: number;
  items: { [k: string]: GetEdgeConfigBackupResponseBodyItems };
  digest: string;
};

export type ResponseBodyMetadata = {
  updatedAt?: string | undefined;
  updatedBy?: string | undefined;
  itemsCount?: number | undefined;
  itemsBytes?: number | undefined;
};

export type GetEdgeConfigBackupResponseBody2 = {
  user: ResponseBodyUser;
  id: string;
  lastModified: number;
  backup: ResponseBodyBackup;
  metadata: ResponseBodyMetadata;
};

export type ResponseBodyItems = {
  createdAt: number;
  updatedAt: number;
  value: EdgeConfigItemValue | null;
  description?: string | undefined;
};

export type Backup = {
  /**
   * Name for the Edge Config Names are not unique. Must start with an alphabetic character and can contain only alphanumeric characters and underscores).
   */
  slug: string;
  updatedAt: number;
  items: { [k: string]: ResponseBodyItems };
  digest: string;
};

export type GetEdgeConfigBackupResponseBodyMetadata = {
  updatedAt?: string | undefined;
  updatedBy?: string | undefined;
  itemsCount?: number | undefined;
  itemsBytes?: number | undefined;
};

export type GetEdgeConfigBackupResponseBodyUser = {
  id: string;
  username: string;
  email: string;
  name?: string | undefined;
  avatar?: string | undefined;
};

/**
 * The object the API responds with when requesting an Edge Config backup
 */
export type GetEdgeConfigBackupResponseBody1 = {
  id: string;
  lastModified: number;
  backup: Backup;
  metadata: GetEdgeConfigBackupResponseBodyMetadata;
  user?: GetEdgeConfigBackupResponseBodyUser | undefined;
};

export type GetEdgeConfigBackupResponseBody =
  | GetEdgeConfigBackupResponseBody2
  | GetEdgeConfigBackupResponseBody1;

/** @internal */
export type GetEdgeConfigBackupRequest$Outbound = {
  edgeConfigId: string;
  edgeConfigBackupVersionId: string;
  teamId?: string | undefined;
  slug?: string | undefined;
};

/** @internal */
export const GetEdgeConfigBackupRequest$outboundSchema: z.ZodType<
  GetEdgeConfigBackupRequest$Outbound,
  z.ZodTypeDef,
  GetEdgeConfigBackupRequest
> = z.object({
  edgeConfigId: z.string(),
  edgeConfigBackupVersionId: z.string(),
  teamId: z.string().optional(),
  slug: z.string().optional(),
});

export function getEdgeConfigBackupRequestToJSON(
  getEdgeConfigBackupRequest: GetEdgeConfigBackupRequest,
): string {
  return JSON.stringify(
    GetEdgeConfigBackupRequest$outboundSchema.parse(getEdgeConfigBackupRequest),
  );
}

/** @internal */
export const ResponseBodyUser$inboundSchema: z.ZodType<
  ResponseBodyUser,
  z.ZodTypeDef,
  unknown
> = z.object({
  id: types.string(),
  username: types.string(),
  email: types.string(),
  name: types.optional(types.string()),
  avatar: types.optional(types.string()),
});

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

/** @internal */
export const GetEdgeConfigBackupResponseBodyItems$inboundSchema: z.ZodType<
  GetEdgeConfigBackupResponseBodyItems,
  z.ZodTypeDef,
  unknown
> = z.object({
  createdAt: types.number(),
  updatedAt: types.number(),
  value: types.nullable(EdgeConfigItemValue$inboundSchema),
  description: types.optional(types.string()),
});

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

/** @internal */
export const ResponseBodyBackup$inboundSchema: z.ZodType<
  ResponseBodyBackup,
  z.ZodTypeDef,
  unknown
> = z.object({
  slug: types.string(),
  updatedAt: types.number(),
  items: z.record(
    z.lazy(() => GetEdgeConfigBackupResponseBodyItems$inboundSchema),
  ),
  digest: types.string(),
});

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

/** @internal */
export const ResponseBodyMetadata$inboundSchema: z.ZodType<
  ResponseBodyMetadata,
  z.ZodTypeDef,
  unknown
> = z.object({
  updatedAt: types.optional(types.string()),
  updatedBy: types.optional(types.string()),
  itemsCount: types.optional(types.number()),
  itemsBytes: types.optional(types.number()),
});

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

/** @internal */
export const GetEdgeConfigBackupResponseBody2$inboundSchema: z.ZodType<
  GetEdgeConfigBackupResponseBody2,
  z.ZodTypeDef,
  unknown
> = z.object({
  user: z.lazy(() => ResponseBodyUser$inboundSchema),
  id: types.string(),
  lastModified: types.number(),
  backup: z.lazy(() => ResponseBodyBackup$inboundSchema),
  metadata: z.lazy(() => ResponseBodyMetadata$inboundSchema),
});

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

/** @internal */
export const ResponseBodyItems$inboundSchema: z.ZodType<
  ResponseBodyItems,
  z.ZodTypeDef,
  unknown
> = z.object({
  createdAt: types.number(),
  updatedAt: types.number(),
  value: types.nullable(EdgeConfigItemValue$inboundSchema),
  description: types.optional(types.string()),
});

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

/** @internal */
export const Backup$inboundSchema: z.ZodType<Backup, z.ZodTypeDef, unknown> = z
  .object({
    slug: types.string(),
    updatedAt: types.number(),
    items: z.record(z.lazy(() => ResponseBodyItems$inboundSchema)),
    digest: types.string(),
  });

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

/** @internal */
export const GetEdgeConfigBackupResponseBodyMetadata$inboundSchema: z.ZodType<
  GetEdgeConfigBackupResponseBodyMetadata,
  z.ZodTypeDef,
  unknown
> = z.object({
  updatedAt: types.optional(types.string()),
  updatedBy: types.optional(types.string()),
  itemsCount: types.optional(types.number()),
  itemsBytes: types.optional(types.number()),
});

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

/** @internal */
export const GetEdgeConfigBackupResponseBodyUser$inboundSchema: z.ZodType<
  GetEdgeConfigBackupResponseBodyUser,
  z.ZodTypeDef,
  unknown
> = z.object({
  id: types.string(),
  username: types.string(),
  email: types.string(),
  name: types.optional(types.string()),
  avatar: types.optional(types.string()),
});

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

/** @internal */
export const GetEdgeConfigBackupResponseBody1$inboundSchema: z.ZodType<
  GetEdgeConfigBackupResponseBody1,
  z.ZodTypeDef,
  unknown
> = z.object({
  id: types.string(),
  lastModified: types.number(),
  backup: z.lazy(() => Backup$inboundSchema),
  metadata: z.lazy(() => GetEdgeConfigBackupResponseBodyMetadata$inboundSchema),
  user: types.optional(
    z.lazy(() => GetEdgeConfigBackupResponseBodyUser$inboundSchema),
  ),
});

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

/** @internal */
export const GetEdgeConfigBackupResponseBody$inboundSchema: z.ZodType<
  GetEdgeConfigBackupResponseBody,
  z.ZodTypeDef,
  unknown
> = smartUnion([
  z.lazy(() => GetEdgeConfigBackupResponseBody2$inboundSchema),
  z.lazy(() => GetEdgeConfigBackupResponseBody1$inboundSchema),
]);

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