import * as fs from "fs";
import * as path from "path";
import * as crypto from "crypto";
import * as os from "os";
const SERVICE_NAME = "com.streamline.sound";
// Fallback storage location
const CREDENTIALS_DIR = path.join(os.homedir(), ".sound-app");
const CREDENTIALS_FILE = path.join(CREDENTIALS_DIR, ".cf-credentials");
const ENCRYPTION_KEY = crypto.scryptSync("sound-app-cf-token", "salt", 32);
export class CredentialsManager {
    /**
   * Ensure credentials directory exists
   */ static ensureDirectory() {
        if (!fs.existsSync(CREDENTIALS_DIR)) {
            fs.mkdirSync(CREDENTIALS_DIR, {
                recursive: true
            });
        }
    }
    /**
   * Encrypt data for file storage
   */ static encrypt(text) {
        const iv = crypto.randomBytes(16);
        const cipher = crypto.createCipheriv("aes-256-cbc", ENCRYPTION_KEY, iv);
        let encrypted = cipher.update(text, "utf8", "hex");
        encrypted += cipher.final("hex");
        return iv.toString("hex") + ":" + encrypted;
    }
    /**
   * Decrypt data from file storage
   */ static decrypt(text) {
        const parts = text.split(":");
        const iv = Buffer.from(parts[0], "hex");
        const encryptedText = parts[1];
        const decipher = crypto.createDecipheriv("aes-256-cbc", ENCRYPTION_KEY, iv);
        let decrypted = decipher.update(encryptedText, "hex", "utf8");
        decrypted += decipher.final("utf8");
        return decrypted;
    }
    /**
   * Store Cloudflare credentials securely
   */ static async setCloudflareCredentials(credentials) {
        this.ensureDirectory();
        // Handle both old format (with password) and new format (with apiToken)
        const fullCredentials = {
            apiToken: credentials.apiToken || credentials.password || "",
            accountId: credentials.accountId || "",
            zoneId: credentials.zoneId || "",
            email: credentials.email || ""
        };
        // Store encrypted credentials
        const encrypted = this.encrypt(JSON.stringify(fullCredentials));
        await fs.promises.writeFile(CREDENTIALS_FILE, encrypted, "utf8");
        console.info("Credentials saved successfully (no keytar)");
    }
    /**
   * Retrieve Cloudflare credentials
   */ static async getCloudflareCredentials() {
        try {
            const encrypted = await fs.promises.readFile(CREDENTIALS_FILE, "utf8");
            const decrypted = this.decrypt(encrypted);
            const credentials = JSON.parse(decrypted);
            if (!credentials.apiToken) {
                return null;
            }
            return credentials;
        } catch (error) {
            console.info("No stored credentials found");
            return null;
        }
    }
    /**
   * Delete Cloudflare credentials
   */ static async deleteCloudflareCredentials() {
        try {
            await fs.promises.unlink(CREDENTIALS_FILE);
            console.info("Credentials deleted successfully");
        } catch  {
        // File might not exist
        }
    }
    /**
   * Check if credentials are stored
   */ static async hasCredentials() {
        const credentials = await this.getCloudflareCredentials();
        return credentials !== null;
    }
}

//# sourceMappingURL=credentials.js.map