/**
 * @vitest-environment happy-dom
 */

import { beforeAll, describe, expect, it } from 'vitest'
import type { TestUtils } from '@factor/api/test-utils/init'
import { createTestUtils } from '@factor/api/test-utils/init'
import { FactorAdmin } from '@factor/plugin-admin'
import { objectId } from '@factor/api'
import { FactorSites } from '..'
import { Site } from '../site'
import { theme } from '../themes/standard'

let siteObj: Site

let testUtils: undefined | TestUtils & { factorAdmin?: FactorAdmin, factorSites?: FactorSites }
describe('iframe editing', () => {
  beforeAll(async () => {
    testUtils = await createTestUtils()
    testUtils.factorAdmin = new FactorAdmin(testUtils)
    testUtils.factorSites = new FactorSites({ ...testUtils, factorAdmin: testUtils.factorAdmin })

    siteObj = new Site({
      siteId: objectId(),
      themeId: 'standard',
      ...theme.toConfig(),
      factorSites: testUtils?.factorSites,
    })

    testUtils.factorSites.activeSite.value = siteObj
  })

  it('gets the correct iframe url', async () => {
    // expect(siteObj.previewUrl.value).toMatchInlineSnapshot(`"http://localhost:6507/site-preview/1454074024321371557"`)

    expect(siteObj.previewUrl.value).toContain(`/site-preview/${siteObj.siteId}`)
  })

  it('gets site from theme', async () => {
    if (!testUtils?.factorSites)
      return

    const s = await testUtils.factorSites.loadSiteFromTheme({ themeId: 'fiction' })
    expect(s?.regions.value.map(r => [
      r.templateKey.value,
      r.regionKey,
      r.tpl.value?.settings.isRegion,
      r.cards.value.length,
    ])).toMatchInlineSnapshot(`
      [
        [
          "engine",
          "page",
          true,
          3,
        ],
        [
          "engine",
          "header",
          true,
          1,
        ],
        [
          "engine",
          "footer",
          true,
          1,
        ],
      ]
    `)
  })
})
