import { ref } from 'vue'
import { defineStore } from 'pinia'
import type { StorageFile } from '@/stores/storage'

export interface Search {
  field: string
  value: string
}

export interface AdvancedSearch {
  op: 'AND' | 'OR'
  field: 'title' | 'keyword'
  value: string
}

export interface AdvancedSearchFields {
  keyword: string[]
  description: string
}

export const useSearchDataStore = defineStore('searched', () => {
  const foundFile = ref<StorageFile[]>([])
  const isAdvSearchCall = ref<boolean>(false)
  const isSearch = ref<Boolean>(false)
  const isActFoundFile = ref<Boolean>(false)
  const searchData = ref<Search>({
    field: 'title',
    value: '',
  })
  const advSearchDataRow = ref<AdvancedSearch[]>([
    {
      op: 'AND',
      field: 'title',
      value: '',
    },
  ])
  const advSearchDataField = ref<AdvancedSearchFields>({
    keyword: [],
    description: '',
  })

  async function getFoundFile(data: StorageFile[]) {
    foundFile.value = data
  }

  return {
    foundFile,
    isSearch,
    isAdvSearchCall,
    isActFoundFile,
    searchData,
    advSearchDataRow,
    advSearchDataField,
    getFoundFile,
  }
})
