import d from"node:path";import{I18N_DIR_NAME as f,L10N_DIR_NAME as u}from"../constants/common.js";import{logger as h}from"../tools/notifiers/logger.js";import{Fs as w}from"./fs.js";import{isL10nPath as n}from"./utils/is-l10n-path.js";import{getLocaleFromRelativePath as c}from"./utils/get-locale-from-relative-path.js";import{removeL10nPrefix as l}from"./utils/remove-l10n-prefix.js";import{LastModifiedTracker as m}from"./last-modified-tracker.js";class M extends w{cwd;ignored;#e=new Map;#s;#t;constructor(e,t=[]){super(e,t),this.cwd=e,this.ignored=t,this.#s=new m(this),this.#r()}get localeFolders(){return Array.from(this.#e.keys())}get localizationFolder(){if(this.#t)return this.#t;let e=!1,t=!1;const s=this.localeFolders;if(!s.length)return;for(const i of s)for(const a of this.#e.get(i)?.values()||[]){const o=a.startsWith(u),p=a.startsWith(f);o&&!e&&(e=!0),p&&!t&&(t=!0)}e&&t&&h.panic("Only one folder `@l10n` should be used for localization"),t&&h.warn("`@i18n` folder is deprecated. Please use `@l10n` folder instead.");const r=e?u:f;return this.#t=r,r}async getFileInfo(e){if(e=e.replace(/^\.\//g,""),n(e)){if(await super.exists(e))return super.getFileInfo(e);const t=l(e),s=await super.getFileInfo(t);return s&&{...s,relativePath:e,realRelativePath:t,dirname:d.posix.dirname(e),isVirtual:!0}}return super.getFileInfo(e)}async readdir(e){if(n(e))throw new Error("readdir is not supported in l10n or i18n directories");return super.readdir(e)}async exists(e){return e=e.replace(/^\.\//g,""),n(e)?await super.exists(e)||await super.exists(l(e)):super.exists(e)}async read(e,t="utf-8"){return n(e)?await super.exists(e)?super.read(e,t):super.read(l(e),t):super.read(e,t)}async scan(e){const t=await super.scan(e),s={},r=new Set;for(const i of t)if(n(i.relativePath)){const a=c(i.relativePath),o=s[a]||new Set;o.add(l(i.relativePath)),s[a]=o}else r.add(i.relativePath);for(const i of r.values())for(const a of this.#e.keys())if(!s[a]?.has(i)){const o=`${this.localizationFolder}/${a}/${i}`;t.push({realRelativePath:i,relativePath:o,dirname:d.posix.dirname(o),isVirtual:!0})}return t}async getLastModified(e){await this.watcher;const t=await this.getFileInfo(e);return this.#s.getLastModified(t?.realRelativePath||e)}async#r(){await this.watcher,this.#e=new Map;for(const e of this.filesList.values())if(n(e.relativePath)){const t=c(e.relativePath),s=this.#e.get(t)||new Set;s.add(e.relativePath),this.#e.set(t,s)}super.watch(async e=>{for(const t of e){if(t.event==="code-updated"||!n(t.path))continue;const s=c(t.path),r=this.#e.get(s)||new Set;switch(t.event){case"add":r.add(t.path),this.#e.set(s,r);break;case"unlink":r&&(r.delete(t.path),r.size===0&&this.#e.delete(s));break}}})}}export{M as ContentFs};
