#lang racket/base

(require
  "../../base-generation/distribution.rkt")

; Model and generation of sexes
(provide
  ; Sexes collection functions
  jsexpr->sexes
  sexes-ref
  ; Sex structure accessors
  sex-name sex-context sex-mother?
  ; Generation of gender
  sex-generate-gender-name
  )

; Sexes structure
(struct sex
  (name
   context
   mother?
   gender-distribution))

; Generation of gender from sex
(define (sex-generate-gender-name sx)
  (distribution-generate (sex-gender-distribution sx)))

; Deserialisation from a json object
(define (jsexpr->sexes js)
  (define ret (make-hash))
  (map
    (lambda (x)
      (define name (hash-ref x 'name))
      (define mother? (hash-ref x 'mother?))
      (hash-set! ret name
        (sex
          name
          (hash-ref x 'context)
          (if (equal? mother? "maybe") 'maybe mother?)
          (jsexpr->distribution/object (hash-ref x 'gender-distribution) #:translator symbol->string))))
    js)
  ret)

; Retrieval from the name
(define (sexes-ref sx name)
  (hash-ref sx name))
