#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 sga_drop(el_val_t s, el_val_t n);
el_val_t sga_first(el_val_t s);
el_val_t sga_rest(el_val_t s);
el_val_t sga_slot(el_val_t person, el_val_t number);
el_val_t sga_lenite(el_val_t word);
el_val_t sga_copula_present(el_val_t slot);
el_val_t sga_bith_present(el_val_t slot);
el_val_t sga_bith_past(el_val_t slot);
el_val_t sga_teit_present(el_val_t slot);
el_val_t sga_teit_past(el_val_t slot);
el_val_t sga_gaibid_present(el_val_t slot);
el_val_t sga_adci_present(el_val_t slot);
el_val_t sga_asbeir_present(el_val_t slot);
el_val_t sga_map_canonical(el_val_t verb);
el_val_t sga_ai_present(el_val_t stem, el_val_t slot);
el_val_t sga_conjugate(el_val_t verb, el_val_t tense, el_val_t person, el_val_t number);
el_val_t sga_decline_ostem(el_val_t noun, el_val_t gram_case, el_val_t number);
el_val_t sga_decline_astem(el_val_t noun, el_val_t gram_case, el_val_t number);
el_val_t sga_detect_gender(el_val_t noun);
el_val_t sga_decline(el_val_t noun, el_val_t gram_case, el_val_t number);
el_val_t sga_noun_phrase(el_val_t noun, el_val_t gram_case, el_val_t number, el_val_t definite);

