"use strict"; var __defProp = Object.defineProperty; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __publicField = (obj, key, value) => { __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); return value; }; Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); const errors = require("./errors.cjs"); const createDebugger = require("./lib/createDebugger.cjs"); const pathToFilePath = require("./lib/pathToFilePath.cjs"); const debug = createDebugger.createDebugger("akte:files"); const debugRender = createDebugger.createDebugger("akte:files:render"); const debugCache = createDebugger.createDebugger("akte:files:cache"); class AkteFiles { constructor(definition) { __publicField(this, "definition"); __publicField(this, "_dataPromiseMap", /* @__PURE__ */ new Map()); __publicField(this, "getDataPromise", (context) => { const maybePromise = this._dataPromiseMap.get(context.path); if (maybePromise) { debugCache("using cached data %o", context.path); return maybePromise; } debugCache("retrieving data... %o", context.path); let promise; if (this.definition.data) { promise = this.definition.data(context); } else if (this.definition.bulkData) { const dataFromBulkData = async (path) => { const bulkData = await this.definition.bulkData({ globalData: context.globalData }); if (path in bulkData) { return bulkData[path]; } throw new errors.NotFoundError(path); }; promise = dataFromBulkData(context.path); } else { throw new Error(`Cannot render file for path \`${context.path}\`, no \`data\` or \`bulkData\` function available`); } if (promise instanceof Promise) { promise.then(() => { debugCache("retrieved data %o", context.path); }).catch(() => { }); } else { debugCache("retrieved data %o", context.path); } this._dataPromiseMap.set(context.path, promise); return promise; }); __publicField(this, "_bulkDataPromise"); __publicField(this, "getBulkDataPromise", (context) => { var _a, _b; if (!this._bulkDataPromise) { debugCache("retrieving bulk data... %o", this.path); const bulkDataPromise = ((_b = (_a = this.definition).bulkData) == null ? void 0 : _b.call(_a, context)) || {}; if (bulkDataPromise instanceof Promise) { bulkDataPromise.then(() => { debugCache("retrieved bulk data %o", this.path); }); } else { debugCache("retrieved bulk data %o", this.path); } this._bulkDataPromise = bulkDataPromise; } else { debugCache("using cached bulk data %o", this.path); } return this._bulkDataPromise; }); this.definition = definition; debug("created %o", this.path); } /** Path pattern of this Akte files. */ get path() { return this.definition.path; } /** @internal Prefer {@link AkteApp.render} or use at your own risks. */ async render(args) { const data = await this.getDataPromise(args); return this.definition.render({ path: args.path, globalData: args.globalData, data }); } /** @internal Prefer {@link AkteApp.renderAll} or use at your own risks. */ async renderAll(args) { if (!this.definition.bulkData) { debugRender("no files to render %o", this.path); return {}; } debugRender("rendering files... %o", this.path); const bulkData = await this.getBulkDataPromise(args); const render = async (path, data) => { const content = await this.definition.render({ path, globalData: args.globalData, data }); debugRender("rendered %o", path); return [pathToFilePath.pathToFilePath(path), content]; }; const promises = []; for (const path in bulkData) { const data = bulkData[path]; promises.push(render(path, data)); } const fileEntries = await Promise.all(Object.values(promises)); debugRender(`rendered %o ${fileEntries.length > 1 ? "files" : "file"} %o`, fileEntries.length, this.path); return Object.fromEntries(fileEntries); } /** @internal Prefer {@link AkteApp.clearCache} or use at your own risks. */ clearCache() { this._dataPromiseMap = /* @__PURE__ */ new Map(); this._bulkDataPromise = void 0; } } exports.AkteFiles = AkteFiles; //# sourceMappingURL=AkteFiles.cjs.map