import assert from 'node:assert/strict'

import {
  resolveKnowledgePreviewMode,
  shouldRenderOnlyOfficeHost,
  shouldRenderOnlyOfficePreview
} from '../src/views/scripts/knowledgePreviewMode.js'

function testPrefersOnlyOfficeForSupportedOfficeFileWhenAvailable() {
  const document = {
    extension: 'xlsx',
    previewKind: 'table'
  }

  assert.equal(resolveKnowledgePreviewMode(document, { onlyOfficeAvailable: true }), 'onlyoffice')
}

function testFallsBackToStructuredPreviewForOfficeFileWhenOnlyOfficeUnavailable() {
  const document = {
    extension: 'xlsx',
    previewKind: 'table'
  }

  assert.equal(resolveKnowledgePreviewMode(document, { onlyOfficeAvailable: false }), 'table')
}

function testUsesPreviewKindForNonOnlyOfficeFile() {
  const document = {
    extension: 'pdf',
    previewKind: 'pdf'
  }

  assert.equal(resolveKnowledgePreviewMode(document, { onlyOfficeAvailable: false }), 'pdf')
}

function testRendersOnlyOfficeContainerWhileOfficePreviewIsLoading() {
  const document = {
    extension: 'docx',
    previewKind: 'text'
  }

  assert.equal(
    shouldRenderOnlyOfficePreview(document, {
      onlyOfficeLoading: true,
      onlyOfficeAvailable: false
    }),
    true
  )
}

function testKeepsOnlyOfficeContainerVisibleWhenOfficePreviewHasError() {
  const document = {
    extension: 'docx',
    previewKind: 'text'
  }

  assert.equal(
    shouldRenderOnlyOfficePreview(document, {
      onlyOfficeLoading: false,
      onlyOfficeAvailable: false,
      onlyOfficeError: 'timeout'
    }),
    true
  )
}

function testDoesNotRenderOnlyOfficeContainerAfterFailedMount() {
  const document = {
    extension: 'xlsx',
    previewKind: 'table'
  }

  assert.equal(
    shouldRenderOnlyOfficePreview(document, {
      onlyOfficeLoading: false,
      onlyOfficeAvailable: false
    }),
    false
  )
}

function testRendersOnlyOfficeHostWhileOfficePreviewIsLoading() {
  const document = {
    extension: 'pptx',
    previewKind: 'slides'
  }

  assert.equal(
    shouldRenderOnlyOfficeHost(document, {
      onlyOfficeLoading: true,
      onlyOfficeAvailable: false
    }),
    true
  )
}

function testKeepsOnlyOfficeHostVisibleWhenOfficePreviewHasError() {
  const document = {
    extension: 'xlsx',
    previewKind: 'table'
  }

  assert.equal(
    shouldRenderOnlyOfficeHost(document, {
      onlyOfficeLoading: false,
      onlyOfficeAvailable: false,
      onlyOfficeError: 'timeout'
    }),
    true
  )
}

function testDoesNotRenderOnlyOfficeHostForNonOfficeDocuments() {
  const document = {
    extension: 'pdf',
    previewKind: 'pdf'
  }

  assert.equal(
    shouldRenderOnlyOfficeHost(document, {
      onlyOfficeLoading: true,
      onlyOfficeAvailable: false
    }),
    false
  )
}

function run() {
  testPrefersOnlyOfficeForSupportedOfficeFileWhenAvailable()
  testFallsBackToStructuredPreviewForOfficeFileWhenOnlyOfficeUnavailable()
  testUsesPreviewKindForNonOnlyOfficeFile()
  testRendersOnlyOfficeContainerWhileOfficePreviewIsLoading()
  testKeepsOnlyOfficeContainerVisibleWhenOfficePreviewHasError()
  testDoesNotRenderOnlyOfficeContainerAfterFailedMount()
  testRendersOnlyOfficeHostWhileOfficePreviewIsLoading()
  testKeepsOnlyOfficeHostVisibleWhenOfficePreviewHasError()
  testDoesNotRenderOnlyOfficeHostForNonOfficeDocuments()
  console.log('knowledge preview mode tests passed')
}

run()
