/*
 * 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 const UpdateSharedEnvVariableTarget = {
  Production: "production",
  Preview: "preview",
  Development: "development",
} as const;
export type UpdateSharedEnvVariableTarget = ClosedEnum<
  typeof UpdateSharedEnvVariableTarget
>;

/**
 * Incrementally update project linking without specifying the full list
 */
export type ProjectIdUpdates = {
  /**
   * Project IDs to add to this environment variable
   */
  link?: Array<string> | undefined;
  /**
   * Project IDs to remove from this environment variable
   */
  unlink?: Array<string> | undefined;
};

/**
 * The new type of the Shared Environment Variable
 */
export const UpdateSharedEnvVariableType = {
  Encrypted: "encrypted",
  Sensitive: "sensitive",
} as const;
/**
 * The new type of the Shared Environment Variable
 */
export type UpdateSharedEnvVariableType = ClosedEnum<
  typeof UpdateSharedEnvVariableType
>;

export type Updates = {
  /**
   * The name of the Shared Environment Variable
   */
  key?: string | undefined;
  /**
   * The value of the Shared Environment Variable
   */
  value?: string | undefined;
  /**
   * The target environment of the Shared Environment Variable
   */
  target?: Array<UpdateSharedEnvVariableTarget> | undefined;
  /**
   * Associate a Shared Environment Variable to projects.
   */
  projectId?: Array<string> | undefined;
  /**
   * Incrementally update project linking without specifying the full list
   */
  projectIdUpdates?: ProjectIdUpdates | undefined;
  /**
   * The new type of the Shared Environment Variable
   */
  type?: UpdateSharedEnvVariableType | undefined;
  /**
   * A comment to add context on what this Shared Environment Variable is for
   */
  comment?: string | undefined;
};

export type UpdateSharedEnvVariableRequestBody = {
  /**
   * An object where each key is an environment variable ID (not the key name) and the value is the update to apply
   */
  updates: { [k: string]: Updates };
};

