import a from"lru-cache";import e from"node:fs";import h from"node:path";import l from"node:os";import{exit as C}from"../utils/process.js";import{PORTAL_VERSION as i}from"../version.js";const o=".cache/babel-plugin",s=e.existsSync("node_modules")?`node_modules/${o}`:h.join(l.tmpdir(),".redocly",o),c="styled-cache.json",m=5e3,p=15e3,r="_package-version";class _{cache;constructor(){this.cache=new a({max:m}),this.initCache(),this.setupListeners()}get(t){return this.cache.get(t)}set(t,n){this.cache.set(t,n)}initCache(){if(!e.existsSync(`${s}/${c}`)){this.cache.load([]);return}const t=JSON.parse(e.readFileSync(`${s}/${c}`,"utf-8"));try{if(this.cache.load(t),this.cache.get(r)?.contents!==i)throw new Error("Cache version mismatch")}catch{this.cache.load([]),e.unlinkSync(`${s}/${c}`)}}storeCacheFile(){e.existsSync(s)||e.mkdirSync(s,{recursive:!0}),this.cache.set(r,{contents:i||"",hash:""}),e.writeFileSync(`${s}/${c}`,JSON.stringify(this.cache.dump()),"utf-8")}setupListeners(){process.on("SIGINT",()=>{this.storeCacheFile(),C(0)}),process.on("exit",()=>{this.storeCacheFile()}),setInterval(()=>{this.storeCacheFile()},p).unref()}}const y=new _;export{_ as BabelCache,y as babelCache};
