const i=/(?:(.+?)\n)?((?:\d{2}:)?\d{2}:\d{2}.\d{3})\s+-->\s+((?:\d{2}:)?\d{2}:\d{2}.\d{3})/g,u=/^WEBVTT(.|\n)*?\n{2}/;export const inlineTimestampRegex=/<(?:(\d{2}):)?(\d{2}):(\d{2}).(\d{3})>/g;export class SubtitleParser{constructor(e){this.preambleText=null,this.preambleEmitted=!1,this.options=e}parse(e){e=e.replaceAll(`\r
`,`
`).replaceAll("\r",`
`),i.lastIndex=0;let t;if(!this.preambleText){if(!u.test(e))throw new Error("WebVTT preamble incorrect.");t=i.exec(e);const r=e.slice(0,t?.index??e.length).trimEnd();if(!r)throw new Error("No WebVTT preamble provided.");this.preambleText=r,t&&(e=e.slice(t.index),i.lastIndex=0)}for(;t=i.exec(e);){const r=e.slice(0,t.index),o=t[1],c=t.index+t[0].length,d=e.indexOf(`
`,c)+1,m=e.slice(c,d).trim();let s=e.indexOf(`

`,c);s===-1&&(s=e.length);const a=parseSubtitleTimestamp(t[2]),p=parseSubtitleTimestamp(t[3])-a,h=e.slice(d,s).trim();e=e.slice(s).trimStart(),i.lastIndex=0;const b={timestamp:a/1e3,duration:p/1e3,text:h,identifier:o,settings:m,notes:r},l={};this.preambleEmitted||(l.config={description:this.preambleText},this.preambleEmitted=!0),this.options.output(b,l)}}}const f=/(?:(\d{2}):)?(\d{2}):(\d{2}).(\d{3})/;export const parseSubtitleTimestamp=n=>{const e=f.exec(n);if(!e)throw new Error("Expected match.");return 3600*1e3*Number(e[1]||"0")+60*1e3*Number(e[2])+1e3*Number(e[3])+Number(e[4])},formatSubtitleTimestamp=n=>{const e=Math.floor(n/36e5),t=Math.floor(n%(3600*1e3)/(60*1e3)),r=Math.floor(n%(60*1e3)/1e3),o=n%1e3;return e.toString().padStart(2,"0")+":"+t.toString().padStart(2,"0")+":"+r.toString().padStart(2,"0")+"."+o.toString().padStart(3,"0")};