el_val_t sga_drop(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 sga_first(el_val_t s) {
  if (str_len(s) == 0) {
    return EL_STR("");
  }
  return str_slice(s, 0, 1);
  return 0;
}

el_val_t sga_rest(el_val_t s) {
  el_val_t n = str_len(s);
  if (n <= 1) {
    return EL_STR("");
  }
  return str_slice(s, 1, n);
  return 0;
}

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

el_val_t sga_lenite(el_val_t word) {
  el_val_t init = sga_first(word);
  el_val_t tail = sga_rest(word);
  if (str_eq(init, EL_STR("b"))) {
    return el_str_concat(EL_STR("bh"), tail);
  }
  if (str_eq(init, EL_STR("c"))) {
    return el_str_concat(EL_STR("ch"), tail);
  }
  if (str_eq(init, EL_STR("d"))) {
    return el_str_concat(EL_STR("dh"), tail);
  }
  if (str_eq(init, EL_STR("f"))) {
    return el_str_concat(EL_STR("fh"), tail);
  }
  if (str_eq(init, EL_STR("g"))) {
    return el_str_concat(EL_STR("gh"), tail);
  }
  if (str_eq(init, EL_STR("m"))) {
    return el_str_concat(EL_STR("mh"), tail);
  }
  if (str_eq(init, EL_STR("p"))) {
    return el_str_concat(EL_STR("ph"), tail);
  }
  if (str_eq(init, EL_STR("s"))) {
    return el_str_concat(EL_STR("sh"), tail);
  }
  if (str_eq(init, EL_STR("t"))) {
    return el_str_concat(EL_STR("th"), tail);
  }
  return word;
  return 0;
}

el_val_t sga_copula_present(el_val_t slot) {
  if (slot == 0) {
    return EL_STR("am");
  }
  if (slot == 1) {
    return EL_STR("at");
  }
  if (slot == 2) {
    return EL_STR("is");
  }
  if (slot == 3) {
    return EL_STR("am");
  }
  if (slot == 4) {
    return EL_STR("adib");
  }
  return EL_STR("it");
  return 0;
}

el_val_t sga_bith_present(el_val_t slot) {
  if (slot == 0) {
    return EL_STR("am");
  }
  if (slot == 1) {
    return EL_STR("at");
  }
  if (slot == 2) {
    return EL_STR("is");
  }
  if (slot == 3) {
    return EL_STR("am");
  }
  if (slot == 4) {
    return EL_STR("adib");
  }
  return EL_STR("at");
  return 0;
}

el_val_t sga_bith_past(el_val_t slot) {
  if (slot == 0) {
    return EL_STR("ba");
  }
  if (slot == 1) {
    return EL_STR("ba");
  }
  if (slot == 2) {
    return EL_STR("ba");
  }
  if (slot == 3) {
    return EL_STR("b\xc3\xa1mmar");
  }
  if (slot == 4) {
    return EL_STR("b\xc3\xa1""daid");
  }
  return EL_STR("batar");
  return 0;
}

el_val_t sga_teit_present(el_val_t slot) {
  if (slot == 0) {
    return EL_STR("t\xc3\xad""agu");
  }
  if (slot == 1) {
    return EL_STR("t\xc3\xa9it");
  }
  if (slot == 2) {
    return EL_STR("t\xc3\xa9it");
  }
  if (slot == 3) {
    return EL_STR("t\xc3\xad""agmai");
  }
  if (slot == 4) {
    return EL_STR("t\xc3\xad""agid");
  }
  return EL_STR("t\xc3\xad""agat");
  return 0;
}

el_val_t sga_teit_past(el_val_t slot) {
  if (slot == 0) {
    return EL_STR("lod");
  }
  if (slot == 1) {
    return EL_STR("lod");
  }
  if (slot == 2) {
    return EL_STR("luid");
  }
  if (slot == 3) {
    return EL_STR("lodmar");
  }
  if (slot == 4) {
    return EL_STR("lodaid");
  }
  return EL_STR("lotar");
  return 0;
}

el_val_t sga_gaibid_present(el_val_t slot) {
  if (slot == 0) {
    return EL_STR("gaibim");
  }
  if (slot == 1) {
    return EL_STR("gaibi");
  }
  if (slot == 2) {
    return EL_STR("gaibid");
  }
  if (slot == 3) {
    return EL_STR("gaibmi");
  }
  if (slot == 4) {
    return EL_STR("gaibthe");
  }
  return EL_STR("gaibid");
  return 0;
}

el_val_t sga_adci_present(el_val_t slot) {
  if (slot == 0) {
    return EL_STR("ad\xc2\xb7""ciu");
  }
  if (slot == 1) {
    return EL_STR("ad\xc2\xb7""c\xc3\xad");
  }
  if (slot == 2) {
    return EL_STR("ad\xc2\xb7""c\xc3\xad");
  }
  if (slot == 3) {
    return EL_STR("ad\xc2\xb7""c\xc3\xadmi");
  }
  if (slot == 4) {
    return EL_STR("ad\xc2\xb7""c\xc3\xadthe");
  }
  return EL_STR("ad\xc2\xb7""ciat");
  return 0;
}

el_val_t sga_asbeir_present(el_val_t slot) {
  if (slot == 0) {
    return EL_STR("as\xc2\xb7""biur");
  }
  if (slot == 1) {
    return EL_STR("as\xc2\xb7""beir");
  }
  if (slot == 2) {
    return EL_STR("as\xc2\xb7""beir");
  }
  if (slot == 3) {
    return EL_STR("as\xc2\xb7""beram");
  }
  if (slot == 4) {
    return EL_STR("as\xc2\xb7""berid");
  }
  return EL_STR("as\xc2\xb7""berat");
  return 0;
}

el_val_t sga_map_canonical(el_val_t verb) {
  if (str_eq(verb, EL_STR("be"))) {
    return EL_STR("is");
  }
  if (str_eq(verb, EL_STR("go"))) {
    return EL_STR("t\xc3\xa9it");
  }
  if (str_eq(verb, EL_STR("take"))) {
    return EL_STR("gaibid");
  }
  if (str_eq(verb, EL_STR("hold"))) {
    return EL_STR("gaibid");
  }
  if (str_eq(verb, EL_STR("see"))) {
    return EL_STR("ad\xc2\xb7""c\xc3\xad");
  }
  if (str_eq(verb, EL_STR("say"))) {
    return EL_STR("as\xc2\xb7""beir");
  }
  return verb;
  return 0;
}

el_val_t sga_ai_present(el_val_t stem, el_val_t slot) {
  if (slot == 0) {
    return el_str_concat(stem, EL_STR("aim"));
  }
  if (slot == 1) {
    return el_str_concat(stem, EL_STR("ai"));
  }
  if (slot == 2) {
    return el_str_concat(stem, EL_STR("aid"));
  }
  if (slot == 3) {
    return el_str_concat(stem, EL_STR("am"));
  }
  if (slot == 4) {
    return el_str_concat(stem, EL_STR("aid"));
  }
  return el_str_concat(stem, EL_STR("at"));
  return 0;
}

el_val_t sga_conjugate(el_val_t verb, el_val_t tense, el_val_t person, el_val_t number) {
  el_val_t v = sga_map_canonical(verb);
  el_val_t slot = sga_slot(person, number);
  if (str_eq(v, EL_STR("is"))) {
    if (str_eq(tense, EL_STR("present"))) {
      return sga_copula_present(slot);
    }
    return EL_STR("ba");
  }
  if (str_eq(v, EL_STR("bith"))) {
    if (str_eq(tense, EL_STR("present"))) {
      return sga_bith_present(slot);
    }
    if (str_eq(tense, EL_STR("past"))) {
      return sga_bith_past(slot);
    }
    return v;
  }
  if (str_eq(v, EL_STR("t\xc3\xa9it"))) {
    if (str_eq(tense, EL_STR("present"))) {
      return sga_teit_present(slot);
    }
    if (str_eq(tense, EL_STR("past"))) {
      return sga_teit_past(slot);
    }
    return v;
  }
  if (str_eq(v, EL_STR("gaibid"))) {
    if (str_eq(tense, EL_STR("present"))) {
      return sga_gaibid_present(slot);
    }
    return EL_STR("gab");
  }
  if (str_eq(v, EL_STR("ad\xc2\xb7""c\xc3\xad"))) {
    if (str_eq(tense, EL_STR("present"))) {
      return sga_adci_present(slot);
    }
    return v;
  }
  if (str_eq(v, EL_STR("as\xc2\xb7""beir"))) {
    if (str_eq(tense, EL_STR("present"))) {
      return sga_asbeir_present(slot);
    }
    return v;
  }
  if (str_ends_with(v, EL_STR("id"))) {
    el_val_t stem = sga_drop(v, 2);
    if (str_eq(tense, EL_STR("present"))) {
      return sga_ai_present(stem, slot);
    }
    return v;
  }
  return v;
  return 0;
}

el_val_t sga_decline_ostem(el_val_t noun, el_val_t gram_case, el_val_t number) {
  if (str_eq(noun, EL_STR("fer"))) {
    if (str_eq(number, EL_STR("singular"))) {
      if (str_eq(gram_case, EL_STR("nominative"))) {
        return EL_STR("fer");
      }
      if (str_eq(gram_case, EL_STR("vocative"))) {
        return EL_STR("fhir");
      }
      if (str_eq(gram_case, EL_STR("accusative"))) {
        return EL_STR("fer");
      }
      if (str_eq(gram_case, EL_STR("genitive"))) {
        return EL_STR("fir");
      }
      if (str_eq(gram_case, EL_STR("dative"))) {
        return EL_STR("fiur");
      }
      return EL_STR("fer");
    }
    if (str_eq(gram_case, EL_STR("nominative"))) {
      return EL_STR("fir");
    }
    if (str_eq(gram_case, EL_STR("vocative"))) {
      return EL_STR("firu");
    }
    if (str_eq(gram_case, EL_STR("accusative"))) {
      return EL_STR("firu");
    }
    if (str_eq(gram_case, EL_STR("genitive"))) {
      return EL_STR("fer");
    }
    if (str_eq(gram_case, EL_STR("dative"))) {
      return EL_STR("feraib");
    }
    return EL_STR("fir");
  }
  if (str_eq(number, EL_STR("singular"))) {
    if (str_eq(gram_case, EL_STR("nominative"))) {
      return noun;
    }
    if (str_eq(gram_case, EL_STR("vocative"))) {
      return sga_lenite(noun);
    }
    if (str_eq(gram_case, EL_STR("accusative"))) {
      return noun;
    }
    if (str_eq(gram_case, EL_STR("genitive"))) {
      return el_str_concat(noun, EL_STR("a"));
    }
    if (str_eq(gram_case, EL_STR("dative"))) {
      return el_str_concat(noun, EL_STR("u"));
    }
    return noun;
  }
  if (str_eq(gram_case, EL_STR("nominative"))) {
    return el_str_concat(noun, EL_STR("i"));
  }
  if (str_eq(gram_case, EL_STR("vocative"))) {
    return el_str_concat(noun, EL_STR("u"));
  }
  if (str_eq(gram_case, EL_STR("accusative"))) {
    return el_str_concat(noun, EL_STR("u"));
  }
  if (str_eq(gram_case, EL_STR("genitive"))) {
    return noun;
  }
  if (str_eq(gram_case, EL_STR("dative"))) {
    return el_str_concat(noun, EL_STR("aib"));
  }
  return el_str_concat(noun, EL_STR("i"));
  return 0;
}

el_val_t sga_decline_astem(el_val_t noun, el_val_t gram_case, el_val_t number) {
  if (str_eq(noun, EL_STR("ben"))) {
    if (str_eq(number, EL_STR("singular"))) {
      if (str_eq(gram_case, EL_STR("nominative"))) {
        return EL_STR("ben");
      }
      if (str_eq(gram_case, EL_STR("vocative"))) {
        return EL_STR("ben");
      }
      if (str_eq(gram_case, EL_STR("accusative"))) {
        return EL_STR("bein");
      }
      if (str_eq(gram_case, EL_STR("genitive"))) {
        return EL_STR("mn\xc3\xa1");
      }
      if (str_eq(gram_case, EL_STR("dative"))) {
        return EL_STR("mn\xc3\xa1ib");
      }
      return EL_STR("ben");
    }
    if (str_eq(gram_case, EL_STR("nominative"))) {
      return EL_STR("mn\xc3\xa1");
    }
    if (str_eq(gram_case, EL_STR("vocative"))) {
      return EL_STR("mn\xc3\xa1");
    }
    if (str_eq(gram_case, EL_STR("accusative"))) {
      return EL_STR("mn\xc3\xa1");
    }
    if (str_eq(gram_case, EL_STR("genitive"))) {
      return EL_STR("ban");
    }
    if (str_eq(gram_case, EL_STR("dative"))) {
      return EL_STR("mn\xc3\xa1ib");
    }
    return EL_STR("mn\xc3\xa1");
  }
  if (str_eq(number, EL_STR("singular"))) {
    if (str_eq(gram_case, EL_STR("nominative"))) {
      return noun;
    }
    if (str_eq(gram_case, EL_STR("vocative"))) {
      return noun;
    }
    if (str_eq(gram_case, EL_STR("accusative"))) {
      return el_str_concat(noun, EL_STR("i"));
    }
    if (str_eq(gram_case, EL_STR("genitive"))) {
      return el_str_concat(noun, EL_STR("e"));
    }
    if (str_eq(gram_case, EL_STR("dative"))) {
      return el_str_concat(noun, EL_STR("aib"));
    }
    return noun;
  }
  if (str_eq(gram_case, EL_STR("nominative"))) {
    return el_str_concat(noun, EL_STR("a"));
  }
  if (str_eq(gram_case, EL_STR("vocative"))) {
    return el_str_concat(noun, EL_STR("a"));
  }
  if (str_eq(gram_case, EL_STR("accusative"))) {
    return el_str_concat(noun, EL_STR("a"));
  }
  if (str_eq(gram_case, EL_STR("genitive"))) {
    return noun;
  }
  if (str_eq(gram_case, EL_STR("dative"))) {
    return el_str_concat(noun, EL_STR("aib"));
  }
  return el_str_concat(noun, EL_STR("a"));
  return 0;
}

el_val_t sga_detect_gender(el_val_t noun) {
  if (str_eq(noun, EL_STR("ben"))) {
    return EL_STR("feminine");
  }
  if (str_eq(noun, EL_STR("mn\xc3\xa1"))) {
    return EL_STR("feminine");
  }
  return EL_STR("masculine");
  return 0;
}

el_val_t sga_decline(el_val_t noun, el_val_t gram_case, el_val_t number) {
  el_val_t gender = sga_detect_gender(noun);
  if (str_eq(gender, EL_STR("masculine"))) {
    return sga_decline_ostem(noun, gram_case, number);
  }
  if (str_eq(gender, EL_STR("feminine"))) {
    return sga_decline_astem(noun, gram_case, number);
  }
  return noun;
  return 0;
}

el_val_t sga_noun_phrase(el_val_t noun, el_val_t gram_case, el_val_t number, el_val_t definite) {
  el_val_t base = sga_decline(noun, gram_case, number);
  if (!str_eq(definite, EL_STR("true"))) {
    return base;
  }
  return el_str_concat(EL_STR("in "), base);
  return 0;
}

