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

import * as z from "zod/v4-mini";
import { remap as remap$ } from "../../lib/primitives.js";
import { safeParse } from "../../lib/schemas.js";
import { Result as SafeParseResult } from "../../types/fp.js";
import { smartUnion } from "../../types/smartUnion.js";
import { SDKValidationError } from "../errors/sdkvalidationerror.js";
import {
  BenefitGrantCustomProperties,
  BenefitGrantCustomProperties$inboundSchema,
  BenefitGrantCustomProperties$Outbound,
  BenefitGrantCustomProperties$outboundSchema,
} from "./benefitgrantcustomproperties.js";
import {
  BenefitGrantDiscordProperties,
  BenefitGrantDiscordProperties$inboundSchema,
  BenefitGrantDiscordProperties$Outbound,
  BenefitGrantDiscordProperties$outboundSchema,
} from "./benefitgrantdiscordproperties.js";
import {
  BenefitGrantDownloadablesProperties,
  BenefitGrantDownloadablesProperties$inboundSchema,
  BenefitGrantDownloadablesProperties$Outbound,
  BenefitGrantDownloadablesProperties$outboundSchema,
} from "./benefitgrantdownloadablesproperties.js";
import {
  BenefitGrantFeatureFlagProperties,
  BenefitGrantFeatureFlagProperties$inboundSchema,
  BenefitGrantFeatureFlagProperties$Outbound,
  BenefitGrantFeatureFlagProperties$outboundSchema,
} from "./benefitgrantfeatureflagproperties.js";
import {
  BenefitGrantGitHubRepositoryProperties,
  BenefitGrantGitHubRepositoryProperties$inboundSchema,
  BenefitGrantGitHubRepositoryProperties$Outbound,
  BenefitGrantGitHubRepositoryProperties$outboundSchema,
} from "./benefitgrantgithubrepositoryproperties.js";
import {
  BenefitGrantLicenseKeysProperties,
  BenefitGrantLicenseKeysProperties$inboundSchema,
  BenefitGrantLicenseKeysProperties$Outbound,
  BenefitGrantLicenseKeysProperties$outboundSchema,
} from "./benefitgrantlicensekeysproperties.js";
import {
  BenefitGrantSlackSharedChannelProperties,
  BenefitGrantSlackSharedChannelProperties$inboundSchema,
  BenefitGrantSlackSharedChannelProperties$Outbound,
  BenefitGrantSlackSharedChannelProperties$outboundSchema,
} from "./benefitgrantslacksharedchannelproperties.js";
import {
  BenefitType,
  BenefitType$inboundSchema,
  BenefitType$outboundSchema,
} from "./benefittype.js";
import {
  MetadataOutputType,
  MetadataOutputType$inboundSchema,
  MetadataOutputType$Outbound,
  MetadataOutputType$outboundSchema,
} from "./metadataoutputtype.js";

export type CustomerStateBenefitGrantProperties =
  | BenefitGrantDiscordProperties
  | BenefitGrantGitHubRepositoryProperties
  | BenefitGrantDownloadablesProperties
  | BenefitGrantLicenseKeysProperties
  | BenefitGrantCustomProperties
  | BenefitGrantFeatureFlagProperties
  | BenefitGrantSlackSharedChannelProperties;

/**
 * An active benefit grant for a customer.
 */
export type CustomerStateBenefitGrant = {
  /**
   * The ID of the grant.
   */
  id: string;
  /**
   * Creation timestamp of the object.
   */
  createdAt: Date;
  /**
   * Last modification timestamp of the object.
   */
  modifiedAt: Date | null;
  /**
   * The timestamp when the benefit was granted.
   */
  grantedAt: Date;
  /**
   * The ID of the benefit concerned by this grant.
   */
  benefitId: string;
  benefitType: BenefitType;
  benefitMetadata: { [k: string]: MetadataOutputType };
  properties:
    | BenefitGrantDiscordProperties
    | BenefitGrantGitHubRepositoryProperties
    | BenefitGrantDownloadablesProperties
    | BenefitGrantLicenseKeysProperties
    | BenefitGrantCustomProperties
    | BenefitGrantFeatureFlagProperties
    | BenefitGrantSlackSharedChannelProperties;
};

/** @internal */
export const CustomerStateBenefitGrantProperties$inboundSchema: z.ZodMiniType<
  CustomerStateBenefitGrantProperties,
  unknown
> = smartUnion([
  BenefitGrantDiscordProperties$inboundSchema,
  BenefitGrantGitHubRepositoryProperties$inboundSchema,
  BenefitGrantDownloadablesProperties$inboundSchema,
  BenefitGrantLicenseKeysProperties$inboundSchema,
  BenefitGrantCustomProperties$inboundSchema,
  BenefitGrantFeatureFlagProperties$inboundSchema,
  BenefitGrantSlackSharedChannelProperties$inboundSchema,
]);
/** @internal */
export type CustomerStateBenefitGrantProperties$Outbound =
  | BenefitGrantDiscordProperties$Outbound
  | BenefitGrantGitHubRepositoryProperties$Outbound
  | BenefitGrantDownloadablesProperties$Outbound
  | BenefitGrantLicenseKeysProperties$Outbound
  | BenefitGrantCustomProperties$Outbound
  | BenefitGrantFeatureFlagProperties$Outbound
  | BenefitGrantSlackSharedChannelProperties$Outbound;

/** @internal */
export const CustomerStateBenefitGrantProperties$outboundSchema: z.ZodMiniType<
  CustomerStateBenefitGrantProperties$Outbound,
  CustomerStateBenefitGrantProperties
