import { mkdir } from 'node:fs/promises';
import { join } from 'node:path';
const activeProfileDirs = new Set();
/**
 * Owns Heddle browser profile path resolution and in-process profile locks.
 */
export class BrowserProfileService {
    static async acquire(input) {
        const profileId = input.profileId ?? 'default';
        const userDataDir = join(input.stateRoot, 'browser-profiles', profileId);
        if (activeProfileDirs.has(userDataDir)) {
            throw new Error(`Browser profile "${profileId}" is already in use by this process.`);
        }
        activeProfileDirs.add(userDataDir);
        await mkdir(userDataDir, { recursive: true });
        let released = false;
        return {
            profile: {
                profileId,
                userDataDir,
                channel: input.channel,
                headless: input.headless,
            },
            release() {
                if (released) {
                    return;
                }
                released = true;
                activeProfileDirs.delete(userDataDir);
            },
        };
    }
}
//# sourceMappingURL=service.js.map