// Keyword set, sanitization and valid-identifier production (implements spec.mbt).

///|
/// True if `s` is a MoonBit hard keyword, lexer reserved keyword (from
/// moonbitlang/lexer 0.4.5), receiver/type-reserved name (`self`, `Self`),
/// wildcard (`_`), or boolean literal (`true`, `false`).
pub fn is_keyword(s : String) -> Bool {
  match s {
    // Hard keywords (moonbitlang/lexer keyword_table)
    "_"
    | "and"
    | "as"
    | "async"
    | "break"
    | "catch"
    | "const"
    | "continue"
    | "declare"
    | "defer"
    | "derive"
    | "else"
    | "enum"
    | "enumview"
    | "errdefer"
    | "extenum"
    | "extend"
    | "extern"
    | "false"
    | "fn"
    | "for"
    | "guard"
    | "if"
    | "impl"
    | "import"
    | "in"
    | "is"
    | "let"
    | "letrec"
    | "lexmatch"
    | "lexscan"
    | "loop"
    | "match"
    | "mut"
    | "nobreak"
    | "nocancel"
    | "noraise"
    | "package"
    | "priv"
    | "proof_assert"
    | "proof_let"
    | "pub"
    | "raise"
    | "readonly"
    | "return"
    | "struct"
    | "suberror"
    | "test"
    | "throw"
    | "trait"
    | "true"
    | "try"
    | "type"
    | "using"
    | "var"
    | "where"
    | "while"
    | "with"
    // Reserved keywords (moonbitlang/lexer + moonc warning [0035])
    | "abstract"
    | "alias"
    | "assert"
    | "await"
    | "constructor"
    | "do"
    | "dyn"
    | "export"
    | "final"
    | "include"
    | "local"
    | "macro"
    | "member"
    | "method"
    | "module"
    | "move"
    | "namespace"
    | "override"
    | "protected"
    | "recur"
    | "ref"
    | "resume"
    | "static"
    | "super"
    | "typeof"
    | "unsafe"
    | "use"
    | "virtual"
    | "yield"
    // Receiver & reserved type name
    | "self"
    | "Self" => true
    _ => false
  }
}

///|
/// Append `_` if `name` is a MoonBit reserved keyword; otherwise return `name` unchanged.
pub fn escape_keyword(name : String) -> String {
  if is_keyword(name) {
    name + "_"
  } else {
    name
  }
}

///|
/// Strip Rust `r#` raw-identifier prefixes and replace non-ASCII-identifier
/// characters with `_`.
pub fn sanitize(input : String) -> String {
  let stripped = strip_rust_raw_prefixes(input)
  let buf = StringBuilder()
  for c in stripped {
    if c is ('A'..<='Z' | 'a'..<='z' | '0'..<='9' | '_') {
      buf.write_char(c)
    } else {
      buf.write_char('_')
    }
  }
  buf.to_string()
}

///|
/// Ensure `s` is a valid MoonBit value identifier (`LIDENT`: non-empty, starts
/// with `[a-z_]`, not bare `_`, no uppercase/digit start).
pub fn ensure_valid_ident(s : String) -> String {
  if s == "" || s == "_" {
    "__"
  } else if s[0] is ('0'..<='9' | 'A'..<='Z') {
    "_" + s
  } else {
    s
  }
}

///|
/// Ensure `s` is a valid MoonBit type/variant identifier (starts with `[A-Z]`).
pub fn ensure_valid_type_ident(s : String) -> String {
  if s == "" {
    "Unnamed"
  } else if s[0] is ('A'..<='Z') {
    s
  } else {
    "V" + s
  }
}

///|
/// Sanitize + PascalCase + UIDENT validity + keyword escape: a valid MoonBit
/// type/variant identifier from any input (`r#CreateParams` -> `CreateParams`,
/// `$float` -> `Float`, `2xx` -> `V2Xx`, `""` -> `Unnamed`, `self` -> `Self_`).
pub fn ident_pascal(input : String) -> String {
  escape_keyword(ensure_valid_type_ident(to_pascal(input)))
}

///|
/// Sanitize + lower_snake_case + LIDENT validity + keyword escape: a valid
/// MoonBit value identifier from any input (`type` -> `type_`, `displayName`
/// -> `display_name`, `$float` -> `float`, `2xx` -> `_2_xx`, `""` -> `__`).
pub fn ident_snake(input : String) -> String {
  ensure_valid_ident(escape_keyword(to_snake(input)))
}