import {parseText} from 'media-captions';
import Fetch from "@11ty/eleventy-fetch";
import path from 'path';

class SearchIndex {
  data() {
    return {eleventyExcludeFromCollections:["episode"], layout: null}
  }

  async render (data) {
    const episodesWithTranscript = data.collections.episode.filter(e=>e.data.podcast?.transcriptPage)
    const result = episodesWithTranscript.map((episode)=>{
      const transcriptPage = data.collections.transcript.find(t=>t.data.episode==episode.data.episode && t.data.season == episode.data.season)
      if (!transcriptPage) {return null}
      return {
        transcriptPageUrl: this.url(transcriptPage.url), 
        episode: episode.data.episode,
        season: episode.data.season,
        title: episode.data.title,
        url: `${this.url(episode.url)}`,
        segments: transcriptPage.data.segments
      }
      })
    return JSON.stringify(result.filter(r=>r))
  }
}

export default SearchIndex