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

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

interface UseSecureStorageReturns {
  /**
   * Get the secret from the secure storage.
   */
  getSecret: () => Promise<string | null>
  /**
   * Set a secret in the secure storage.
   */
  setSecret: (params: SetSecretParams) => Promise<void>
  /**
   * Remove the secret from the secure storage.
   */
  removeSecret: () => Promise<void>
}

export function useSecureStorage(): UseSecureStorageReturns {
  const {getSecret, setSecret, removeSecret} = useShopActions()

  return {
    getSecret: useHandleAction(getSecret),
    setSecret: useHandleAction(setSecret),
    removeSecret: useHandleAction(removeSecret),
  }
}
