///|
/// # WebP 编码 WebP Encode
///
/// WebP 图像格式编码
/// WebP 图像格式编码 WebP image format encoding
// 纯 MoonBit WebP lossy 编码器 (VP8)
// 规范: https://datatracker.ietf.org/doc/html/rfc6386
// 实现: 仅 intra 帧 (key frame), DC-only 预测, 固定量化
// 产出: 可被标准 WebP 解码器解码的有效 VP8 bitstream

///|
/// VP8 Bool 编码器(算术编码)
priv struct BoolEncoder {
  output : Array[Byte]
  mut low : Int32
  mut count : Int32
  mut range : Int32
}

///|
fn BoolEncoder::new() -> BoolEncoder {
  { output: [], low: 0, count: 0, range: 255, }
}

///|
/// 编码一个 bool 值,probability 为 0 时编码 0
fn BoolEncoder::encode_bool(
  self : BoolEncoder,
  bit : Int32,
  probability : Int32,
) -> Unit {
  let split = 1 + (((self.range - 1) * probability) >> 8)
  if bit != 0 {
    self.low += split
    self.range -= split
  } else {
    self.range = split
  }
  while self.range < 128 {
    self.shift()
    self.range = self.range << 1
  }
}

///|
fn BoolEncoder::shift(self : BoolEncoder) -> Unit {
  let bit = if ((self.low >> 8) & 1) != 0 { 1 } else { 0 }
  self.low = (self.low << 1) & 0xFFFF
  self.count += 1
  if self.count == 8 {
    self.output.push(bit.to_byte())
    self.count = 0
  }
}

///|
/// 编码固定概率的 bool (50%)
fn BoolEncoder::encode_bit(self : BoolEncoder, bit : Int32) -> Unit {
  self.encode_bool(bit, 128)
}

///|
/// 编码无符号整数 (n bits, MSB first)
fn BoolEncoder::encode_uint(
  self : BoolEncoder,
  value : Int32,
  n : Int32,
) -> Unit {
  for i in 0..> (n - 1 - i)) & 1
    self.encode_bit(bit)
  }
}

///|
/// 编码带符号整数
fn BoolEncoder::encode_int(
  self : BoolEncoder,
  value : Int32,
  n : Int32,
) -> Unit {
  if value < 0 {
    self.encode_bit(1)
    self.encode_uint(-value, n)
  } else {
    self.encode_bit(0)
    self.encode_uint(value, n)
  }
}

///|
/// 完成编码,刷新剩余 bits
fn BoolEncoder::finish(self : BoolEncoder) -> Bytes {
  for _ in 0.. Bytes {
  let result : Array[Byte] = []
  result.push(b'\x52')
  result.push(b'\x49')
  result.push(b'\x46')
  result.push(b'\x46')
  let file_size_pos = result.length()
  result.push(b'\x00')
  result.push(b'\x00')
  result.push(b'\x00')
  result.push(b'\x00')
  result.push(b'\x57')
  result.push(b'\x45')
  result.push(b'\x42')
  result.push(b'\x50')
  result.push(b'\x56')
  result.push(b'\x50')
  result.push(b'\x38')
  result.push(b'\x20')
  let chunk_size_pos = result.length()
  result.push(b'\x00')
  result.push(b'\x00')
  result.push(b'\x00')
  result.push(b'\x00')
  let vp8_len = vp8_data.length()
  for i in 0..> 8) & 0xFF).to_byte()
  result[chunk_size_pos + 2] = ((chunk_size >> 16) & 0xFF).to_byte()
  result[chunk_size_pos + 3] = ((chunk_size >> 24) & 0xFF).to_byte()
  let total_size = result.length() - 8
  result[file_size_pos] = (total_size & 0xFF).to_byte()
  result[file_size_pos + 1] = ((total_size >> 8) & 0xFF).to_byte()
  result[file_size_pos + 2] = ((total_size >> 16) & 0xFF).to_byte()
  result[file_size_pos + 3] = ((total_size >> 24) & 0xFF).to_byte()
  let _ = width
  let _ = height
  Bytes::from_array(result)
}

///|
/// 构建 VP8 frame header (key frame)
fn build_vp8_frame_header(
  width : Int32,
  height : Int32,
  first_part_size : Int32,
) -> Bytes {
  let result : Array[Byte] = []
  let tag = (0 << 0) | (0 << 1) | (1 << 4) | (first_part_size << 5)
  result.push((tag & 0xFF).to_byte())
  result.push(((tag >> 8) & 0xFF).to_byte())
  result.push(((tag >> 16) & 0xFF).to_byte())
  result.push(b'\x9D')
  result.push(b'\x01')
  result.push(b'\x2A')
  result.push((width & 0xFF).to_byte())
  result.push(((width >> 8) & 0xFF).to_byte())
  result.push((height & 0xFF).to_byte())
  result.push(((height >> 8) & 0xFF).to_byte())
  Bytes::from_array(result)
}

///|
/// WebP lossy 编码(纯 MoonBit,全目标可用)
/// 实现: VP8 key frame, DC-only 预测, 固定量化
/// quality: 1-100 (越高越好质量,文件越大)
/// ⚠️ 实测(2026-11-25):产物**不符合 VP8 规范**。RIFF 容器正确,但缺少 first partition
/// 中的分段/滤波/量化/概率更新与 token partition 尺寸表,first_part_size 被写成整个
/// token 流长度。ffmpeg 报 `Invalid partitions`,浏览器同样无法解码。
/// 本函数仅为占位实现,不得在任何能力宣称中使用;合法 VP8/VP8L 编码器列为后续工作。
/// 本库解码器仅支持 lossless (VP8L),也无法解此函数的输出。
/// 自 S02 M3 起改用 [`encode_webp_lossless`](VP8L,产物经 ffmpeg / libwebp 逐字节校验)。
#deprecated("产物不符合 VP8 规范,请改用 encode_webp_lossless")
pub fn encode_webp_lossy_pure(
  img : @types.Image,
  quality~ : Int32 = 75,
) -> Bytes raise @types.LoadError {
  let w = img.width
  let h = img.height
  let ch = img.channels
  if w <= 0 || h <= 0 {
    raise @types.LoadError::EncodeFailed("WebP: 尺寸无效")
  }
  if ch != 3 && ch != 4 {
    raise @types.LoadError::EncodeFailed("WebP: 仅支持 RGB/RGBA")
  }
  let q_factor = ((100 - quality).clamp(min=1, max=99) * 63 / 100).clamp(
    min=1,
    max=63,
  )
  // 转为 Y 分量
  let y_data : Array[Int32] = Array::make(w * h, 0)
  for y in 0..> 8
    }
  }
  let mb_w = (w + 15) / 16
  let mb_h = (h + 15) / 16
  let enc = BoolEncoder::new()
  for mby in 0..