"use strict"; "use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var __publicField = (obj, key, value) => { __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); return value; }; // src/index.ts var src_exports = {}; __export(src_exports, { Backend: () => Backend }); module.exports = __toCommonJS(src_exports); var import_node_fs = require("fs"); var import_promises = require("fs/promises"); var import_node_url = require("url"); var _Backend = class _Backend { constructor() { __publicField(this, "type", "backend"); __publicField(this, "paths"); __publicField(this, "i18nextOptions"); } init(_, backendOptions, i18nextOptions) { this.paths = backendOptions.paths ?? []; this.i18nextOptions = i18nextOptions; } read(language, namespace, callback) { if (this.paths.length === 0) return callback(new Error("No files registered"), null); if (this.i18nextOptions.initImmediate === true) { try { return callback(null, this.readPathsSync(language, namespace)); } catch (error) { return callback(error, false); } } this.readPaths(language, namespace).then((data) => callback(null, data)).catch((error) => callback(error, false)); } async readPaths(language, namespace) { if (this.paths.length === 1) return _Backend.readPath(_Backend.resolvePath(language, namespace, this.paths[0])); const results = await Promise.allSettled(this.paths.map((path) => _Backend.readPath(_Backend.resolvePath(language, namespace, path)))); return _Backend.handleResults(results); } readPathsSync(language, namespace) { if (this.paths.length === 1) return _Backend.readPathSync(_Backend.resolvePath(language, namespace, this.paths[0])); const results = this.paths.map((path) => { try { return { status: "fulfilled", value: _Backend.readPathSync(_Backend.resolvePath(language, namespace, path)) }; } catch (error) { return { status: "rejected", reason: error }; } }); return _Backend.handleResults(results); } static async readPath(path) { return JSON.parse(await (0, import_promises.readFile)(path, "utf8")); } static readPathSync(path) { return JSON.parse((0, import_node_fs.readFileSync)(path, "utf8")); } static handleResults(results) { const filtered = results.filter((result) => result.status === "fulfilled"); if (filtered.length === 0) { throw new AggregateError( results.map((result) => result.reason), "Could not find the file in any of the registered paths" ); } return Object.assign({}, ...filtered.map((result) => result.value)); } static resolvePath(language, namespace, path) { if (typeof path === "function") return path(language, namespace); if (typeof path !== "string") path = (0, import_node_url.fileURLToPath)(path); return path.replaceAll(/\{\{(?:lng|ns)\}\}/g, (match) => match === "{{lng}}" ? language : namespace); } }; __name(_Backend, "Backend"); __publicField(_Backend, "type", "backend"); var Backend = _Backend; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { Backend }); //# sourceMappingURL=index.cjs.map