///|
/// Raised when a source position or span breaks its invariants.
#derive(Eq, Debug)
pub(all) suberror PositionError {
  /// A line or column is less than 1.
  InvalidPosition(line~ : Int32, column~ : Int32)
  /// The start position is after the end position.
  StartAfterEnd(start~ : TextPosition, end~ : TextPosition)
  /// An offset is negative or the start offset is after the end offset.
  InvalidOffsets(start~ : Int32, end~ : Int32)
}

///|
/// A 1-based line and column position in source text. Columns count UTF-16 code
/// units. Positions order by line, then by column.
#derive(Eq, Compare, Hash, Debug)
struct TextPosition {
  line : Int32
  column : Int32
}

///|
/// Creates a validated 1-based text position.
pub fn TextPosition::TextPosition(
  line~ : Int32,
  column~ : Int32,
) -> TextPosition raise PositionError {
  if line < 1 || column < 1 {
    raise InvalidPosition(line~, column~)
  }
  { line, column, }
}

///|
/// The 1-based start of a file (`1:1`).
pub fn TextPosition::start_of_file() -> TextPosition {
  { line: 1, column: 1, }
}

///|
/// The 1-based line number.
pub fn TextPosition::line(self : TextPosition) -> Int32 {
  self.line
}

///|
/// The 1-based UTF-16 column number.
pub fn TextPosition::column(self : TextPosition) -> Int32 {
  self.column
}

///|
/// Formats the position as `line:column`.
pub fn TextPosition::to_string(self : TextPosition) -> String {
  "\{self.line}:\{self.column}"
}

///|
pub impl Show for TextPosition with fn output(self, logger) {
  logger.write_string(self.to_string())
}

///|
/// A half-open UTF-16 offset range `[start_offset, end_offset)` paired with
/// 1-based start and end positions.
#derive(Eq, Hash, Debug)
struct SourceSpan {
  start : TextPosition
  end : TextPosition
  start_offset : Int32
  end_offset : Int32
}

///|
/// Creates a validated source span.
pub fn SourceSpan::SourceSpan(
  start~ : TextPosition,
  end~ : TextPosition,
  start_offset~ : Int32,
  end_offset~ : Int32,
) -> SourceSpan raise PositionError {
  if start > end {
    raise StartAfterEnd(start~, end~)
  }
  if start_offset < 0 || start_offset > end_offset {
    raise InvalidOffsets(start=start_offset, end=end_offset)
  }
  { start, end, start_offset, end_offset, }
}

///|
/// An empty span at the start of a file (`0..0`, `1:1..1:1`).
pub fn SourceSpan::empty_at_start() -> SourceSpan {
  let pos = TextPosition::start_of_file()
  { start: pos, end: pos, start_offset: 0, end_offset: 0, }
}

///|
/// Internal helper for constructing a validated span by construction when
/// offsets and positions come directly from the monotonic lexer cursor.
fn SourceSpan::unchecked(
  start : TextPosition,
  end : TextPosition,
  start_offset : Int32,
  end_offset : Int32,
) -> SourceSpan {
  { start, end, start_offset, end_offset, }
}

///|
/// Merges two spans into the smallest span covering both.
pub fn SourceSpan::cover(self : SourceSpan, other : SourceSpan) -> SourceSpan {
  let (start, start_offset) = if self.start_offset <= other.start_offset {
    (self.start, self.start_offset)
  } else {
    (other.start, other.start_offset)
  }
  let (end, end_offset) = if self.end_offset >= other.end_offset {
    (self.end, self.end_offset)
  } else {
    (other.end, other.end_offset)
  }
  { start, end, start_offset, end_offset, }
}

///|
pub fn SourceSpan::start(self : SourceSpan) -> TextPosition {
  self.start
}

///|
pub fn SourceSpan::end(self : SourceSpan) -> TextPosition {
  self.end
}

///|
pub fn SourceSpan::start_offset(self : SourceSpan) -> Int32 {
  self.start_offset
}

///|
pub fn SourceSpan::end_offset(self : SourceSpan) -> Int32 {
  self.end_offset
}

///|
/// Length in UTF-16 code units.
pub fn SourceSpan::len(self : SourceSpan) -> Int32 {
  self.end_offset - self.start_offset
}

///|
/// Whether `other` lies completely inside `self`.
pub fn SourceSpan::contains(self : SourceSpan, other : SourceSpan) -> Bool {
  self.start_offset <= other.start_offset && other.end_offset <= self.end_offset
}

///|
pub fn SourceSpan::to_string(self : SourceSpan) -> String {
  "\{self.start}..\{self.end}"
}

///|
pub impl Show for SourceSpan with fn output(self, logger) {
  logger.write_string(self.to_string())
}