---
import type { types } from 'react-bricks/astro'
import { reactBricksAstroStore, AstroBrick } from 'react-bricks/astro'
import PageViewer from './PageViewer.astro'

interface Props {
  block: types.IContentBlock
  index?: number
  itemsCount?: number
  page?: types.Page
  [x: string]: unknown
}

let error = false
const { block, index, itemsCount, page, ...props } = Astro.props as Props
const config = reactBricksAstroStore.getConfig()

if (!config) {
  console.warn('React Bricks Store is not initialized')
  error = true
}

reactBricksAstroStore.setBlock(block)

const Brick = reactBricksAstroStore.getBrick(block)

let blockProps = { ...block.props, ...props }

if (!Brick) {
  if (process.env.NODE_ENV === 'development') {
    console.warn(`Missing component for block type "${block.type}"`)
  }
  error = true
} else {
  if (
    Brick.schema.mapExternalDataToProps &&
    typeof Brick.schema.mapExternalDataToProps === 'function' &&
    page?.externalData &&
    typeof page.externalData === 'object'
  ) {
    blockProps = {
      ...blockProps,
      ...Brick.schema.mapExternalDataToProps(page.externalData, block.props),
    }
  }
}

function trimEndChar(str: string, charToRemove: string) {
  return str.endsWith(charToRemove) ? str.slice(0, -1) : str
}

const pathname =
  Astro.url.pathname === '/'
    ? Astro.url.pathname
    : trimEndChar(Astro.url.pathname || '', '/')

const isDev = import.meta.env.DEV
---

{
  error || !Brick ? (
    ''
  ) : blockProps['RB_PAGE_EMBED'] &&
    blockProps['RB_PAGE_EMBED_CONTENT'] &&
    blockProps['RB_PAGE_EMBED_CONTENT'].statusCode !== 404 &&
    blockProps['RB_PAGE_EMBED_CONTENT'].error !== 'Not found' ? (
    <PageViewer page={blockProps['RB_PAGE_EMBED_CONTENT']} main={false} />
  ) : !Brick.schema.astroInteractivity ? (
    <AstroBrick
      blockProps={blockProps}
      index={index}
      itemsCount={itemsCount}
      page={page}
      block={block}
      pathname={pathname}
    />
  ) : Brick.schema.astroInteractivity && isDev ? (
    <AstroBrick
      client:only
      blockProps={blockProps}
      index={index}
      itemsCount={itemsCount}
      page={page}
      block={block}
      pathname={pathname}
    />
  ) : Brick.schema.astroInteractivity === 'load' ||
    Brick.schema.astroInteractivity.load === true ? (
    <AstroBrick
      client:load
      blockProps={blockProps}
      index={index}
      itemsCount={itemsCount}
      page={page}
      block={block}
      pathname={pathname}
    />
  ) : Brick.schema.astroInteractivity === 'idle' ||
    Brick.schema.astroInteractivity.idle === true ? (
    <AstroBrick
      client:idle
      blockProps={blockProps}
      index={index}
      itemsCount={itemsCount}
      page={page}
      block={block}
      pathname={pathname}
    />
  ) : !!Brick.schema.astroInteractivity.idle?.timeout ? (
    <AstroBrick
      client:idle={{
        timeout: Brick.schema.astroInteractivity.idle?.timeout,
      }}
      blockProps={blockProps}
      index={index}
      itemsCount={itemsCount}
      page={page}
      block={block}
      pathname={pathname}
    />
  ) : Brick.schema.astroInteractivity === 'visible' ||
    Brick.schema.astroInteractivity.visible === true ? (
    <AstroBrick
      client:visible
      blockProps={blockProps}
      index={index}
      itemsCount={itemsCount}
      page={page}
      block={block}
      pathname={pathname}
    />
  ) : !!Brick.schema.astroInteractivity.visible?.rootMargin ? (
    <AstroBrick
      client:visible={{
        rootMargin: Brick.schema.astroInteractivity.visible?.rootMargin,
      }}
      blockProps={blockProps}
      index={index}
      itemsCount={itemsCount}
      page={page}
      block={block}
      pathname={pathname}
    />
  ) : !!Brick.schema.astroInteractivity.media ? (
    <AstroBrick
      client:media={Brick.schema.astroInteractivity.media}
      blockProps={blockProps}
      index={index}
      itemsCount={itemsCount}
      page={page}
      block={block}
      pathname={pathname}
    />
  ) : !!Brick.schema.astroInteractivity.only ? (
    <AstroBrick
      client:only={Brick.schema.astroInteractivity.only}
      blockProps={blockProps}
      index={index}
      itemsCount={itemsCount}
      page={page}
      block={block}
      pathname={pathname}
    />
  ) : (
    <AstroBrick
      blockProps={blockProps}
      index={index}
      itemsCount={itemsCount}
      page={page}
      block={block}
      pathname={pathname}
    />
  )
}
