import Taro from '@tarojs/taro'

export enum TabBarKey {
  home = 'home',
  radar = 'radar',
  renmai = 'renmai',
  cardFolder = 'cardFolder',
}

export function getTabParams(key: TabBarKey): Record<string, any> {
  const old = Taro.getStorageSync('tabParams')
  return parseQueryParams(old?.[key] || '')
}

export function consumeTabParams(key: TabBarKey) {
  const old = Taro.getStorageSync('tabParams')
  Taro.setStorageSync('tabParams', {
    ...old,
    [key]: undefined,
  })
}

export function parseQueryParams(search: string) {
  return search.split('&')?.reduce((acc, item) => {
    if (item) {
      const [key, value] = item.split('=')
      acc[key] = value
    }
    return acc
  }, {})
}
