#!/bin/sh
set -e

node <<'EOF'
const assert = require('assert')
const fs = require('fs')
const vm = require('vm')

for (const filename of [
  '/usr/share/javascript/fullcalendar/index.global.js',
  '/usr/share/javascript/fullcalendar/index.global.min.js',
]) {
  const context = {}
  vm.runInNewContext(fs.readFileSync(filename, 'utf8'), context, { filename })
  assert.strictEqual(typeof context.FullCalendar.Calendar, 'function')
}
EOF

node --input-type=module <<'EOF'
const { access } = await import('node:fs/promises')
await access('/usr/share/nodejs/@fullcalendar/core/index.d.ts')

const fullcalendar = await import('/usr/share/nodejs/fullcalendar/index.js')
if (typeof fullcalendar.Calendar !== 'function') {
  throw new Error('fullcalendar ESM did not export Calendar')
}

for (const moduleName of [
  'bootstrap5',
  'format-luxon3',
  'format-moment',
  'google-calendar',
  'icalendar',
  'preact',
  'react',
  'rrule',
]) {
  const mod = await import(`/usr/share/nodejs/@fullcalendar/${moduleName}/index.js`)
  if (!(mod.default || mod.Calendar || Object.keys(mod).length)) {
    throw new Error(`${moduleName} ESM did not export anything`)
  }
}

{
  const mod = await import('/usr/share/nodejs/@full-ui/headless-calendar/index.js')
  if (!(mod.default || Object.keys(mod).length)) {
    throw new Error('@full-ui/headless-calendar ESM did not export anything')
  }
}

{
  const mod = await import('/usr/share/nodejs/fullcalendar-w-react/index.js')
  if (!(mod.default || mod.Calendar || Object.keys(mod).length)) {
    throw new Error('fullcalendar-w-react ESM did not export anything')
  }
}
EOF
