/*
 * 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 { Flag, Flag$inboundSchema } from "./flag.js";
import {
  MarketplaceFlag,
  MarketplaceFlag$inboundSchema,
} from "./marketplaceflag.js";
import { SDKValidationError } from "./sdkvalidationerror.js";

/**
 * The state of the flags to retrieve. Defaults to `active`.
 */
export const QueryParamState = {
  Active: "active",
  Archived: "archived",
} as const;
/**
 * The state of the flags to retrieve. Defaults to `active`.
 */
export type QueryParamState = ClosedEnum<typeof QueryParamState>;

export type ListFlagsV2Request = {
  /**
   * The project id or name
   */
  projectIdOrName: string;
  /**
   * The state of the flags to retrieve. Defaults to `active`.
   */
  state?: QueryParamState | undefined;
  /**
   * Maximum number of flags to return.
   */
  limit?: number | undefined;
  /**
   * Pagination cursor to continue from.
   */
  cursor?: string | undefined;
  /**
   * Search flags by their slug or description. Case-insensitive.
   */
  search?: string | undefined;
  /**
   * Filter flags by tag. Repeat the parameter for multiple tags (all must match).
   */
  tags?: Array<string> | undefined;
  /**
   * Filter flags by the id of the entity that created them (a user or team id).
   */
  createdBy?: string | undefined;
  /**
   * Filter flags by maintainer user id. Repeat the parameter for multiple maintainers (any may match).
   */
  maintainerIds?: Array<string> | undefined;
  /**
   * Whether to include Marketplace experimentation items in the paginated response. Defaults to false.
   */
  includeMarketplaceFlags?: boolean | undefined;
  /**
   * 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 ListFlagsV2Pagination = {
  next: string | null;
};

export type ListFlagsV2Data = (Flag & { typeName: "flag" }) | MarketplaceFlag;

export type ListFlagsV2ResponseBody = {
  pagination: ListFlagsV2Pagination;
  data: Array<(Flag & { typeName: "flag" }) | MarketplaceFlag>;
};

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

/** @internal */
export type ListFlagsV2Request$Outbound = {
  projectIdOrName: string;
  state?: string | undefined;
  limit: number;
  cursor?: string | undefined;
  search?: string | undefined;
  tags?: Array<string> | undefined;
  createdBy?: string | undefined;
  maintainerIds?: Array<string> | undefined;
  includeMarketplaceFlags?: boolean | undefined;
  teamId?: string | undefined;
  slug?: string | undefined;
};

/** @internal */
export const ListFlagsV2Request$outboundSchema: z.ZodType<
  ListFlagsV2Request$Outbound,
  z.ZodTypeDef,
  ListFlagsV2Request
> = z.object({
  projectIdOrName: z.string(),
  state: QueryParamState$outboundSchema.optional(),
  limit: z.number().int().default(25),
  cursor: z.string().optional(),
  search: z.string().optional(),
  tags: z.array(z.string()).optional(),
  createdBy: z.string().optional(),
  maintainerIds: z.array(z.string()).optional(),
  includeMarketplaceFlags: z.boolean().optional(),
  teamId: z.string().optional(),
  slug: z.string().optional(),
});

export function listFlagsV2RequestToJSON(
  listFlagsV2Request: ListFlagsV2Request,
): string {
  return JSON.stringify(
    ListFlagsV2Request$outboundSchema.parse(listFlagsV2Request),
  );
}

/** @internal */
export const ListFlagsV2Pagination$inboundSchema: z.ZodType<
  ListFlagsV2Pagination,
  z.ZodTypeDef,
  unknown
> = z.object({
  next: types.nullable(types.string()),
});

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

/** @internal */
export const ListFlagsV2Data$inboundSchema: z.ZodType<
  ListFlagsV2Data,
  z.ZodTypeDef,
  unknown
> = z.union([
  Flag$inboundSchema.and(z.object({ typeName: z.literal("flag") })),
  MarketplaceFlag$inboundSchema,
]);

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

/** @internal */
export const ListFlagsV2ResponseBody$inboundSchema: z.ZodType<
  ListFlagsV2ResponseBody,
  z.ZodTypeDef,
  unknown
> = z.object({
  pagination: z.lazy(() => ListFlagsV2Pagination$inboundSchema),
  data: z.array(
    z.union([
      Flag$inboundSchema.and(z.object({ typeName: z.literal("flag") })),
      MarketplaceFlag$inboundSchema,
    ]),
  ),
});

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