import { defineStore } from 'pinia'

interface HeaderSearch {
  searchValue: string
  searchResult?: string
  trackingData?: any
}
export const useHeaderSearch = defineStore('headerSearch', {
  state: (): HeaderSearch => ({
    searchValue: JSON.parse(localStorage.getItem('searchData'))?.searchValue || '',
    searchResult: JSON.parse(localStorage.getItem('searchData'))?.searchResult || '',
    trackingData: JSON.parse(localStorage.getItem('searchData'))?.searchResult || {}
  }),
  getters: {},
  actions: {
    setSearchData(searchData: any) {
      localStorage.setItem('searchData', JSON.stringify(searchData))
      this.searchValue = searchData.searchValue
      this.searchResult = searchData?.searchResult
      this.trackingData = searchData?.trackingData
    },

    clearSearchData() {
      localStorage.removeItem('searchData')
      this.searchValue = ''
      this.searchResult = ''
      this.trackingData = ''
    }
  }
})
