import {uploadData} from './publishRunner'

export async function downloadStoreAsFile() {
  const {data, fileUrl} = await uploadData()

  const dataStr =
    'data:text/json;charset=utf-8,' +
    encodeURIComponent(
      JSON.stringify({
        data,
        fileUrl
      })
    )

  const fileNameWithDate = `jaenjs-state-${new Date().toISOString()}.json`

  const downloadAnchorNode = document.createElement('a')
  downloadAnchorNode.setAttribute('href', dataStr)
  downloadAnchorNode.setAttribute('download', fileNameWithDate)
  document.body.appendChild(downloadAnchorNode) // required for firefox
  downloadAnchorNode.click()
  downloadAnchorNode.remove()
}
