const ONLYOFFICE_EXTENSIONS = new Set(['docx', 'xlsx', 'pptx'])

function supportsOnlyOfficePreview(document) {
  return ONLYOFFICE_EXTENSIONS.has(String(document?.extension || '').toLowerCase())
}

export function shouldRenderOnlyOfficePreview(document, options = {}) {
  if (!supportsOnlyOfficePreview(document)) {
    return false
  }

  return (
    Boolean(options.onlyOfficeLoading) ||
    Boolean(options.onlyOfficeAvailable) ||
    Boolean(options.onlyOfficeError)
  )
}

export function shouldRenderOnlyOfficeHost(document, options = {}) {
  if (!supportsOnlyOfficePreview(document)) {
    return false
  }

  return (
    Boolean(options.onlyOfficeLoading) ||
    Boolean(options.onlyOfficeAvailable) ||
    Boolean(options.onlyOfficeError)
  )
}

export function resolveKnowledgePreviewMode(document, options = {}) {
  if (!document) {
    return 'none'
  }

  if (supportsOnlyOfficePreview(document) && options.onlyOfficeAvailable) {
    return 'onlyoffice'
  }

  return document.previewKind || 'unsupported'
}

export function canUseOnlyOfficePreview(document) {
  return supportsOnlyOfficePreview(document)
}
