import{InputFormat as e}from"./input-format.js";import{assert as a}from"./misc.js";import{Reader as s}from"./reader.js";import{Source as o}from"./source.js";export class Input{constructor(r){if(this._demuxerPromise=null,this._format=null,!r||typeof r!="object")throw new TypeError("options must be an object.");if(!Array.isArray(r.formats)||r.formats.some(t=>!(t instanceof e)))throw new TypeError("options.formats must be an array of InputFormat.");if(!(r.source instanceof o))throw new TypeError("options.source must be a Source.");this._formats=r.formats,this._source=r.source,this._mainReader=new s(r.source)}_getDemuxer(){return this._demuxerPromise??=(async()=>{await this._mainReader.loadRange(0,4096);for(const r of this._formats)if(await r._canReadInput(this))return this._format=r,r._createDemuxer(this);throw new Error("Input has an unsupported or unrecognizable format.")})()}get source(){return this._source}async getFormat(){return await this._getDemuxer(),a(this._format),this._format}async computeDuration(){return(await this._getDemuxer()).computeDuration()}async getTracks(){return(await this._getDemuxer()).getTracks()}async getVideoTracks(){return(await this.getTracks()).filter(t=>t.isVideoTrack())}async getPrimaryVideoTrack(){return(await this.getTracks()).find(t=>t.isVideoTrack())??null}async getAudioTracks(){return(await this.getTracks()).filter(t=>t.isAudioTrack())}async getPrimaryAudioTrack(){return(await this.getTracks()).find(t=>t.isAudioTrack())??null}async getMimeType(){return(await this._getDemuxer()).getMimeType()}}
