/*
 * 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 { Benefit, Benefit$inboundSchema } from "./benefit.js";
import {
  BenefitGrantCustomProperties,
  BenefitGrantCustomProperties$inboundSchema,
} from "./benefitgrantcustomproperties.js";
import {
  BenefitGrantDiscordProperties,
  BenefitGrantDiscordProperties$inboundSchema,
} from "./benefitgrantdiscordproperties.js";
import {
  BenefitGrantDownloadablesProperties,
  BenefitGrantDownloadablesProperties$inboundSchema,
} from "./benefitgrantdownloadablesproperties.js";
import {
  BenefitGrantError,
  BenefitGrantError$inboundSchema,
} from "./benefitgranterror.js";
import {
  BenefitGrantFeatureFlagProperties,
  BenefitGrantFeatureFlagProperties$inboundSchema,
} from "./benefitgrantfeatureflagproperties.js";
import {
  BenefitGrantGitHubRepositoryProperties,
  BenefitGrantGitHubRepositoryProperties$inboundSchema,
} from "./benefitgrantgithubrepositoryproperties.js";
import {
  BenefitGrantLicenseKeysProperties,
  BenefitGrantLicenseKeysProperties$inboundSchema,
} from "./benefitgrantlicensekeysproperties.js";
import {
  BenefitGrantSlackSharedChannelProperties,
  BenefitGrantSlackSharedChannelProperties$inboundSchema,
} from "./benefitgrantslacksharedchannelproperties.js";
import { Customer, Customer$inboundSchema } from "./customer.js";
import { Member, Member$inboundSchema } from "./member.js";

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

export type BenefitGrant = {
  /**
   * Creation timestamp of the object.
   */
  createdAt: Date;
  /**
   * Last modification timestamp of the object.
   */
  modifiedAt: Date | null;
  /**
   * The ID of the grant.
   */
  id: string;
  /**
   * The timestamp when the benefit was granted. If `None`, the benefit is not granted.
   */
  grantedAt?: Date | null | undefined;
  /**
   * Whether the benefit is granted.
   */
  isGranted: boolean;
  /**
   * The timestamp when the benefit was revoked. If `None`, the benefit is not revoked.
   */
  revokedAt?: Date | null | undefined;
  /**
   * Whether the benefit is revoked.
   */
  isRevoked: boolean;
  /**
   * The ID of the subscription that granted this benefit.
   */
  subscriptionId: string | null;
  /**
   * The ID of the order that granted this benefit.
   */
  orderId: string | null;
  /**
   * The ID of the customer concerned by this grant.
   */
  customerId: string;
  /**
   * The ID of the member concerned by this grant.
   */
  memberId?: string | null | undefined;
  /**
   * The ID of the benefit concerned by this grant.
   */
  benefitId: string;
  /**
   * The error information if the benefit grant failed with an unrecoverable error.
   */
  error?: BenefitGrantError | null | undefined;
  customer: Customer;
  member?: Member | null | undefined;
  benefit: Benefit;
  properties:
    | BenefitGrantDiscordProperties
    | BenefitGrantGitHubRepositoryProperties
    | BenefitGrantDownloadablesProperties
    | BenefitGrantLicenseKeysProperties
    | BenefitGrantCustomProperties
    | BenefitGrantFeatureFlagProperties
    | BenefitGrantSlackSharedChannelProperties;
};

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

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

/** @internal */
export const BenefitGrant$inboundSchema: z.ZodMiniType<BenefitGrant, unknown> =
  z.pipe(
    z.object({
      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))),
      ),
      id: z.string(),
      granted_at: z.optional(
        z.nullable(z.pipe(
          z.iso.datetime({ offset: true }),
          z.transform(v => new Date(v)),
        )),
      ),
      is_granted: z.boolean(),
      revoked_at: z.optional(
        z.nullable(z.pipe(
          z.iso.datetime({ offset: true }),
          z.transform(v => new Date(v)),
        )),
      ),
      is_revoked: z.boolean(),
      subscription_id: z.nullable(z.string()),
      order_id: z.nullable(z.string()),
      customer_id: z.string(),
      member_id: z.optional(z.nullable(z.string())),
      benefit_id: z.string(),
      error: z.optional(z.nullable(BenefitGrantError$inboundSchema)),
      customer: Customer$inboundSchema,
      member: z.optional(z.nullable(Member$inboundSchema)),
      benefit: Benefit$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",
        "is_granted": "isGranted",
        "revoked_at": "revokedAt",
        "is_revoked": "isRevoked",
        "subscription_id": "subscriptionId",
        "order_id": "orderId",
        "customer_id": "customerId",
        "member_id": "memberId",
        "benefit_id": "benefitId",
      });
    }),
  );

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