export const getLyricsRangeIndexes = (
  alignedLyrics: any,
  startSecs: number | null,
  endSecs: number | null
) => {
  if (!alignedLyrics) {
    return {};
  }
  const lyricsInfillContent =
    alignedLyrics &&
    startSecs !== endSecs &&
    startSecs !== null &&
    endSecs !== null
      ? alignedLyrics
          .map((lyric: any, index: number) => [lyric, index])
          .filter((lyricPair: any) => {
            return (
              (lyricPair[0].start_s > (startSecs || 0) &&
                lyricPair[0].start_s < (endSecs || 0)) ||
              (lyricPair[0].end_s > (startSecs || 0) &&
                lyricPair[0].end_s < (endSecs || 0))
            );
          })
      : null;

  const lyricsInfillIndexes = lyricsInfillContent?.map(
    (lyricPair: any) => lyricPair[1]
  );
  const lyricsInfillStartIndex = lyricsInfillIndexes?.[0];
  const lyricsInfillEndIndex =
    lyricsInfillIndexes?.[(lyricsInfillIndexes?.length || 0) - 1];
  const lyricsStartIndex = alignedLyrics
    .slice(0, lyricsInfillStartIndex)
    .map((lyric: any) => lyric.word)
    .join('').length;

  const lyricsEndIndex =
    alignedLyrics
      .slice(lyricsInfillStartIndex, lyricsInfillEndIndex + 1)
      .map((lyric: any) => lyric.word)
      .join('').length + lyricsStartIndex;

  return {
    startIndex: lyricsStartIndex,
    endIndex: lyricsEndIndex,
  };
};
