import { z } from 'zod';

export const ItemType = {
  NotSet: 0,
  SingleUnit: 1,
  SalesUnit: 2,
  Set: 4,
  PackagingUnit: 8,
  Configurable: 16,
  Discount: 32,
  Percentaged: 64,
  FixedPrice: 128,
  Postage: 256,
  HandlingFee: 512,
  Voucher: 1024,
  ComponentList: 2048,
  Download: 4096,
  Streaming: 8192,
  ItemPrice: 16384,
  SingleUnitItemPrice: 16385,
  ComponentPrice: 32768,
  CustomPrice: 65536,
} as const;

const ALL_FLAGS = Object.values(ItemType).reduce<number>((a, b) => a | b, 0);

export const ItemTypeSchema = z
  .number()
  .int()
  .nonnegative()
  .refine((val) => (val & ~ALL_FLAGS) === 0, {
    message: 'Invalid ItemType: contains unknown flags',
  })
  .describe('Item type (bitflags)');

export type ItemType = z.infer<typeof ItemTypeSchema>;
