/**
 * This file is part of the NocoBase (R) project.
 * Copyright (c) 2020-2024 NocoBase Co., Ltd.
 * Authors: NocoBase Team.
 *
 * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
 * For more information, please refer to: https://www.nocobase.com/agreement.
 */

var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
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 resolve_exports = {};
__export(resolve_exports, {
  resolveVariablesBatch: () => resolveVariablesBatch,
  resolveVariablesTemplate: () => resolveVariablesTemplate
});
module.exports = __toCommonJS(resolve_exports);
var import_contexts = require("../template/contexts");
var import_resolver = require("../template/resolver");
var import_registry = require("./registry");
var import_utils = require("./utils");
const GLOBAL_CONTEXT_KEY = Symbol.for("nocobase.flow-engine.variables.global-context");
function getGlobalContext(ctx) {
  var _a, _b;
  const app = ctx.app;
  if (!app[GLOBAL_CONTEXT_KEY]) {
    app[GLOBAL_CONTEXT_KEY] = new import_contexts.GlobalContext((_b = (_a = app.environment) == null ? void 0 : _a.getVariables) == null ? void 0 : _b.call(_a));
  }
  return app[GLOBAL_CONTEXT_KEY];
}
async function resolveVariablesTemplate(ctx, template, contextParams = {}) {
  await (0, import_utils.prefetchRecordsForResolve)(ctx, [{ template, contextParams }]);
  return resolveVariablesTemplateWithPrefetchedRecords(ctx, template, contextParams);
}
async function resolveVariablesTemplateWithPrefetchedRecords(ctx, template, contextParams = {}) {
  const requestCtx = new import_contexts.HttpRequestContext(ctx);
  requestCtx.delegate(getGlobalContext(ctx));
  await import_registry.variables.attachUsedVariables(requestCtx, ctx, template, contextParams);
  return (0, import_resolver.resolveJsonTemplate)(template, requestCtx);
}
async function resolveVariablesBatch(ctx, items) {
  await (0, import_utils.prefetchRecordsForResolve)(
    ctx,
    items.map((item) => ({
      template: (item == null ? void 0 : item.template) ?? {},
      contextParams: (item == null ? void 0 : item.contextParams) || {}
    }))
  );
  const results = [];
  for (const item of items) {
    const data = await resolveVariablesTemplateWithPrefetchedRecords(
      ctx,
      (item == null ? void 0 : item.template) ?? {},
      (item == null ? void 0 : item.contextParams) || {}
    );
    results.push({ id: item == null ? void 0 : item.id, data });
  }
  return results;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  resolveVariablesBatch,
  resolveVariablesTemplate
});
