///|
#derive(Debug)
pub struct SunPixelFrame {
  priv width : Int32
  priv height : Int32
  priv row_bytes : Int32
  priv scale_factor : Double
  // The snapshot is stored once, as `bytes`; `pixels()` materialises a fixed
  // array view on demand for tests and golden comparisons.
  priv mut pixels_cache : FixedArray[Byte]?
  priv bytes : Bytes
}

///|
pub extend SunPixelFrame with Debug::{to_repr}

///|
pub struct SunPresentTarget {
  description : String
  present : (SunPixelFrame) -> Bool
}

///|
#derive(Eq, Debug, ToJson)
pub struct SunUnsupportedCommandDiagnostic {
  priv command : String
  priv reason : String
}

///|
pub fn SunUnsupportedCommandDiagnostic::command(
  self : SunUnsupportedCommandDiagnostic,
) -> String {
  self.command
}

///|
pub fn SunUnsupportedCommandDiagnostic::reason(
  self : SunUnsupportedCommandDiagnostic,
) -> String {
  self.reason
}

///|
pub extend SunUnsupportedCommandDiagnostic with Debug::{to_repr}

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

///|
pub extend SunUnsupportedCommandDiagnostic with ToJson::{to_json}

///|
pub fn SunPresentTarget::new(
  description~ : String,
  present~ : (SunPixelFrame) -> Bool,
) -> SunPresentTarget {
  { description, present, }
}

///|
pub fn SunPixelFrame::width(self : SunPixelFrame) -> Int32 {
  self.width
}

///|
pub fn SunPixelFrame::height(self : SunPixelFrame) -> Int32 {
  self.height
}

///|
pub fn SunPixelFrame::row_bytes(self : SunPixelFrame) -> Int32 {
  self.row_bytes
}

///|
pub fn SunPixelFrame::scale_factor(self : SunPixelFrame) -> Double {
  self.scale_factor
}

///|
pub fn SunPixelFrame::pixels(self : SunPixelFrame) -> FixedArray[Byte] {
  match self.pixels_cache {
    Some(cached) => cached
    None => {
      let bytes = self.bytes
      let out : FixedArray[Byte] = FixedArray::make(bytes.length(), 0)
      // `blit_from_bytes` is the `%fixedarray.copy` intrinsic, so deriving the
      // view is one bulk copy instead of a per-byte loop.
      out.blit_from_bytes(0, bytes, 0, bytes.length())
      self.pixels_cache = Some(out)
      out
    }
  }
}

///|
pub fn SunPixelFrame::bytes(self : SunPixelFrame) -> Bytes {
  self.bytes
}