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
  exact: boolean
}

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 isExact = ref<boolean>(false)
  const searchData = ref<Search>({
    field: 'title',
    value: '',
  })
  const advSearchDataRow = ref<AdvancedSearch[]>([
    {
      op: 'AND',
      field: 'title',
      value: '',
      exact: false,
    },
  ])
  const optionsField = ref([
    { label: 'ชื่อเรื่อง (title)', value: 'title' },
    { label: 'คำสำคัญ (keyword)', value: 'keyword' },
    { label: 'หมวดหมู่ (category)', value: 'category' },
    { label: 'เนื้อหาในไฟล์ (content)', value: 'attachment.content' },
    { label: 'เจ้าของผลงาน (author)', value: 'author' },
  ])
  const advSearchDataField = ref<AdvancedSearchFields>({
    keyword: [],
    description: '',
  })

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

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