#include <stdint.h>
#include <stdlib.h>
#include "el_runtime.h"

el_val_t str_ends(el_val_t s, el_val_t suf);
el_val_t str_last_char(el_val_t s);
el_val_t str_last2(el_val_t s);
el_val_t str_last3(el_val_t s);
el_val_t str_drop_last(el_val_t s, el_val_t n);
el_val_t is_vowel(el_val_t c);
el_val_t morph_apply_suffix(el_val_t base, el_val_t suffix);
el_val_t en_irregular_plural(el_val_t word);
el_val_t en_irregular_singular(el_val_t word);
el_val_t en_irregular_verb(el_val_t base);
el_val_t en_verb_3sg(el_val_t base);
el_val_t en_should_double_final(el_val_t base);
el_val_t en_verb_past(el_val_t base);
el_val_t en_verb_gerund(el_val_t base);
el_val_t en_pluralize_regular(el_val_t singular);
el_val_t en_verb_form(el_val_t base, el_val_t tense, el_val_t person, el_val_t number);
el_val_t agree_determiner(el_val_t det, el_val_t noun);
el_val_t morph_pluralize(el_val_t noun, el_val_t profile);
el_val_t morph_map_canonical(el_val_t verb, el_val_t code);
el_val_t morph_conjugate(el_val_t verb, el_val_t tense, el_val_t person, el_val_t number, el_val_t profile);
el_val_t morph_inflect(el_val_t word, el_val_t features, el_val_t profile);
el_val_t pluralize(el_val_t singular);
el_val_t singularize(el_val_t plural);
el_val_t verb_form(el_val_t base, el_val_t tense, el_val_t person, el_val_t number);
el_val_t irregular_plural(el_val_t word);
el_val_t irregular_singular(el_val_t word);
el_val_t uga_str_ends(el_val_t s, el_val_t suf);
el_val_t uga_str_len(el_val_t s);
el_val_t uga_str_drop_last(el_val_t s, el_val_t n);
el_val_t uga_slot(el_val_t person, el_val_t number);
el_val_t uga_slot_g(el_val_t person, el_val_t gender, el_val_t number);
el_val_t uga_kn_perfect(el_val_t slot);
el_val_t uga_kn_imperfect(el_val_t slot);
el_val_t uga_is_copula(el_val_t verb);
el_val_t uga_conjugate_copula(el_val_t tense, el_val_t slot);
el_val_t uga_hlk_perfect(el_val_t slot);
el_val_t uga_hlk_imperfect(el_val_t slot);
el_val_t uga_ray_perfect(el_val_t slot);
el_val_t uga_ray_imperfect(el_val_t slot);
el_val_t uga_amr_perfect(el_val_t slot);
el_val_t uga_amr_imperfect(el_val_t slot);
el_val_t uga_generic_perfect(el_val_t base3sg, el_val_t slot);
el_val_t uga_generic_imperfect(el_val_t base3sg, el_val_t slot);
el_val_t uga_known_verb(el_val_t verb, el_val_t tense, el_val_t slot);
el_val_t uga_conjugate(el_val_t verb, el_val_t tense, el_val_t person, el_val_t number);
el_val_t uga_strip_nom(el_val_t noun);
el_val_t uga_is_fem(el_val_t noun);
el_val_t uga_decline(el_val_t noun, el_val_t gram_case, el_val_t number);
el_val_t uga_noun_phrase(el_val_t noun, el_val_t gram_case, el_val_t number, el_val_t definite);
el_val_t uga_map_canonical(el_val_t verb);

el_val_t uga_str_ends(el_val_t s, el_val_t suf) {
  return str_ends_with(s, suf);
  return 0;
}

el_val_t uga_str_len(el_val_t s) {
  return str_len(s);
  return 0;
}

el_val_t uga_str_drop_last(el_val_t s, el_val_t n) {
  el_val_t len = str_len(s);
  if (n >= len) {
    return EL_STR("");
  }
  return str_slice(s, 0, (len - n));
  return 0;
}

el_val_t uga_slot(el_val_t person, el_val_t number) {
  if (str_eq(person, EL_STR("first"))) {
    if (str_eq(number, EL_STR("plural"))) {
      return 4;
    }
    return 0;
  }
  if (str_eq(person, EL_STR("second"))) {
    return 1;
  }
  if (str_eq(number, EL_STR("plural"))) {
    return 5;
  }
  return 2;
  return 0;
}

