const allergenMap = {
  eggs: 1,
  peanuts: 2,
  shellfish: 4,
  strawberries: 8,
  tomatoes: 16,
  chocolate: 32,
  pollen: 64,
  cats: 128,
} as const

type Allergen = keyof typeof allergenMap

export class Allergies {
  constructor(private _allergenIndex: number) {}

  public list(): Allergen[] {
    return Object.entries(allergenMap)
      .filter(([_, weight]) => (this._allergenIndex & weight) !== 0)
      .map(([allergen]) => allergen as Allergen)
  }

  public allergicTo(allergen: Allergen): boolean {
    return this.list().includes(allergen)
  }
}
