"use strict";var __create=Object.create,__defProp=Object.defineProperty,__getOwnPropDesc=Object.getOwnPropertyDescriptor,__getOwnPropNames=Object.getOwnPropertyNames,__getProtoOf=Object.getPrototypeOf,__hasOwnProp=Object.prototype.hasOwnProperty,__export=(e,t)=>{for(var r in t)__defProp(e,r,{get:t[r],enumerable:!0})},__copyProps=(e,t,r,o)=>{if(t&&"object"==typeof t||"function"==typeof t)for(let i of __getOwnPropNames(t))__hasOwnProp.call(e,i)||i===r||__defProp(e,i,{get:()=>t[i],enumerable:!(o=__getOwnPropDesc(t,i))||o.enumerable});return e},__toESM=(e,t,r)=>(r=null!=e?__create(__getProtoOf(e)):{},__copyProps(!t&&e&&e.__esModule?r:__defProp(r,"default",{value:e,enumerable:!0}),e)),__toCommonJS=e=>__copyProps(__defProp({},"__esModule",{value:!0}),e),registry_exports={};__export(registry_exports,{Registry:()=>Registry,baseDaemonDir:()=>baseDaemonDir,clientKey:()=>clientKey,createClientInfo:()=>createClientInfo,explicitSessionName:()=>explicitSessionName,resolveSessionName:()=>resolveSessionName}),module.exports=__toCommonJS(registry_exports);var import_crypto=__toESM(require("crypto")),import_fs=__toESM(require("fs")),import_os=__toESM(require("os")),import_path=__toESM(require("path")),import_package=require("../../package");function clientKey(e){return e.workspaceDir||e.workspaceDirHash}class Registry{constructor(e){this._files=e}entry(e,t){let r=clientKey(e);return(this._files.get(r)||[]).find(e=>e.config.name===t)}entries(e){return this._files.get(clientKey(e))||[]}entryMap(){return this._files}async loadEntry(e,t){let r=await Registry._loadSessionEntry(e.daemonProfilesDir,t+".session");if(!r)throw Error(`Could not start the session "${t}"`);let o=clientKey(e),i=this._files.get(o);i||(i=[],this._files.set(o,i));let s=i.findIndex(e=>e.config.name===t);return -1!==s&&i.splice(s,1),i.push(r),r}static async _loadSessionEntry(e,t){try{let r=import_path.default.join(e,t),o=await import_fs.default.promises.readFile(r,"utf-8"),i=JSON.parse(o);return i.name||(i.name=import_path.default.basename(t,".session")),i.timestamp||(i.timestamp=0),{file:r,config:i,daemonDir:e}}catch{return}}static async load(){let e=new Map;for(let t of(await import_fs.default.promises.readdir(baseDaemonDir).catch(()=>[]))){let r=import_path.default.join(baseDaemonDir,t);if((await import_fs.default.promises.stat(r)).isDirectory())for(let o of(await import_fs.default.promises.readdir(r).catch(()=>[]))){if(!o.endsWith(".session"))continue;let i=await Registry._loadSessionEntry(r,o);if(!i)continue;let s=i.config.workspaceDir||t,a=e.get(s);a||(a=[],e.set(s,a)),a.push(i)}}return new Registry(e)}}const baseDaemonDir=(()=>{let e;if(process.env.PLAYWRIGHT_DAEMON_SESSION_DIR)return process.env.PLAYWRIGHT_DAEMON_SESSION_DIR;if("linux"===process.platform&&(e=process.env.XDG_CACHE_HOME||import_path.default.join(import_os.default.homedir(),".cache")),"darwin"===process.platform&&(e=import_path.default.join(import_os.default.homedir(),"Library","Caches")),"win32"===process.platform&&(e=process.env.LOCALAPPDATA||import_path.default.join(import_os.default.homedir(),"AppData","Local")),!e)throw Error("Unsupported platform: "+process.platform);return import_path.default.join(e,"ms-playwright","daemon")})();function createClientInfo(){let e=findWorkspaceDir(process.cwd()),t=process.env.PLAYWRIGHT_CLI_VERSION_FOR_TEST||import_package.packageJSON.version,r=import_crypto.default.createHash("sha1");r.update(e||import_package.packageRoot);let o=r.digest("hex").substring(0,16);return{version:t,workspaceDir:e,workspaceDirHash:o,daemonProfilesDir:daemonProfilesDir(o),homeDir:import_os.default.homedir()}}function findWorkspaceDir(e){let t=e;for(let e=0;e<10;e++){if(import_fs.default.existsSync(import_path.default.join(t,".playwright")))return t;let e=import_path.default.dirname(t);if(e===t)break;t=e}}const daemonProfilesDir=e=>import_path.default.join(baseDaemonDir,e);function explicitSessionName(e){return e||process.env.PLAYWRIGHT_CLI_SESSION}function resolveSessionName(e){return explicitSessionName(e)||"default"}