el_val_t uga_slot_g(el_val_t person, el_val_t gender, el_val_t number) {
  el_val_t base = uga_slot(person, number);
  if (str_eq(person, EL_STR("third"))) {
    if (str_eq(number, EL_STR("singular"))) {
      if (str_eq(gender, EL_STR("f"))) {
        return 3;
      }
    }
  }
  return base;
  return 0;
}

el_val_t uga_kn_perfect(el_val_t slot) {
  if (slot == 0) {
    return EL_STR("k\xc4\x81ntu");
  }
  if (slot == 1) {
    return EL_STR("k\xc4\x81nta");
  }
  if (slot == 2) {
    return EL_STR("k\xc4\x81na");
  }
  if (slot == 3) {
    return EL_STR("k\xc4\x81nat");
  }
  if (slot == 4) {
    return EL_STR("k\xc4\x81nnu");
  }
  return EL_STR("k\xc4\x81nu");
  return 0;
}

el_val_t uga_kn_imperfect(el_val_t slot) {
  if (slot == 0) {
    return EL_STR("\xca\xbc""ak\xc5\xabnu");
  }
  if (slot == 1) {
    return EL_STR("tak\xc5\xabnu");
  }
  if (slot == 2) {
    return EL_STR("yak\xc5\xabnu");
  }
  if (slot == 3) {
    return EL_STR("tak\xc5\xabnu");
  }
  if (slot == 4) {
    return EL_STR("nak\xc5\xabnu");
  }
  return EL_STR("yak\xc5\xabnuna");
  return 0;
}

el_val_t uga_is_copula(el_val_t verb) {
  if (str_eq(verb, EL_STR("kn"))) {
    return 1;
  }
  if (str_eq(verb, EL_STR("k\xc4\x81na"))) {
    return 1;
  }
  if (str_eq(verb, EL_STR("be"))) {
    return 1;
  }
  return 0;
  return 0;
}

el_val_t uga_conjugate_copula(el_val_t tense, el_val_t slot) {
  if (str_eq(tense, EL_STR("perfect"))) {
    return uga_kn_perfect(slot);
  }
  return uga_kn_imperfect(slot);
  return 0;
}

el_val_t uga_hlk_perfect(el_val_t slot) {
  if (slot == 0) {
    return EL_STR("halaktu");
  }
  if (slot == 1) {
    return EL_STR("halakta");
  }
  if (slot == 2) {
    return EL_STR("halaka");
  }
  if (slot == 3) {
    return EL_STR("halakat");
  }
  if (slot == 4) {
    return EL_STR("halaknu");
  }
  return EL_STR("halaku");
  return 0;
}

el_val_t uga_hlk_imperfect(el_val_t slot) {
  if (slot == 0) {
    return EL_STR("\xca\xbc""ahluku");
  }
  if (slot == 1) {
    return EL_STR("tahluku");
  }
  if (slot == 2) {
    return EL_STR("yahluku");
  }
  if (slot == 3) {
    return EL_STR("tahluku");
  }
  if (slot == 4) {
    return EL_STR("nahluku");
  }
  return EL_STR("yahlukuna");
  return 0;
}

el_val_t uga_ray_perfect(el_val_t slot) {
  if (slot == 0) {
    return EL_STR("ra\xca\xbc""aytu");
  }
  if (slot == 1) {
    return EL_STR("ra\xca\xbc""ayta");
  }
  if (slot == 2) {
    return EL_STR("ra\xca\xbc""aya");
  }
  if (slot == 3) {
    return EL_STR("ra\xca\xbc""ayat");
  }
  if (slot == 4) {
    return EL_STR("ra\xca\xbc""aynu");
  }
  return EL_STR("ra\xca\xbc""ayu");
  return 0;
}

el_val_t uga_ray_imperfect(el_val_t slot) {
  if (slot == 0) {
    return EL_STR("\xca\xbc""ar\xca\xbc\xc4\x81");
  }
  if (slot == 1) {
    return EL_STR("tar\xca\xbc\xc4\x81");
  }
  if (slot == 2) {
    return EL_STR("yar\xca\xbc\xc4\x81");
  }
  if (slot == 3) {
    return EL_STR("tar\xca\xbc\xc4\x81");
  }
  if (slot == 4) {
    return EL_STR("nar\xca\xbc\xc4\x81");
  }
  return EL_STR("yar\xca\xbc""ayna");
  return 0;
}

