///|
/// Encode the largest prefix that fits a miniSEED 3 record. A positive-length
/// input always consumes at least one sample. Resume at the returned count,
/// using the original header and cumulative sample index to calculate the start.
pub fn pack_record(
  header : @mseed.Header,
  encoding : @codec.Encoding,
  input : ArrayView[Int32],
  max_frames~ : Int32,
) -> (Bytes, Int32) raise WaveformError {
  guard max_frames >= 1 && max_frames <= 65536 else {
    raise InvalidWaveform(
      "a record requires a frame budget between 1 and 65536",
    )
  }
  if input.is_empty() {
    let bytes = @mseed.make_v3({ ..header, rate: 0.0, }, 0, 0, b"") catch {
      e => raise InvalidWaveform(Show::to_string(e))
    }
    return (bytes, 0)
  }
  guard header.rate.is_finite() && header.rate != 0.0 else {
    raise InvalidWaveform("a waveform needs a finite nonzero sample rate")
  }
  let (payload, count) = @codec.encode(encoding, input, max_frames~) catch {
    e => raise InvalidWaveform(Show::to_string(e))
  }
  let code = match encoding {
    Steim1 => 10
    Steim2 => 11
  }
  let record = @mseed.make_v3(header, code, count, payload) catch {
    e => raise InvalidWaveform(Show::to_string(e))
  }
  (record, count)
}