import zhCN from 'element-plus/es/locale/lang/zh-cn';
import enUS from 'element-plus/es/locale/lang/en';

export const useAppStore = defineStore('app', () => {
  const showLeftSection = ref(true);
  const showRightSection = ref(true);
  const sidebarStatus = useStorage('sidebarStatus', '1');
  const sidebar = reactive({
    opened: sidebarStatus.value ? !!+sidebarStatus.value : true,
    withoutAnimation: false,
    hide: false
  });
  const device = ref('desktop');
  const size = useStorage<'large' | 'default' | 'small'>('size', 'default');
  const zIndex = ref(999);

  // 语言
  const language = useStorage('language', 'zh_CN');
  const languageObj = {
    en_US: enUS,
    zh_CN: zhCN
  };
  const locale = computed(() => {
    return languageObj[language.value];
  });

  const toggleSideBar = (withoutAnimation) => {
    if (sidebar.hide) {
      return false;
    }

    sidebar.opened = !sidebar.opened;
    sidebar.withoutAnimation = withoutAnimation;
    if (sidebar.opened) {
      sidebarStatus.value = '1';
    } else {
      sidebarStatus.value = '0';
    }
  };

  const closeSideBar = ({ withoutAnimation }) => {
    sidebarStatus.value = '0';
    sidebar.opened = false;
    sidebar.withoutAnimation = withoutAnimation;
  };
  const toggleDevice = (d) => {
    device.value = d;
  };
  const setSize = (s) => {
    size.value = s;
  };
  const toggleSideBarHide = (status) => {
    sidebar.hide = status;
  };

  const changeLanguage = (val) => {
    language.value = val;
  };

  const toggleLeftSection = () => {
    showLeftSection.value = !showLeftSection.value;
  };
  const toggleRightSection = () => {
    showRightSection.value = !showRightSection.value;
  };
  const getZIndex = () => {
    zIndex.value = zIndex.value + 1;
    return zIndex.value;
  };
  return {
    device,
    sidebar,
    language,
    locale,
    size,
    showLeftSection,
    showRightSection,
    toggleLeftSection,
    toggleRightSection,
    changeLanguage,
    toggleSideBar,
    closeSideBar,
    toggleDevice,
    setSize,
    toggleSideBarHide,
    getZIndex
  };
});

export default useAppStore;
