import {useCallback} from 'react'

import {ReportInteractionParams} from '@shopify/shop-minis-platform/actions'

import {getWindowLocationPathname} from '../utils/getWindowLocationPathname'

import {useHandleAction} from './useHandleAction'
import {useShopActions} from './useShopActions'

interface UseReportInteractionReturns {
  /**
   * Report a user interaction event for analytics.
   */
  reportInteraction: (params: ReportInteractionParams) => Promise<void>
}

export const useReportInteraction = (): UseReportInteractionReturns => {
  const {reportInteraction} = useShopActions()
  const handleAction = useHandleAction(reportInteraction)

  const report = useCallback(
    (params: ReportInteractionParams) => {
      const enrichedParams = {
        ...params,
        pageValue: params.pageValue || getWindowLocationPathname(),
      }
      return handleAction(enrichedParams)
    },
    [handleAction]
  )

  return {reportInteraction: report}
}
