import { AssetMediaEngine } from "./AssetMediaEngine.js";
var AssetIdMediaEngine = class AssetIdMediaEngine extends AssetMediaEngine {
	static async fetchByAssetId(host, _urlGenerator, assetId, apiHost) {
		const url = `${apiHost}/api/v1/isobmff_files/${assetId}/index`;
		const response = await host.fetch(url);
		const data = await response.json();
		return new AssetIdMediaEngine(host, assetId, data, apiHost);
	}
	constructor(host, assetId, data, apiHost) {
		super(host, assetId);
		this.assetId = assetId;
		this.apiHost = apiHost;
		this.data = data;
		const longestFragment = Object.values(this.data).reduce((max, fragment) => Math.max(max, fragment.duration / fragment.timescale), 0);
		this.durationMs = longestFragment * 1e3;
	}
	get initSegmentPaths() {
		const paths = {};
		if (this.audioTrackIndex !== void 0) paths.audio = {
			path: `${this.apiHost}/api/v1/isobmff_tracks/${this.assetId}/${this.audioTrackIndex.track}`,
			pos: this.audioTrackIndex.initSegment.offset,
			size: this.audioTrackIndex.initSegment.size
		};
		if (this.videoTrackIndex !== void 0) paths.video = {
			path: `${this.apiHost}/api/v1/isobmff_tracks/${this.assetId}/${this.videoTrackIndex.track}`,
			pos: this.videoTrackIndex.initSegment.offset,
			size: this.videoTrackIndex.initSegment.size
		};
		return paths;
	}
	get templates() {
		return {
			initSegment: `${this.apiHost}/api/v1/isobmff_tracks/${this.assetId}/{trackId}`,
			mediaSegment: `${this.apiHost}/api/v1/isobmff_tracks/${this.assetId}/{trackId}`
		};
	}
	buildInitSegmentUrl(trackId) {
		return `${this.apiHost}/api/v1/isobmff_tracks/${this.assetId}/${trackId}`;
	}
	buildMediaSegmentUrl(trackId, _segmentId) {
		return `${this.apiHost}/api/v1/isobmff_tracks/${this.assetId}/${trackId}`;
	}
};
export { AssetIdMediaEngine };
