// walkzzz/emoji.mbt — 纯 MoonBit emoji 库(上游 Rust emojis 0.9.1 API 对齐)
// 主包门面:类型定义 + 迭代与查询入口。

///|
/// Emoji 或 Unicode 版本(major.minor)。
///
/// An emoji or Unicode version as defined by the Unicode Standard.
#derive(Compare, Eq, Debug)
pub(all) struct Version {
  major : Int32
  minor : Int32
}

///|
/// 构造版本号(上游 `UnicodeVersion::new` / `EmojiVersion::new`)。
///
/// Construct a new version. (Upstream `UnicodeVersion::new`)
pub fn Version::new(major : Int32, minor : Int32) -> Version {
  { major, minor, }
}

///|
pub extend Version with Compare::{op_lt, op_le, op_ge, compare, op_gt}

///|
pub extend Version with Eq::{not_equal, equal}

///|
pub extend Version with @moonbitlang/core/debug.Debug::{to_repr}

///|
/// 表示一个 emoji(字段只读快照,构造仅限本包数据驱动)。
///
/// Represents an emoji. Fields are read-only; construction is data-driven
/// inside this package (mirrors upstream "no public constructors" policy).
#derive(Debug)
pub struct Emoji {
  /// emoji 本体字符串(上游 as_str())
  glyph : String
  /// CLDR 名称(上游 name())
  name : String
  /// UTS #51 emoji 版本(上游 emoji_version())
  emoji_version : Version
  /// 所属分组(上游 group())
  group : Group
  /// 内部:默认肤色版本在数据表中的下标(-1 = 无肤色族)
  base_idx : Int32
  /// 内部:肤色族大小(1 = 无变体;26 = 双人对序列)
  variant_n : Int32
  /// 内部:当前肤色序号(-1 = 肤色不适用)
  tone_idx : Int32
  /// gemoji 短码列表(可为空数组)
  shortcodes : Array[String]
}

///|
pub extend Emoji with @moonbitlang/core/debug.Debug::{to_repr}

///|
/// 按数据表下标构造 Emoji 视图(包内使用)。
fn emoji_at(i : Int32) -> Emoji {
  let (mj, mn) = @data.emojis_emoji_versions[i]
  {
    glyph: @data.emojis_glyphs[i],
    name: @data.emojis_names[i],
    emoji_version: { major: mj, minor: mn, },
    group: group_from_idx(@data.emojis_groups[i]),
    base_idx: @data.emojis_base_idxs[i],
    variant_n: @data.emojis_variant_ns[i],
    tone_idx: @data.emojis_tone_idxs[i],
    shortcodes: @data.emojis_shortcodes[i],
  }
}

///|
/// 迭代全部 emoji:按 Unicode CLDR 顺序,仅含默认肤色版本。
///
/// Returns an iterator over all emojis, ordered by Unicode CLDR data,
/// excluding non-default skin tones. (Upstream `iter()`)
pub fn iter() -> Iter[Emoji] {
  let arr : Array[Emoji] = []
  for i in 0..<@data.default_idxs.length() {
    arr.push(emoji_at(@data.default_idxs[i]))
  }
  arr.iter()
}

///|
/// 默认肤色 emoji 总数。
///
/// Total number of emojis with default skin tone.
/// (MoonBit-specific, @since 0.1.0 moonbit)
pub fn count() -> Int32 {
  @data.default_idxs.length()
}

///|
/// emoji 本体字符串。
///
/// Returns this emoji as a string. (Upstream `as_str()`)
pub fn Emoji::to_string(self : Emoji) -> String {
  self.glyph
}

///|
/// 返回 Unicode 标准版本:由 emoji_version 派生(上游 unicode_version())。
///
/// Returns the version of the Unicode Standard in which the underlying
/// code point(s) were first encoded. (Upstream `unicode_version()`)
pub fn Emoji::unicode_version(self : Emoji) -> Version {
  let ev = self.emoji_version
  let m = @data.emoji_version_to_unicode_version
  let mut i = 0
  while i < m.length() {
    let (emj, emn, uvj, uvn) = m[i]
    if emj == ev.major && emn == ev.minor {
      return { major: uvj, minor: uvn, }
    }
    i += 1
  }
  ev
}

///|
/// 返回 emoji 本体的 UTF-8 编码字节。
///
/// Returns this emoji as UTF-8 encoded bytes. (Upstream `as_bytes()`)
pub fn Emoji::utf8_bytes(self : Emoji) -> Bytes {
  @utf8.encode(self.glyph)
}