// The names that `x:Name` gives to objects, and the names of event handlers.

///|
/// Whether `text` can be the name of an object, by the rules of System.Xaml `NameValidationHelper`: a letter
/// or `_`, and then letters, `_`, digits and marks. Each UTF-16 unit counts alone, so a character above U+FFFF
/// is in no name. Empty text is no name.
pub fn is_object_name(text : String) -> Bool {
  is_name(text, modifier_starts=false)
}

///|
/// Whether `text` can be the name of an event handler, by the pattern of [MS-XAML-2017] 7.4.14: as for the name of an object, and a
/// modifier letter can also be the first one.
pub fn is_handler_name(text : String) -> Bool {
  is_name(text, modifier_starts=true)
}

///|
fn is_name(text : String, modifier_starts~ : Bool) -> Bool {
  for i in 0.. ()
      Modifier if i > 0 || modifier_starts => ()
      Continue if i > 0 => ()
      _ => return false
    }
  }
  !text.is_empty()
}

///|
priv enum NameClass {
  /// In no name.
  Neither
  /// The categories Lu, Ll, Lt, Lo and Nl, and `_`.
  Start
  /// The category Lm.
  Modifier
  /// The categories Mn, Mc and Nd.
  Continue
}

///|
fn name_class(unit : Int32) -> NameClass {
  // The last run that starts at `unit` or before it.
  let mut low = 0
  let mut high = name_classes.length()
  while high - low > 1 {
    let middle = low + (high - low) / 2
    if name_classes[middle].0 <= unit {
      low = middle
    } else {
      high = middle
    }
  }
  name_classes[low].1
}

///|
/// The runs of UTF-16 units with one class, each with its first unit.
let name_classes : Array[(Int32, NameClass)] = read_name_classes()

///|
fn read_name_classes() -> Array[(Int32, NameClass)] {
  let runs = []
  let mut start = 0
  let mut length = 0
  for character in name_class_runs {
    let found : NameClass = match character {
      'N' => Neither
      'S' => Start
      'M' => Modifier
      'C' => Continue
      '0'..<='9' => {
        length = length * 16 + (character.to_int() - '0')
        continue
      }
      'a'..<='f' => {
        length = length * 16 + (character.to_int() - 'a') + 10
        continue
      }
      _ => continue
    }
    if !runs.is_empty() {
      start += if length == 0 { 1 } else { length }
    }
    length = 0
    runs.push((start, found))
  }
  runs
}

