import { describe, expect, it } from 'vitest';

import getChangedAlignedLyrics from './getChangedAlignedLyrics';

describe('getChangedAlignedLyrics', () => {
  it('replaces the lyric when the change occurs within a word', () => {
    const alignedLyrics = getChangedAlignedLyrics(
      [
        {
          text: 'Word',
          timing: {
            type: 'range',
            startSeconds: 0,
            endSeconds: 1,
          },
          dirty: false,
        },
      ],
      'World'
    );
    expect(alignedLyrics).toEqual([
      {
        text: 'World',
        timing: {
          type: 'range',
          startSeconds: 0,
          endSeconds: Infinity,
        },
        dirty: true,
      },
    ]);
  });

  it('appends the lyric with time continuing to infinity if the change is at the end and starts with a space', () => {
    const alignedLyrics = getChangedAlignedLyrics(
      [
        {
          text: 'Hello',
          timing: {
            type: 'range',
            startSeconds: 0,
            endSeconds: 1,
          },
          dirty: false,
        },
      ],
      'Hello world'
    );
    expect(alignedLyrics).toEqual([
      {
        text: 'Hello',
        timing: {
          type: 'range',
          startSeconds: 0,
          endSeconds: 1,
        },
        dirty: false,
      },
      {
        text: ' world',
        timing: {
          type: 'range',
          startSeconds: 1,
          endSeconds: Infinity,
        },
        dirty: true,
      },
    ]);
  });

  it('prepends the lyric with time starting at zero if the change is at the start and ends with a space', () => {
    const alignedLyrics = getChangedAlignedLyrics(
      [
        {
          text: 'world',
          timing: {
            type: 'range',
            startSeconds: 1,
            endSeconds: 2,
          },
          dirty: false,
        },
      ],
      'Hello world'
    );
    expect(alignedLyrics).toEqual([
      {
        text: 'Hello ',
        timing: {
          type: 'range',
          startSeconds: 0,
          endSeconds: 1,
        },
        dirty: true,
      },
      {
        text: 'world',
        timing: {
          type: 'range',
          startSeconds: 1,
          endSeconds: 2,
        },
        dirty: false,
      },
    ]);
  });

  it('Handles new words added around an existing timed word by filling time from 0 to Infinity', () => {
    const alignedLyrics = getChangedAlignedLyrics(
      [
        {
          text: 'the',
          timing: {
            type: 'range',
            startSeconds: 1,
            endSeconds: 2,
          },
          dirty: false,
        },
      ],
      'In the end'
    );
    expect(alignedLyrics).toEqual([
      {
        text: 'In ',
        timing: {
          type: 'range',
          startSeconds: 0,
          endSeconds: 1,
        },
        dirty: true,
      },
      {
        text: 'the',
        timing: {
          type: 'range',
          startSeconds: 1,
          endSeconds: 2,
        },
        dirty: false,
      },
      {
        text: ' end',
        timing: {
          type: 'range',
          startSeconds: 2,
          endSeconds: Infinity,
        },
        dirty: true,
      },
    ]);
  });
});
