import Repo from './repo-hunt-store-repo'

const today = () => new Date().toString().split(' ').slice(0, 4).join(' ')

const todayData = async () => {
  const date = today()
  const REPO_HUNT = new EdgeKV({namespace: 'repo_hunt'})
  const persisted = await REPO_HUNT.get(date)
  return persisted ? JSON.parse(persisted) : []
}

const Day = {
  getRepos: async function() {
    const ids = await todayData()
    return ids.length ? Repo.findMany(ids) : []
  },

  add: async function(id) {
    const date = today()
    let ids = await todayData()
    ids = ids.concat(id)
    const REPO_HUNT = new EdgeKV({namespace: 'repo_hunt'})
    return REPO_HUNT.put(date, JSON.stringify(ids), {expiration: Math.floor(new Date().getTime()/1000)+60*60*24*29})
  },
}

export default Day