import Cookies from 'js-cookie'
import { fromBrowserLang } from '@/utils/i18n'

const useAppStore = defineStore(
  'app',
  {
    state: () => ({
      sidebar: {
        opened: Cookies.get('sidebarStatus') ? !!+Cookies.get('sidebarStatus') : true,
        withoutAnimation: false,
        hide: false
      },
      device: 'desktop',
      size: Cookies.get('size') || 'default',
      lang: Cookies.get('lang') || fromBrowserLang(),
      // 1=显示轮播图
      showBanner: Cookies.get('showBanner') || '1',
      // 发送验证码倒计时
      countdown: Cookies.get('cd') || '0',
      kycStatus: false,
      // mt账号样式 line,block
      accountStyle: Cookies.get('accountStyle') || 'line'
    }),
    actions: {
      toggleSideBar(withoutAnimation) {
        if (this.sidebar.hide) {
          return false;
        }
        this.sidebar.opened = !this.sidebar.opened
        this.sidebar.withoutAnimation = withoutAnimation
        if (this.sidebar.opened) {
          Cookies.set('sidebarStatus', 1)
        } else {
          Cookies.set('sidebarStatus', 0)
        }
      },
      closeSideBar({ withoutAnimation }) {
        Cookies.set('sidebarStatus', 0)
        this.sidebar.opened = false
        this.sidebar.withoutAnimation = withoutAnimation
      },
      toggleDevice(device) {
        this.device = device
      },
      setSize(size) {
        this.size = size;
        Cookies.set('size', size)
      },
      toggleSideBarHide(status) {
        this.sidebar.hide = status
      },
      // 设置前端语言
      changeLanguage(data) {
        this.lang = data
        Cookies.set('lang', data)
      },
      // 设置是否显示banner
      setShowBanner(data) {
        this.showBanner = data
        Cookies.set('showBanner', data)
      },
      setCountdown(data) {
        this.countdown = data + ''
        Cookies.set('cd', data + '')
      },
      setKycStatus(data) {
        this.kycStatus = data
      },
      setAccountStyle(data) {
        this.accountStyle = data
        Cookies.set('accountStyle', data)
      }
    }
  })

export default useAppStore
