import {
  GeneratedTokenData,
  UserTokenGenerateUserErrors,
} from '@shopify/shop-minis-platform'

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

interface UseGenerateUserTokenReturns {
  /**
   * Generates a temporary token for the user.
   */
  generateUserToken: () => Promise<{
    data: GeneratedTokenData
    userErrors?: UserTokenGenerateUserErrors[]
  }>
}

export function useGenerateUserToken(): UseGenerateUserTokenReturns {
  const {generateUserToken} = useShopActions()

  return {
    generateUserToken: useHandleAction(generateUserToken),
  }
}
