//
//  KeyboardSharedDataManager.swift
//  Runner
//
//  Created by Destiny on 2025/5/7.
//

import Foundation

class KeyboardSharedDataManager {
    
    static let shared = KeyboardSharedDataManager()
    
    // App Group标识符，需要在主应用和键盘扩展中保持一致
    private let appGroupIdentifier = "group.com.yourdomain.AiKeyboard2025"
    
    // 共享的UserDefaults
    private lazy var sharedUserDefaults: UserDefaults? = {
        return UserDefaults(suiteName: appGroupIdentifier)
    }()
    
    private init() {}
    
    // 保存Token
    func saveToken(_ token: String) {
        sharedUserDefaults?.set(token, forKey: "user_token")
        sharedUserDefaults?.synchronize()
    }
    
    // 获取Token
    func getToken() -> String? {
        return sharedUserDefaults?.string(forKey: "user_token")
    }
    
    // 清除所有数据
    func clearAllData() {
        sharedUserDefaults?.removeObject(forKey: "user_token")
        sharedUserDefaults?.synchronize()
    }
    
    // 检查是否已登录
    func isLoggedIn() -> Bool {
        return getToken() != nil
    }
}
