import { Duration } from '../generated/google/protobuf/duration';
import millisFromDuration from './millisFromDuration';

function toHHMMSS(dur: Duration): string {
  const millis = millisFromDuration(dur);
  let secs = Math.floor(millis / 1_000);
  const hrs = Math.floor(secs / 3600);
  const mins = Math.floor(secs / 60) % 60;
  secs = secs % 60;

  return [hrs, mins, secs]
    .map((v) => (v < 10 ? '0' + v : v))
    .filter((v, i) => v != '00' || i > 0)
    .join(':');
}

export default toHHMMSS;
