import StaffService from '@/service/StaffService'
import { iOutLet } from '@/service/StaffService/types'

const OUT_LET = 'out-let'

export async function getSelfOutLet(): Promise<iOutLet> {
  const outletStorage = localStorage.getItem(OUT_LET)
  const outlet = (outletStorage && JSON.parse(outletStorage)) || undefined
  if (!outlet) {
    const ret = await StaffService.getInstance().getSelfOutLet()
    if (ret.code === 200) {
      const data: iOutLet = ret.data
      localStorage.setItem(OUT_LET, JSON.stringify(data))
    } else {
      throw new Error('用户网点信息获取失败')
    }
  }
  const storage = localStorage.getItem(OUT_LET)
  const result = (storage && JSON.parse(storage)) || undefined
  return result
}
