import { ref } from 'vue'
import { defineStore } from 'pinia'
import type { VillageListItem } from '@/api/light/LightVillageApi';

/** 
 * 村庄各页面共享 数据
 */
export const useVillageStore = defineStore('village', () => {
  
  const currentVillage = ref<VillageListItem | null>(null);
  const currentLonlat = ref<{ longitude: number, latitude: number } | null>(null);
  const currentRegion = ref<string | null>(null);
  const myFollowVillages = ref<VillageListItem[]>([]);
  const myJoinedVillages = ref<VillageListItem[]>([]);

  function setCurrentVillage(village: VillageListItem) {
    currentVillage.value = village;
    uni.setStorageSync('currentVillage', village.id);
  }
  function loadCurrentVillage() {
    return uni.getStorageSync('currentVillage') as number|unknown;
  }
  function setCurrentLonlat(lonlat: { longitude: number, latitude: number }) {
    currentLonlat.value = lonlat;
  }
  function setCurrentRegion(region: string) {
    currentRegion.value = region;
  }
  function setMyFollowVillages(villages: VillageListItem[]) {
    myFollowVillages.value = villages;
  }
  function setMyJoinedVillages(villages: VillageListItem[]) {
    myJoinedVillages.value = villages;
  }

  return { 
    currentVillage,
    currentLonlat,
    currentRegion,
    myFollowVillages,
    myJoinedVillages,
    loadCurrentVillage,
    setCurrentVillage,
    setCurrentLonlat,
    setCurrentRegion,
    setMyFollowVillages,
    setMyJoinedVillages,
  }
})
