/*
 * 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 {
  Link,
  Link$inboundSchema,
  Link$Outbound,
  Link$outboundSchema,
} from "./link.js";

/**
 * The kind of event the metric tracks
 */
export const MetricV2RepKind = {
  Pageview: "pageview",
  Click: "click",
  Custom: "custom",
  Trace: "trace",
} as const;
/**
 * The kind of event the metric tracks
 */
export type MetricV2RepKind = ClosedEnum<typeof MetricV2RepKind>;

/**
 * The type of unit aggregation to use for the metric
 */
export const MetricV2RepUnitAggregationType = {
  Sum: "sum",
  Average: "average",
  CountDistinct: "count_distinct",
} as const;
/**
 * The type of unit aggregation to use for the metric
 */
export type MetricV2RepUnitAggregationType = ClosedEnum<
  typeof MetricV2RepUnitAggregationType
>;

/**
 * The method for analyzing metric events
 */
export const MetricV2RepAnalysisType = {
  Mean: "mean",
  Percentile: "percentile",
} as const;
/**
 * The method for analyzing metric events
 */
export type MetricV2RepAnalysisType = ClosedEnum<
  typeof MetricV2RepAnalysisType
>;

export type MetricV2Rep = {
  /**
   * The metric key
   */
  key: string;
  /**
   * The version ID of the metric
   */
  versionId?: string | undefined;
  /**
   * The metric name
   */
  name: string;
  /**
   * The kind of event the metric tracks
   */
  kind: MetricV2RepKind;
  /**
   * For custom metrics, whether to track numeric changes in value against a baseline (<code>true</code>) or to track a conversion when an end user takes an action (<code>false</code>).
   */
  isNumeric?: boolean | undefined;
  /**
   * The type of unit aggregation to use for the metric
   */
  unitAggregationType?: MetricV2RepUnitAggregationType | undefined;
  /**
   * The method for analyzing metric events
   */
  analysisType?: MetricV2RepAnalysisType | undefined;
  /**
   * The event key sent with the metric. Only relevant for custom metrics.
   */
  eventKey?: string | undefined;
  /**
   * The context kind used as the analysis unit for this metric in the experiment iteration
   */
  analysisUnit?: string | undefined;
  /**
   * The location and content type of related resources
   */
  links: { [k: string]: Link };
};

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

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

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

/** @internal */
export const MetricV2Rep$inboundSchema: z.ZodType<
  MetricV2Rep,
  z.ZodTypeDef,
  unknown
> = z.object({
  key: z.string(),
  _versionId: z.string().optional(),
  name: z.string(),
  kind: MetricV2RepKind$inboundSchema,
  isNumeric: z.boolean().optional(),
  unitAggregationType: MetricV2RepUnitAggregationType$inboundSchema.optional(),
  analysisType: MetricV2RepAnalysisType$inboundSchema.optional(),
  eventKey: z.string().optional(),
  analysisUnit: z.string().optional(),
  _links: z.record(Link$inboundSchema),
}).transform((v) => {
  return remap$(v, {
    "_versionId": "versionId",
    "_links": "links",
  });
});
/** @internal */
export type MetricV2Rep$Outbound = {
  key: string;
  _versionId?: string | undefined;
  name: string;
  kind: string;
  isNumeric?: boolean | undefined;
  unitAggregationType?: string | undefined;
  analysisType?: string | undefined;
  eventKey?: string | undefined;
  analysisUnit?: string | undefined;
  _links: { [k: string]: Link$Outbound };
};

/** @internal */
export const MetricV2Rep$outboundSchema: z.ZodType<
  MetricV2Rep$Outbound,
  z.ZodTypeDef,
  MetricV2Rep
> = z.object({
  key: z.string(),
  versionId: z.string().optional(),
  name: z.string(),
  kind: MetricV2RepKind$outboundSchema,
  isNumeric: z.boolean().optional(),
  unitAggregationType: MetricV2RepUnitAggregationType$outboundSchema.optional(),
  analysisType: MetricV2RepAnalysisType$outboundSchema.optional(),
  eventKey: z.string().optional(),
  analysisUnit: z.string().optional(),
  links: z.record(Link$outboundSchema),
}).transform((v) => {
  return remap$(v, {
    versionId: "_versionId",
    links: "_links",
  });
});

export function metricV2RepToJSON(metricV2Rep: MetricV2Rep): string {
  return JSON.stringify(MetricV2Rep$outboundSchema.parse(metricV2Rep));
}
export function metricV2RepFromJSON(
  jsonString: string,
): SafeParseResult<MetricV2Rep, SDKValidationError> {
  return safeParse(
    jsonString,
    (x) => MetricV2Rep$inboundSchema.parse(JSON.parse(x)),
    `Failed to parse 'MetricV2Rep' from JSON`,
  );
}