///|
/// The class of each run (`N`, `S`, `M` or `C`, as in `NameClass`) and its length in hexadecimal, which is left out
/// for one unit. `char.GetUnicodeCategory` of .NET 10, which `NameValidationHelper` calls, gave the classes of
/// all 65,536 units. MoonBit's core library has no general categories.
let name_class_runs : String =
  #|N30CaN7S1aN4SNS1aN2fSNaSN4SN5S17NS1fNS1b8M12N4McNeM5N7MNMN11C70S4MNS2N2MS3NSN6SNS3NSNS14NS53NS8bNC5N2Sa6
  #|NS26N2MN6S29N8C2dNCNC2NC2NCN8S1bN4S4N1dCbN5S20MSaC1fN4S2CS63NSC7N2C6M2C2NC4S2CaS3N2SN10SCS1eC1bN2S59CbSN
  #|eCaS21C9M2N4MN2CN2S16C4MC9MC3MC5N12S19C3N4SbN5S18NS6N8C9S29MC18NC21S36C3SC12SC7SaC2N2CaNMSfC3NS8N2S2N2S1
  #|6NS7NSN3S4N2CSC7N2C2N2C3SN8CN4S2NS3C2N2CaS2NaSNCN2C3NS6N4S2N2S16NS7NS2NS2NS2N2CNC5N4C2N2C3N3CN7S4NSN7CcS
  #|3CNbC3NS9NS3NS16NS7NS2NS5N2CSC8NC3NC3N2SNfS2C2N2CaN9SC6NC3NS8N2S2N2S16NS7NS2NS5N2CSC7N2C2N2C3N7C3N4S2NS3
  #|C2N2CaNSN10CSNS6N3S3NS4N3S2NSNS2N3S2N3S3N3ScN4C5N3C3NC4N2SN6CNeCaN10C5S8NS3NS17NS10N2CSC7NC3NC4N7C2NS3N2
  #|SN2S2C2N2CaN10SC3NS8NS3NS17NSaNS5N2CSC7NC3NC4N7C2N6S2NS2C2N2CaNS2CNcC4S9NS3NS29C2SC7NC3NC4SN5S3CN7S3C2N2
  #|CaNaS6NC3NS12N3S18NS9NSN2S7N3CN4C6NCNC8N6CaN2C2NdS30CS2C7N5S6MC8NCaN27S2NSNS5NS18NSNSaCS2C9SN2S5NMNC7NCa
  #|N2S4N20SN17C2N6CaNbCNCNCN4C2S8NS24N4C14NC2S5CbNC24N9CN39S2bC14SCaN6S6C4S4C3SC3S2C7S3C4SdCcSCfN2S26NSN5SN
  #|2S2bNMS14cNS4N2S7NSNS4N2S29NS4N2S21NS4N2S7NSNS4N2SfNS39NS4N2S43N2C3N20S10N10S56N2S6N3S26cN2S11NS1aN5S4bN
  #|3SbN7S12C4N9S13C3NbS12C2NcSdNS3NC2NcS34C20N3MN4SCN2CaN21C3NCbN6S23MS35N7S5C2S22CSN5S46NaS1fNCcN4CcNaCaS1
  #|eN2S5NbS2cN4S1aN6CaN26S17C5N4S35CaNC1dN2CbN6CaNdMN8CeNC10N31C5S2fC11S8N3CaN11C9NcC3S1eCdS2CaS2cCeNcS24C1
  #|4N8CaN3S3CaS1eM6N2SbN5S2bN2S3N10C3NC15S4CS6CS2C3SN5S2cM3fSdMS22M25C40S116N2S6N2S26N2S6N2S8NSNSNSNS1fN2S3
  #|5NS7NSN3S3NS7N3S4N2S6N4SdN5S3NS7N74MNdMN10MdN33CdN4CN3CcN11SN4SN2SaNSN3S5N6SNSNSNS4NSbN2S4N5S5N4SN11S29N
  #|a77S7cM2S67N6S4C3S2NcS26NSN5SN2S38N7MNfCS17N9S7NS7NS7NS7NS7NS7NS7NS7NC20N2fMN1d5MS2N19S9C6NM5N2S3MSN4S56
  #|N2C2N2M2SNS5aNM3SN5S2bNS5eN11S20N30S10N200S19c0N40S5215MS477N43S28M6N2S10cMN3S10CaS2N14S2fCN4CaNMS1cM2C2
  #|S50C2N25M9N2S4eMS17MN2S43N2S2NSNS8N15M3S3M2S8CS3CS4CS17C5N4CN13S34NcC2S32C12NaCaN6C12S6N3SNS2CbS1cC8N2S1
  #|7CdNcS1dN3C4S2fCeNeMCaN6S5CMS9CaS5NS29CeN9S3CS8C2N2CaN6S10MS6N3SC3S32CSC3S2C2S5C2SCSN18S2MN2SbC5N2SM2C2N
  #|aS6N2S6N2S6N9S7NS7NS2bNM4S9MN6S73C8NC2N2CaN6S2ba4NcS17N4S31N2104S16eN2S6aN26S7NcS5N5SCSaNSdNS5NSNS2NS2NS
  #|6cN21S16bN12S40N2S36N28ScN4C10N10C10N40S5NS87N13CaN7S1aN6S1aNbSaMS2dM2S1fN3S6N2S6N2S6N2S3N23