//https://github.com/bazh/subtitles-parser/blob/master/index.js
export class CaptionParser {
  constructor() {
    this.pItems = {};
  }
  fromSrt(data, s) {
    var useSec = s ? true : false;
    data = data.replace(/\r/g, '');
    var regex = /(\d+)\n(\d{2}:\d{2}:\d{2},\d{3}) --> (\d{2}:\d{2}:\d{2},\d{3})/g;
    data = data.split(regex);
    data.shift();
    var items = {};
    // for (var i = 0; i < data.length; i += 4) {
    //     items.push({
    //         id: data[i].trim(),
    //         startTime: useMs ? this.timeMs(data[i + 1].trim()) : data[i + 1].trim(),
    //         endTime: useMs ? this.timeMs(data[i + 2].trim()) : data[i + 2].trim(),
    //         text: data[i + 3].trim()
    //     });
    // }
    for (var i = 0; i < data.length; i += 4) {
      items[useSec ? this.timeS(data[i + 1].trim()) : data[i + 1].trim()] = data[i + 3].trim();
    }
    return items;
  }
  ;
  toSrt(data) {
    if (!(data instanceof Array))
      return '';
    var res = '';
    for (var i = 0; i < data.length; i++) {
      var s = data[i];
      if (!isNaN(s.startTime) && !isNaN(s.endTime)) {
        s.startTime = this.msTime(parseInt(s.startTime, 10));
        s.endTime = this.msTime(parseInt(s.endTime, 10));
      }
      res += s.id + '\r\n';
      res += s.startTime + ' --> ' + s.endTime + '\r\n';
      res += s.text.replace('\n', '\r\n') + '\r\n\r\n';
    }
    return res;
  }
  ;
  // private timeMs(val) {
  //     var regex = /(\d+):(\d{2}):(\d{2}),(\d{3})/;
  //     var parts = regex.exec(val);
  //     if (parts === null) {
  //         return 0;
  //     }
  //     for (var i = 1; i < 5; i++) {
  //         // parts[i] = parseInt(parts[i], 10);
  //         // if (isNaN(parts[i])) parts[i] = 0;
  //         parts[i] = parseInt(parts[i], 10).toString();
  //         if (isNaN(parseInt(parts[i], 10))) parts[i] = "0";
  //     }
  //     // hours + minutes + seconds + ms
  //     return (parseInt(parts[1], 10) * 3600000) + (parseInt(parts[2], 10) * 60000) + (parseInt(parts[3], 10) * 1000) + parseInt(parts[4], 10);
  // };
  timeS(val) {
    var regex = /(\d+):(\d{2}):(\d{2}),(\d{3})/;
    var parts = regex.exec(val);
    if (parts === null) {
      return 0;
    }
    for (var i = 1; i < 5; i++) {
      // parts[i] = parseInt(parts[i], 10);
      // if (isNaN(parts[i])) parts[i] = 0;
      parts[i] = parseInt(parts[i], 10).toString();
      if (isNaN(parseInt(parts[i], 10)))
        parts[i] = "0";
    }
    // hours + minutes + seconds + ms
    return (parseInt(parts[1], 10) * 3600) + (parseInt(parts[2], 10) * 60) + (parseInt(parts[3], 10)) + Math.floor(parseInt(parts[4], 10) / 1000);
  }
  ;
  msTime(val) {
    var measures = [3600000, 60000, 1000];
    var time = [];
    for (var i in measures) {
      var res = (val / measures[i] >> 0).toString();
      if (res.length < 2)
        res = '0' + res;
      val %= measures[i];
      time.push(res);
    }
    var ms = val.toString();
    if (ms.length < 3) {
      for (let i = 0; i <= 3 - ms.length; i++)
        ms = '0' + ms;
    }
    return time.join(':') + ',' + ms;
  }
  ;
}