> = smartUnion([
  BenefitGrantDiscordProperties$outboundSchema,
  BenefitGrantGitHubRepositoryProperties$outboundSchema,
  BenefitGrantDownloadablesProperties$outboundSchema,
  BenefitGrantLicenseKeysProperties$outboundSchema,
  BenefitGrantCustomProperties$outboundSchema,
  BenefitGrantFeatureFlagProperties$outboundSchema,
  BenefitGrantSlackSharedChannelProperties$outboundSchema,
]);

export function customerStateBenefitGrantPropertiesToJSON(
  customerStateBenefitGrantProperties: CustomerStateBenefitGrantProperties,
): string {
  return JSON.stringify(
    CustomerStateBenefitGrantProperties$outboundSchema.parse(
      customerStateBenefitGrantProperties,
    ),
  );
}
export function customerStateBenefitGrantPropertiesFromJSON(
  jsonString: string,
): SafeParseResult<CustomerStateBenefitGrantProperties, SDKValidationError> {
  return safeParse(
    jsonString,
    (x) =>
      CustomerStateBenefitGrantProperties$inboundSchema.parse(JSON.parse(x)),
    `Failed to parse 'CustomerStateBenefitGrantProperties' from JSON`,
  );
}

/** @internal */
export const CustomerStateBenefitGrant$inboundSchema: z.ZodMiniType<
  CustomerStateBenefitGrant,
  unknown
> = z.pipe(
  z.object({
    id: z.string(),
    created_at: z.pipe(
      z.iso.datetime({ offset: true }),
      z.transform(v => new Date(v)),
    ),
    modified_at: z.nullable(
      z.pipe(z.iso.datetime({ offset: true }), z.transform(v => new Date(v))),
    ),
    granted_at: z.pipe(
      z.iso.datetime({ offset: true }),
      z.transform(v => new Date(v)),
    ),
    benefit_id: z.string(),
    benefit_type: BenefitType$inboundSchema,
    benefit_metadata: z.record(z.string(), MetadataOutputType$inboundSchema),
    properties: smartUnion([
      BenefitGrantDiscordProperties$inboundSchema,
      BenefitGrantGitHubRepositoryProperties$inboundSchema,
      BenefitGrantDownloadablesProperties$inboundSchema,
      BenefitGrantLicenseKeysProperties$inboundSchema,
      BenefitGrantCustomProperties$inboundSchema,
      BenefitGrantFeatureFlagProperties$inboundSchema,
      BenefitGrantSlackSharedChannelProperties$inboundSchema,
    ]),
  }),
  z.transform((v) => {
    return remap$(v, {
      "created_at": "createdAt",
      "modified_at": "modifiedAt",
      "granted_at": "grantedAt",
      "benefit_id": "benefitId",
      "benefit_type": "benefitType",
      "benefit_metadata": "benefitMetadata",
    });
  }),
);
/** @internal */
export type CustomerStateBenefitGrant$Outbound = {
  id: string;
  created_at: string;
  modified_at: string | null;
  granted_at: string;
  benefit_id: string;
  benefit_type: string;
  benefit_metadata: { [k: string]: MetadataOutputType$Outbound };
  properties:
    | BenefitGrantDiscordProperties$Outbound
    | BenefitGrantGitHubRepositoryProperties$Outbound
    | BenefitGrantDownloadablesProperties$Outbound
    | BenefitGrantLicenseKeysProperties$Outbound
    | BenefitGrantCustomProperties$Outbound
    | BenefitGrantFeatureFlagProperties$Outbound
    | BenefitGrantSlackSharedChannelProperties$Outbound;
};

/** @internal */
export const CustomerStateBenefitGrant$outboundSchema: z.ZodMiniType<
  CustomerStateBenefitGrant$Outbound,
  CustomerStateBenefitGrant
> = z.pipe(
  z.object({
    id: z.string(),
    createdAt: z.pipe(z.date(), z.transform(v => v.toISOString())),
    modifiedAt: z.nullable(z.pipe(z.date(), z.transform(v => v.toISOString()))),
    grantedAt: z.pipe(z.date(), z.transform(v => v.toISOString())),
    benefitId: z.string(),
    benefitType: BenefitType$outboundSchema,
    benefitMetadata: z.record(z.string(), MetadataOutputType$outboundSchema),
    properties: smartUnion([
      BenefitGrantDiscordProperties$outboundSchema,
      BenefitGrantGitHubRepositoryProperties$outboundSchema,
      BenefitGrantDownloadablesProperties$outboundSchema,
      BenefitGrantLicenseKeysProperties$outboundSchema,
      BenefitGrantCustomProperties$outboundSchema,
      BenefitGrantFeatureFlagProperties$outboundSchema,
      BenefitGrantSlackSharedChannelProperties$outboundSchema,
    ]),
  }),
  z.transform((v) => {
    return remap$(v, {
      createdAt: "created_at",
      modifiedAt: "modified_at",
      grantedAt: "granted_at",
      benefitId: "benefit_id",
      benefitType: "benefit_type",
      benefitMetadata: "benefit_metadata",
    });
  }),
);

export function customerStateBenefitGrantToJSON(
  customerStateBenefitGrant: CustomerStateBenefitGrant,
): string {
  return JSON.stringify(
    CustomerStateBenefitGrant$outboundSchema.parse(customerStateBenefitGrant),
  );
}
export function customerStateBenefitGrantFromJSON(
  jsonString: string,
): SafeParseResult<CustomerStateBenefitGrant, SDKValidationError> {
  return safeParse(
    jsonString,
    (x) => CustomerStateBenefitGrant$inboundSchema.parse(JSON.parse(x)),
    `Failed to parse 'CustomerStateBenefitGrant' from JSON`,
  );
}
