import fs from "node:fs";
import os from "node:os";
import path from "node:path";
function tokensPath() {
    return path.join(os.homedir(), ".openclaw", "friday-next", "device-tokens.json");
}
function loadTokens() {
    const file = tokensPath();
    if (!fs.existsSync(file))
        return {};
    try {
        const raw = fs.readFileSync(file, "utf8");
        return JSON.parse(raw);
    }
    catch {
        return {};
    }
}
function saveTokens(tokens) {
    const file = tokensPath();
    fs.mkdirSync(path.dirname(file), { recursive: true });
    fs.writeFileSync(file, JSON.stringify(tokens, null, 2), "utf8");
}
export function saveToken(deviceId, token) {
    const key = deviceId.trim().toUpperCase();
    const tokens = loadTokens();
    tokens[key] = { token, updatedAt: Date.now() };
    saveTokens(tokens);
}
export function getToken(deviceId) {
    const key = deviceId.trim().toUpperCase();
    return loadTokens()[key]?.token ?? null;
}
export function removeToken(deviceId) {
    const key = deviceId.trim().toUpperCase();
    const tokens = loadTokens();
    delete tokens[key];
    saveTokens(tokens);
}
