/*
 * SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file)
 *
 * SPDX-License-Identifier: AGPL-3.0-only
 */

import { render } from '@testing-library/react'
import { TestMarkdownRenderer } from '../../test-utils/test-markdown-renderer'
import React from 'react'
import type { HighlightedCodeProps } from './highlighted-code'
import * as HighlightedCodeModule from './highlighted-code'
import { mockI18n } from '../../test-utils/mock-i18n'
import { HighlightedCodeMarkdownExtension } from './highlighted-code-markdown-extension'

describe('Highlighted code markdown extension', () => {
  describe('renders', () => {
    beforeAll(async () => {
      jest.spyOn(HighlightedCodeModule, 'HighlightedCode').mockImplementation((({
        code,
        language,
        startLineNumber,
        wrapLines
      }) => {
        return (
          <span>
            this is a mock for highlighted code
            <code>{code}</code>
            <span>language: {language}</span>
            <span>start line number: {startLineNumber}</span>
            <span>wrap line: {wrapLines ? 'true' : 'false'}</span>
          </span>
        )
      }) as React.FC<HighlightedCodeProps>)
      await mockI18n()
    })

    describe('with just the language', () => {
      it("doesn't show a gutter", () => {
        const view = render(
          <TestMarkdownRenderer
            extensions={[new HighlightedCodeMarkdownExtension()]}
            content={'```javascript \nlet x = 0\n```'}
          />
        )
        expect(view.container).toMatchSnapshot()
      })

      describe('and line wrapping', () => {
        it("doesn't show a gutter", () => {
          const view = render(
            <TestMarkdownRenderer
              extensions={[new HighlightedCodeMarkdownExtension()]}
              content={'```javascript! \nlet x = 0\n```'}
            />
          )
          expect(view.container).toMatchSnapshot()
        })
      })
    })

    describe('with the language and show gutter', () => {
      it('shows the correct line number', () => {
        const view = render(
          <TestMarkdownRenderer
            extensions={[new HighlightedCodeMarkdownExtension()]}
            content={'```javascript= \nlet x = 0\n```'}
          />
        )
        expect(view.container).toMatchSnapshot()
      })

      describe('and line wrapping', () => {
        it('shows the correct line number', () => {
          const view = render(
            <TestMarkdownRenderer
              extensions={[new HighlightedCodeMarkdownExtension()]}
              content={'```javascript=! \nlet x = 0\n```'}
            />
          )
          expect(view.container).toMatchSnapshot()
        })
      })
    })

    describe('with the language, show gutter with a start number', () => {
      it('shows the correct line number', () => {
        const view = render(
          <TestMarkdownRenderer
            extensions={[new HighlightedCodeMarkdownExtension()]}
            content={'```javascript=100 \nlet x = 0\n```'}
          />
        )
        expect(view.container).toMatchSnapshot()
      })

      it('shows the correct line number and continues in another codeblock', () => {
        const view = render(
          <TestMarkdownRenderer
            extensions={[new HighlightedCodeMarkdownExtension()]}
            content={'```javascript=100 \nlet x = 0\nlet y = 1\n```\n\n```javascript=+\nlet y = 2\n```\n'}
          />
        )
        expect(view.container).toMatchSnapshot()
      })

      describe('and line wrapping', () => {
        it('shows the correct line number', () => {
          const view = render(
            <TestMarkdownRenderer
              extensions={[new HighlightedCodeMarkdownExtension()]}
              content={'```javascript=100! \nlet x = 0\n```'}
            />
          )
          expect(view.container).toMatchSnapshot()
        })

        it('shows the correct line number and continues in another codeblock', () => {
          const view = render(
            <TestMarkdownRenderer
              extensions={[new HighlightedCodeMarkdownExtension()]}
              content={'```javascript=100! \nlet x = 0\nlet y = 1\n```\n\n```javascript=+\nlet y = 2\n```\n'}
            />
          )
          expect(view.container).toMatchSnapshot()
        })
      })
    })
  })
})