el_val_t uga_amr_perfect(el_val_t slot) {
  if (slot == 0) {
    return EL_STR("\xca\xbc""amartu");
  }
  if (slot == 1) {
    return EL_STR("\xca\xbc""amarta");
  }
  if (slot == 2) {
    return EL_STR("\xca\xbc""amara");
  }
  if (slot == 3) {
    return EL_STR("\xca\xbc""amarat");
  }
  if (slot == 4) {
    return EL_STR("\xca\xbc""amarnu");
  }
  return EL_STR("\xca\xbc""amaru");
  return 0;
}

el_val_t uga_amr_imperfect(el_val_t slot) {
  if (slot == 0) {
    return EL_STR("\xca\xbc""a\xca\xbcmuru");
  }
  if (slot == 1) {
    return EL_STR("ta\xca\xbcmuru");
  }
  if (slot == 2) {
    return EL_STR("ya\xca\xbcmuru");
  }
  if (slot == 3) {
    return EL_STR("ta\xca\xbcmuru");
  }
  if (slot == 4) {
    return EL_STR("na\xca\xbcmuru");
  }
  return EL_STR("ya\xca\xbcmuruna");
  return 0;
}

el_val_t uga_generic_perfect(el_val_t base3sg, el_val_t slot) {
  if (slot == 0) {
    return el_str_concat(base3sg, EL_STR("tu"));
  }
  if (slot == 1) {
    return el_str_concat(base3sg, EL_STR("ta"));
  }
  if (slot == 2) {
    return base3sg;
  }
  if (slot == 3) {
    return el_str_concat(base3sg, EL_STR("at"));
  }
  if (slot == 4) {
    return el_str_concat(base3sg, EL_STR("nu"));
  }
  return el_str_concat(base3sg, EL_STR("u"));
  return 0;
}

el_val_t uga_generic_imperfect(el_val_t base3sg, el_val_t slot) {
  if (slot == 0) {
    return el_str_concat(EL_STR("\xca\xbc""a"), base3sg);
  }
  if (slot == 1) {
    return el_str_concat(EL_STR("ta"), base3sg);
  }
  if (slot == 2) {
    return el_str_concat(EL_STR("ya"), base3sg);
  }
  if (slot == 3) {
    return el_str_concat(EL_STR("ta"), base3sg);
  }
  if (slot == 4) {
    return el_str_concat(EL_STR("na"), base3sg);
  }
  return el_str_concat(el_str_concat(EL_STR("ya"), base3sg), EL_STR("una"));
  return 0;
}

el_val_t uga_known_verb(el_val_t verb, el_val_t tense, el_val_t slot) {
  if (str_eq(verb, EL_STR("kn"))) {
    return uga_conjugate_copula(tense, slot);
  }
  if (str_eq(verb, EL_STR("k\xc4\x81na"))) {
    return uga_conjugate_copula(tense, slot);
  }
  if (str_eq(verb, EL_STR("hlk"))) {
    if (str_eq(tense, EL_STR("perfect"))) {
      return uga_hlk_perfect(slot);
    }
    return uga_hlk_imperfect(slot);
  }
  if (str_eq(verb, EL_STR("halaka"))) {
    if (str_eq(tense, EL_STR("perfect"))) {
      return uga_hlk_perfect(slot);
    }
    return uga_hlk_imperfect(slot);
  }
  if (str_eq(verb, EL_STR("r\xca\xbcy"))) {
    if (str_eq(tense, EL_STR("perfect"))) {
      return uga_ray_perfect(slot);
    }
    return uga_ray_imperfect(slot);
  }
  if (str_eq(verb, EL_STR("ra\xca\xbc""aya"))) {
    if (str_eq(tense, EL_STR("perfect"))) {
      return uga_ray_perfect(slot);
    }
    return uga_ray_imperfect(slot);
  }
  if (str_eq(verb, EL_STR("\xca\xbcmr"))) {
    if (str_eq(tense, EL_STR("perfect"))) {
      return uga_amr_perfect(slot);
    }
    return uga_amr_imperfect(slot);
  }
  if (str_eq(verb, EL_STR("\xca\xbc""amara"))) {
    if (str_eq(tense, EL_STR("perfect"))) {
      return uga_amr_perfect(slot);
    }
    return uga_amr_imperfect(slot);
  }
  return EL_STR("");
  return 0;
}

