/**
 * Shares API — creating, updating, listing, deleting shares and viewing received shares.
 *
 * Consumers: SharedView.vue, ShareModal.vue
 */

import { request, jsonRequest } from './utils.js'

export function createShare(docId, recipientHandle, permission = 'view') {
  return jsonRequest('/api/shares', 'POST', {
    document_id: docId,
    recipient_handle: recipientHandle,
    permission,
  })
}

export function updateSharePermission(shareId, permission) {
  return jsonRequest(`/api/shares/${shareId}`, 'PATCH', { permission })
}

export function listShares(docId) {
  const params = new URLSearchParams({ document_id: docId })
  return request(`/api/shares?${params}`)
}

export function deleteShare(shareId) {
  return request(`/api/shares/${shareId}`, { method: 'DELETE' })
}

export function getSharedWithMe() {
  return request('/api/shares/received')
}
