import type { CartStartCheckoutProps } from '@graphcommerce/magento-cart'
import type { PluginConfig, PluginProps } from '@graphcommerce/next-config'
import { useMemoObject } from '@graphcommerce/next-ui'
import { useEffect, useRef } from 'react'
import { useSendEvent } from '../hooks/useSendEvent'
import { cartToBeginCheckout } from '../mapping/cartToBeginCheckout/cartToBeginCheckout'
import { cartToViewCart } from '../mapping/cartToViewCart/cartToViewCart'

export const config: PluginConfig = {
  module: '@graphcommerce/magento-cart',
  type: 'component',
}

export function CartStartCheckout(props: PluginProps<CartStartCheckoutProps>) {
  const { Prev, onStart, ...rest } = props

  const send = useRef(false)
  const sendEvent = useSendEvent()
  const viewCart = useMemoObject(cartToViewCart({ __typename: 'Cart', ...props }))
  useEffect(() => {
    if (!send.current) {
      sendEvent('view_cart', viewCart)
      send.current = true
    }
  }, [sendEvent, viewCart])

  return (
    <Prev
      {...rest}
      onStart={async (e, cart) => {
        await onStart?.(e, cart)
        if (cart) sendEvent('begin_checkout', cartToBeginCheckout(cart))
      }}
    />
  )
}
