import type { TaskAttachment } from '~/stores/projects'

const state = reactive({
  isOpen: false,
  attachments: [] as TaskAttachment[],
  index: 0,
  basePath: '/api/attachments'
})

export function useAttachmentViewer() {
  function open(attachments: TaskAttachment[], startIndex: number, basePath = '/api/attachments') {
    state.attachments = attachments
    state.index = startIndex
    state.basePath = basePath
    state.isOpen = true
  }

  function close() {
    state.isOpen = false
  }

  function next() {
    if (state.index < state.attachments.length - 1) state.index++
  }

  function prev() {
    if (state.index > 0) state.index--
  }

  return { state, open, close, next, prev }
}
