import { defineStore } from 'pinia'
import { ref } from 'vue'
import { fetchStakingConfigList, type StakingConfig } from '@/api/staking'

export const useFinanceNavStore = defineStore('financeNav', () => {
  const configs = ref<StakingConfig[]>([])
  const loading = ref(false)
  const loaded = ref(false)

  async function load(force = false) {
    if (loaded.value && !force) return
    loading.value = true
    try {
      const list = await fetchStakingConfigList()
      configs.value = Array.isArray(list) ? list : []
      loaded.value = true
    } catch {
      configs.value = []
    } finally {
      loading.value = false
    }
  }

  function invalidate() {
    loaded.value = false
  }

  return { configs, loading, loaded, load, invalidate }
})
