import { pb } from 'src/boot/pocketbase'

export async function loadAppData (store) {
  return Promise.all([
    pb.collection('stock').getFullList(),
    pb.collection('history').getFullList(),
    pb.collection('categories').getFullList()
  ]).then(([stock, history, categories]) => {
    return store.commit('setFirebaseState', {
      stock,
      history,
      categories
    })
  })
}

export function addToCollection (store, data) {
  return pb.collection(data.collection).create(data.data)
}

export function removeFromCollection (store, data) {
  return pb.collection(data.collection).delete(data.data.id)
}

export function updateItem (store, data) {
  return pb.collection(data.collection).update(data.data.id, data.data)
}

export function deleteItem (store, data) {
  return pb.collection(data.collection).delete(data.data.id)
}

export function getNextShortId (store) {
  return pb.collection('stock').getFirstListItem('short_id>0', {
    sort: '-short_id'
  }).then(item => {
    return item.short_id + 1
  })
}

// export function uploadFile (store, file) {
//   return Firebase.storage().ref().child(Date.now() + file.name).put(file)
// }
