import { computed, ref } from 'vue'
import { storeToRefs } from 'pinia'
import { useFilesStore } from '@/stores/files'
import type { FileEntry } from '@/types/api'
import { joinRemotePath } from '@/utils/remotePath'

export function useFilesSelection() {
  const store = useFilesStore()
  const { currentPath } = storeToRefs(store)
  const selectedFiles = ref<FileEntry[]>([])
  const selectedFileCount = computed(() => selectedFiles.value.length)

  function clearSelection() {
    selectedFiles.value = []
  }

  function selectedAbsolutePaths(extra?: FileEntry[]): string[] {
    const items = extra?.length ? extra : selectedFiles.value
    return items.map((f) => joinRemotePath(currentPath.value, f.name))
  }

  return {
    selectedFiles,
    selectedFileCount,
    clearSelection,
    selectedAbsolutePaths,
  }
}
