/**
 * 统一登录SDK入口文件
 * 支持OAuth2授权码模式，提供完整的认证和权限管理功能
 */

// 核心功能导出
export { Auth } from './core/auth';
export { TokenManager } from './core/token';
export { HttpClient, HttpError } from './core/http';

// 工具类导出
export { Storage } from './utils/storage';
export { RouterGuard, RouterGuardOptions } from './guards/router';

// Vue集成导出
export { VuePlugin, createVuePlugin, useAuth } from './plugins/vue';

// 工具函数导出
export { 
  generateRandomString, 
  parseQueryParams, 
  buildQueryParams, 
  generateAuthorizationUrl, 
  isCallbackUrl 
} from './utils/url';

// 类型定义导出
export type { 
  SDKConfig, 
  UnifiedLoginSDK, 
  UserInfo, 
  RouterInfo,
  EventType
} from './types';

// 创建默认SDK实例
import { SDKConfig, UnifiedLoginSDK } from './types';
import { Auth } from './core/auth';
import { Storage } from './utils/storage';

// 全局单例实例
let defaultSDK: UnifiedLoginSDK | null = null;
let defaultAuth: Auth | null = null;

/**
 * 获取默认SDK实例
 * @returns UnifiedLoginSDK SDK实例
 */
function getDefaultSDK(): UnifiedLoginSDK {
  if (!defaultSDK) {
    const storage = new Storage();
    defaultAuth = new Auth(storage);
    
    defaultSDK = {
      init: (config: SDKConfig) => defaultAuth!.init(config),
      getToken: () => defaultAuth!.getToken(),
      login: (redirectUri?: string) => defaultAuth!.login(redirectUri),
      logout: () => defaultAuth!.logout(),
      handleCallback: () => defaultAuth!.handleCallback(),
      getRoutes: () => defaultAuth!.getRoutes(),
      getUserInfo: () => defaultAuth!.getUserInfo(),
      refreshUserInfo: () => defaultAuth!.refreshUserInfo(),
      isAuthenticated: () => defaultAuth!.isAuthenticated(),
      hasRole: (role: string | string[]) => defaultAuth!.hasRole(role),
      hasAllRoles: (roles: string[]) => defaultAuth!.hasAllRoles(roles),
      hasPermission: (permission: string | string[]) => defaultAuth!.hasPermission(permission),
      hasAllPermissions: (permissions: string[]) => defaultAuth!.hasAllPermissions(permissions),
      on: (event, callback) => defaultAuth!.on(event, callback),
      off: (event, callback) => defaultAuth!.off(event, callback),
      isCallback: () => defaultAuth!.isCallback()
    };
  }
  
  return defaultSDK!;
}

/**
 * 默认导出的SDK实例
 * 提供最简化的使用方式
 */
export const unifiedLoginSDK = getDefaultSDK();

/**
 * 默认导出
 */
export default unifiedLoginSDK;

// 版本信息
export const VERSION = '1.0.0';
export const version = VERSION;

// 便捷别名导出
export {
  unifiedLoginSDK as sdk,
  unifiedLoginSDK as auth
};

// 全局类型声明
declare global {
  interface Window {
    unifiedLoginSDK: typeof unifiedLoginSDK;
  }
}

// 如果在浏览器环境中，将SDK挂载到window对象
if (typeof window !== 'undefined') {
  window.unifiedLoginSDK = unifiedLoginSDK;
}