el_val_t uga_conjugate(el_val_t verb, el_val_t tense, el_val_t person, el_val_t number) {
  el_val_t slot = uga_slot(person, number);
  if (uga_is_copula(verb)) {
    return uga_conjugate_copula(tense, slot);
  }
  el_val_t known = uga_known_verb(verb, tense, slot);
  if (!str_eq(known, EL_STR(""))) {
    return known;
  }
  return verb;
  return 0;
}

el_val_t uga_strip_nom(el_val_t noun) {
  if (uga_str_ends(noun, EL_STR("u"))) {
    el_val_t len = uga_str_len(noun);
    if (len > 1) {
      return uga_str_drop_last(noun, 1);
    }
  }
  if (uga_str_ends(noun, EL_STR("atu"))) {
    return uga_str_drop_last(noun, 3);
  }
  return noun;
  return 0;
}

el_val_t uga_is_fem(el_val_t noun) {
  if (uga_str_ends(noun, EL_STR("atu"))) {
    return 1;
  }
  if (uga_str_ends(noun, EL_STR("ata"))) {
    return 1;
  }
  if (uga_str_ends(noun, EL_STR("ati"))) {
    return 1;
  }
  if (uga_str_ends(noun, EL_STR("\xc4\x81tu"))) {
    return 1;
  }
  if (uga_str_ends(noun, EL_STR("\xc4\x81ti"))) {
    return 1;
  }
  return 0;
  return 0;
}

el_val_t uga_decline(el_val_t noun, el_val_t gram_case, el_val_t number) {
  el_val_t fem = uga_is_fem(noun);
  el_val_t stem = uga_strip_nom(noun);
  if (str_eq(number, EL_STR("dual"))) {
    if (str_eq(gram_case, EL_STR("nom"))) {
      return el_str_concat(stem, EL_STR("\xc4\x81ma"));
    }
    return el_str_concat(stem, EL_STR("\xc4\x93ma"));
  }
  if (str_eq(number, EL_STR("plural"))) {
    if (fem) {
      if (str_eq(gram_case, EL_STR("nom"))) {
        return el_str_concat(stem, EL_STR("\xc4\x81tu"));
      }
      return el_str_concat(stem, EL_STR("\xc4\x81ti"));
    }
    if (str_eq(gram_case, EL_STR("nom"))) {
      return el_str_concat(stem, EL_STR("\xc5\xabma"));
    }
    return el_str_concat(stem, EL_STR("\xc4\xabma"));
  }
  if (fem) {
    if (str_eq(gram_case, EL_STR("nom"))) {
      return el_str_concat(stem, EL_STR("atu"));
    }
    if (str_eq(gram_case, EL_STR("acc"))) {
      return el_str_concat(stem, EL_STR("ata"));
    }
    if (str_eq(gram_case, EL_STR("gen"))) {
      return el_str_concat(stem, EL_STR("ati"));
    }
    return el_str_concat(stem, EL_STR("atu"));
  }
  if (str_eq(gram_case, EL_STR("nom"))) {
    return el_str_concat(stem, EL_STR("u"));
  }
  if (str_eq(gram_case, EL_STR("acc"))) {
    return el_str_concat(stem, EL_STR("a"));
  }
  if (str_eq(gram_case, EL_STR("gen"))) {
    return el_str_concat(stem, EL_STR("i"));
  }
  return el_str_concat(stem, EL_STR("u"));
  return 0;
}

el_val_t uga_noun_phrase(el_val_t noun, el_val_t gram_case, el_val_t number, el_val_t definite) {
  return uga_decline(noun, gram_case, number);
  return 0;
}

el_val_t uga_map_canonical(el_val_t verb) {
  if (str_eq(verb, EL_STR("be"))) {
    return EL_STR("kn");
  }
  if (str_eq(verb, EL_STR("go"))) {
    return EL_STR("hlk");
  }
  if (str_eq(verb, EL_STR("see"))) {
    return EL_STR("r\xca\xbcy");
  }
  if (str_eq(verb, EL_STR("say"))) {
    return EL_STR("\xca\xbcmr");
  }
  if (str_eq(verb, EL_STR("speak"))) {
    return EL_STR("\xca\xbcmr");
  }
  return verb;
  return 0;
}

