import DOMPurify from 'dompurify'

let instance: typeof DOMPurify

function isNodeJsEnvironment(): boolean {
  return (
    // eslint-disable-next-line no-restricted-globals
    typeof process !== 'undefined' &&
    // eslint-disable-next-line no-restricted-globals
    typeof process.versions !== 'undefined' &&
    // eslint-disable-next-line no-restricted-globals
    typeof process.versions.node !== 'undefined'
  )
}
if (isNodeJsEnvironment()) {
  const { JSDOM } = await import('jsdom')
  const { window } = new JSDOM('<!DOCTYPE html>')
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  // @ts-ignore
  instance = DOMPurify(window)
} else {
  instance = DOMPurify
}

export default instance
