import zhCN from 'element-plus/es/locale/lang/zh-cn';
import enUS from 'element-plus/es/locale/lang/en';
import { ModuleKey } from '@/enums/ModuleEnum';

export const useAppStore = defineStore('app', () => {
  const sidebarStatus = useStorage('sidebarStatus', '1');
  const sidebar = reactive({
    // 桌面端固定展开左侧菜单，避免误折叠影响迁移期操作
    opened: true,
    withoutAnimation: false,
    hide: false,

  });
  const device = ref<string>('desktop');
  const isDark = ref<boolean>(false);
  const size = useStorage('size', 'default');

  // 当前激活的顶部大模块（首页 / OTR / 销售 / 项目 / 工资 / 官网）
  const activeModule = useStorage<ModuleKey>('activeModule', ModuleKey.HOME);
  const setActiveModule = (key: ModuleKey): void => {
    activeModule.value = key;
  };

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

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

    // 桌面端不允许折叠菜单
    if (device.value !== 'mobile') {
      sidebar.opened = true;
      sidebarStatus.value = '1';
      sidebar.withoutAnimation = withoutAnimation;
      return true;
    }

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

  const closeSideBar = ({ withoutAnimation }: any): void => {
    // 桌面端保持展开，仅移动端允许收起（抽屉模式）
    if (device.value !== 'mobile') {
      sidebarStatus.value = '1';
      sidebar.opened = true;
      sidebar.withoutAnimation = withoutAnimation;
      return;
    }

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

  const changeLanguage = (val: string): void => {
    language.value = val;
  };
  const getIsDark = (): boolean => {
    return false
  };

  return {
      getIsDark,
    device,
    sidebar,
    language,
    locale,
    size,
    activeModule,
    setActiveModule,
    changeLanguage,
    toggleSideBar,
    closeSideBar,
    toggleDevice,
    setSize,
    toggleSideBarHide
  };
});

export default useAppStore;
