import { createContext, useState, useContext } from 'react';
import { getColor } from '@/assets/styles/css';
import { Provider } from '@ant-design/react-native';

import { SafeAreaView } from 'react-native-safe-area-context';
import { StatusBar } from 'react-native';

StatusBar.setHidden(true);

// 创建一个上下文
const AuthContext = createContext();

export const AuthProvider = ({ children }) => {
  const [user, setUser] = useState(null);
  const hasPerm = (roles = []) => {
    if (user && user.roles) {
      return (user.roles || []).some(role => {
        return (roles || []).includes(role);
      });
    } else {
      return false
    }
  }
  const currentLocale = 'zh-CN';
  const themeColor = getColor();

  return (
    <Provider locale={currentLocale} theme={themeColor}>
      <SafeAreaView style={{ flex: 1}}>
        <AuthContext.Provider value={{ user, setUser, hasPerm, themeColor, currentLocale }}>
          {children}
        </AuthContext.Provider>
      </SafeAreaView>
    </Provider>
  );
};

// 自定义 Hook 来使用 AuthContext
export const useContextHook = () => {
  return useContext(AuthContext);
};
