/**
 * Persistent browser profile management.
 *
 * A profile is a directory under ~/.avis/browser/profiles/<name>/
 * that Playwright uses as its userDataDir for launchPersistentContext().
 * Cookies, localStorage, service workers, and cache live inside the profile.
 */
import { existsSync, mkdirSync, readdirSync, rmSync } from "node:fs";
import { homedir } from "node:os";
import { join } from "node:path";
export const BROWSER_ROOT = join(homedir(), ".avis", "browser");
export const PROFILES_ROOT = join(BROWSER_ROOT, "profiles");
export const DOWNLOADS_ROOT = join(BROWSER_ROOT, "downloads");
function ensureDir(path) {
    if (!existsSync(path))
        mkdirSync(path, { recursive: true });
}
export class BrowserProfileStore {
    constructor() {
        ensureDir(PROFILES_ROOT);
        ensureDir(DOWNLOADS_ROOT);
    }
    list() {
        if (!existsSync(PROFILES_ROOT))
            return [];
        return readdirSync(PROFILES_ROOT, { withFileTypes: true })
            .filter((d) => d.isDirectory())
            .map((d) => ({ name: d.name, path: join(PROFILES_ROOT, d.name) }));
    }
    has(name) {
        return existsSync(join(PROFILES_ROOT, name));
    }
    /** Returns the profile directory, creating it if missing. */
    ensure(name) {
        const path = join(PROFILES_ROOT, name);
        ensureDir(path);
        return { name, path };
    }
    delete(name) {
        const path = join(PROFILES_ROOT, name);
        if (!existsSync(path))
            return false;
        rmSync(path, { recursive: true, force: true });
        return true;
    }
    downloadsFor(name) {
        const path = join(DOWNLOADS_ROOT, name);
        ensureDir(path);
        return path;
    }
}
//# sourceMappingURL=browser-profile.js.map