export type UpdateSharedEnvVariableRequest = {
  /**
   * 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?: UpdateSharedEnvVariableRequestBody | undefined;
};

/**
 * The type of this cosmos doc instance, if blank, assume secret.
 */
export const UpdateSharedEnvVariableEnvironmentType = {
  Encrypted: "encrypted",
  Plain: "plain",
  Sensitive: "sensitive",
  System: "system",
} as const;
/**
 * The type of this cosmos doc instance, if blank, assume secret.
 */
export type UpdateSharedEnvVariableEnvironmentType = ClosedEnum<
  typeof UpdateSharedEnvVariableEnvironmentType
>;

/**
 * environments this env variable targets
 */
export const UpdateSharedEnvVariableEnvironmentTarget = {
  Development: "development",
  Preview: "preview",
  Production: "production",
} as const;
/**
 * environments this env variable targets
 */
export type UpdateSharedEnvVariableEnvironmentTarget = ClosedEnum<
  typeof UpdateSharedEnvVariableEnvironmentTarget
>;

export type Updated = {
  /**
   * The date when the Shared Env Var was created.
   */
  created?: Date | undefined;
  /**
   * The name of the Shared Env Var.
   */
  key?: string | undefined;
  /**
   * The unique identifier of the owner (team) the Shared Env Var was created for.
   */
  ownerId?: string | null | undefined;
  /**
   * The unique identifier of the Shared Env Var.
   */
  id?: string | undefined;
  /**
   * The unique identifier of the user who created the Shared Env Var.
   */
  createdBy?: string | null | undefined;
  /**
   * The unique identifier of the user who deleted the Shared Env Var.
   */
  deletedBy?: string | null | undefined;
  /**
   * The unique identifier of the user who last updated the Shared Env Var.
   */
  updatedBy?: string | null | undefined;
  /**
   * Timestamp for when the Shared Env Var was created.
   */
  createdAt?: number | undefined;
  /**
   * Timestamp for when the Shared Env Var was (soft) deleted.
   */
  deletedAt?: number | undefined;
  /**
   * Timestamp for when the Shared Env Var was last updated.
   */
  updatedAt?: number | undefined;
  /**
   * The value of the Shared Env Var.
   */
  value?: string | undefined;
  /**
   * The unique identifiers of the projects which the Shared Env Var is linked to.
   */
  projectId?: Array<string> | undefined;
  /**
   * The type of this cosmos doc instance, if blank, assume secret.
   */
  type?: UpdateSharedEnvVariableEnvironmentType | undefined;
  /**
   * environments this env variable targets
   */
  target?: Array<UpdateSharedEnvVariableEnvironmentTarget> | undefined;
  /**
   * whether or not this env varible applies to custom environments
   */
  applyToAllCustomEnvironments?: boolean | undefined;
  /**
   * The custom environment IDs that this Shared Env Var is scoped to.
   */
  customEnvironmentIds?: Array<string> | undefined;
  /**
   * whether or not this env variable is decrypted
   */
  decrypted?: boolean | undefined;
  /**
   * A user provided comment that describes what this Shared Env Var is for.
   */
  comment?: string | undefined;
  /**
   * The last editor full name or username.
   */
  lastEditedByDisplayName?: string | undefined;
};

export const UpdateSharedEnvVariableValue2 = {
  Development: "development",
  Preview: "preview",
  Production: "production",
} as const;
export type UpdateSharedEnvVariableValue2 = ClosedEnum<
  typeof UpdateSharedEnvVariableValue2
>;

export type UpdateSharedEnvVariableValue =
  | string
  | Array<UpdateSharedEnvVariableValue2>;

export const UpdateSharedEnvVariableTarget2 = {
  Development: "development",
  Preview: "preview",
  Production: "production",
} as const;
export type UpdateSharedEnvVariableTarget2 = ClosedEnum<
  typeof UpdateSharedEnvVariableTarget2
>;

export const UpdateSharedEnvVariableTarget1 = {
  Development: "development",
  Preview: "preview",
  Production: "production",
} as const;
export type UpdateSharedEnvVariableTarget1 = ClosedEnum<
  typeof UpdateSharedEnvVariableTarget1
>;

export type UpdateSharedEnvVariableEnvironmentResponseTarget =
  | Array<UpdateSharedEnvVariableTarget1>
  | UpdateSharedEnvVariableTarget2;

export type UpdateSharedEnvVariableError = {
  code: string;
  message: string;
  key?: string | undefined;
  envVarId?: string | undefined;
  envVarKey?: string | undefined;
  action?: string | undefined;
  link?: string | undefined;
  value?: string | Array<UpdateSharedEnvVariableValue2> | undefined;
  gitBranch?: string | undefined;
  target?:
    | Array<UpdateSharedEnvVariableTarget1>
    | UpdateSharedEnvVariableTarget2
    | undefined;
  project?: string | undefined;
};

export type UpdateSharedEnvVariableFailed = {
  error: UpdateSharedEnvVariableError;
};

export type UpdateSharedEnvVariableResponseBody = {
  updated: Array<Updated>;
  failed: Array<UpdateSharedEnvVariableFailed>;
};

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

/** @internal */
export type ProjectIdUpdates$Outbound = {
  link?: Array<string> | undefined;
  unlink?: Array<string> | undefined;
};

/** @internal */
export const ProjectIdUpdates$outboundSchema: z.ZodType<
  ProjectIdUpdates$Outbound,
  z.ZodTypeDef,
  ProjectIdUpdates
> = z.object({
  link: z.array(z.string()).optional(),
  unlink: z.array(z.string()).optional(),
});

export function projectIdUpdatesToJSON(
  projectIdUpdates: ProjectIdUpdates,
): string {
  return JSON.stringify(
    ProjectIdUpdates$outboundSchema.parse(projectIdUpdates),
  );
}

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

/** @internal */
export type Updates$Outbound = {
  key?: string | undefined;
  value?: string | undefined;
  target?: Array<string> | undefined;
  projectId?: Array<string> | undefined;
  projectIdUpdates?: ProjectIdUpdates$Outbound | undefined;
  type?: string | undefined;
  comment?: string | undefined;
};

/** @internal */
export const Updates$outboundSchema: z.ZodType<
  Updates$Outbound,
  z.ZodTypeDef,
  Updates
> = z.object({
  key: z.string().optional(),
  value: z.string().optional(),
  target: z.array(UpdateSharedEnvVariableTarget$outboundSchema).optional(),
  projectId: z.array(z.string()).optional(),
  projectIdUpdates: z.lazy(() => ProjectIdUpdates$outboundSchema).optional(),
  type: UpdateSharedEnvVariableType$outboundSchema.optional(),
  comment: z.string().optional(),
});

export function updatesToJSON(updates: Updates): string {
  return JSON.stringify(Updates$outboundSchema.parse(updates));
}

/** @internal */
export type UpdateSharedEnvVariableRequestBody$Outbound = {
  updates: { [k: string]: Updates$Outbound };
};

/** @internal */
export const UpdateSharedEnvVariableRequestBody$outboundSchema: z.ZodType<
  UpdateSharedEnvVariableRequestBody$Outbound,
  z.ZodTypeDef,
  UpdateSharedEnvVariableRequestBody
> = z.object({
  updates: z.record(z.lazy(() => Updates$outboundSchema)),
});

export function updateSharedEnvVariableRequestBodyToJSON(
  updateSharedEnvVariableRequestBody: UpdateSharedEnvVariableRequestBody,
): string {
  return JSON.stringify(
    UpdateSharedEnvVariableRequestBody$outboundSchema.parse(
      updateSharedEnvVariableRequestBody,
    ),
  );
}

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

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

export function updateSharedEnvVariableRequestToJSON(
  updateSharedEnvVariableRequest: UpdateSharedEnvVariableRequest,
): string {
  return JSON.stringify(
    UpdateSharedEnvVariableRequest$outboundSchema.parse(
      updateSharedEnvVariableRequest,
    ),
  );
}

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

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

/** @internal */
export const Updated$inboundSchema: z.ZodType<Updated, z.ZodTypeDef, unknown> =
  z.object({
    created: types.optional(types.date()),
    key: types.optional(types.string()),
    ownerId: z.nullable(types.string()).optional(),
    id: types.optional(types.string()),
    createdBy: z.nullable(types.string()).optional(),
    deletedBy: z.nullable(types.string()).optional(),
    updatedBy: z.nullable(types.string()).optional(),
    createdAt: types.optional(types.number()),
    deletedAt: types.optional(types.number()),
    updatedAt: types.optional(types.number()),
    value: types.optional(types.string()),
    projectId: types.optional(z.array(types.string())),
    type: types.optional(UpdateSharedEnvVariableEnvironmentType$inboundSchema),
    target: types.optional(
      z.array(UpdateSharedEnvVariableEnvironmentTarget$inboundSchema),
    ),
    applyToAllCustomEnvironments: types.optional(types.boolean()),
    customEnvironmentIds: types.optional(z.array(types.string())),
    decrypted: types.optional(types.boolean()),
    comment: types.optional(types.string()),
    lastEditedByDisplayName: types.optional(types.string()),
  });

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

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

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

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

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

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

/** @internal */
export const UpdateSharedEnvVariableEnvironmentResponseTarget$inboundSchema:
  z.ZodType<
    UpdateSharedEnvVariableEnvironmentResponseTarget,
    z.ZodTypeDef,
    unknown
  > = smartUnion([
    z.array(UpdateSharedEnvVariableTarget1$inboundSchema),
    UpdateSharedEnvVariableTarget2$inboundSchema,
  ]);

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

/** @internal */
export const UpdateSharedEnvVariableError$inboundSchema: z.ZodType<
  UpdateSharedEnvVariableError,
  z.ZodTypeDef,
  unknown
> = z.object({
  code: types.string(),
  message: types.string(),
  key: types.optional(types.string()),
  envVarId: types.optional(types.string()),
  envVarKey: types.optional(types.string()),
  action: types.optional(types.string()),
  link: types.optional(types.string()),
  value: types.optional(
    smartUnion([
      types.string(),
      z.array(UpdateSharedEnvVariableValue2$inboundSchema),
    ]),
  ),
  gitBranch: types.optional(types.string()),
  target: types.optional(
    smartUnion([
      z.array(UpdateSharedEnvVariableTarget1$inboundSchema),
      UpdateSharedEnvVariableTarget2$inboundSchema,
    ]),
  ),
  project: types.optional(types.string()),
});

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

/** @internal */
export const UpdateSharedEnvVariableFailed$inboundSchema: z.ZodType<
  UpdateSharedEnvVariableFailed,
  z.ZodTypeDef,
  unknown
> = z.object({
  error: z.lazy(() => UpdateSharedEnvVariableError$inboundSchema),
});

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

/** @internal */
export const UpdateSharedEnvVariableResponseBody$inboundSchema: z.ZodType<
  UpdateSharedEnvVariableResponseBody,
  z.ZodTypeDef,
  unknown
> = z.object({
  updated: z.array(z.lazy(() => Updated$inboundSchema)),
  failed: z.array(z.lazy(() => UpdateSharedEnvVariableFailed$inboundSchema)),
});

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