#lang racket/base

(require
  "random.rkt")

; Category generator
; Generate collections of items from named lists
(provide
  jsexpr->category-generator
  category-generate
  category-generate-list
  category-generate-dictionary
  )

; Read from a json expresion
; The model is directly the structure read from json (which is a hash)
(define (jsexpr->category-generator js)
  js)

; Generate one element from a given category
(define (category-generate gen (cat #f))
  (random:from-list
    (if cat
        (hash-ref gen cat)
        (cdr (random:from-hash gen)))))

; Generate a list of n elements from a category
(define (category-generate-list gen cat n)
  (random:sublist (hash-ref gen cat) n))

; Generate a dictionary of category => item
(define (category-generate-dictionary gen)
  (let ((ret (make-hash)))
    (hash-for-each
      gen
      (lambda (k v)
        (hash-set! ret k (random:from-list v))))
    ret))
