import customersAiTools from "./ai-tools.js";
import { createLogger } from "@open-mercato/shared/lib/logger";
const logger = createLogger("customers");
const UUID_REGEX = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
function isUuid(value) {
  return typeof value === "string" && UUID_REGEX.test(value);
}
function findTool(name) {
  return customersAiTools.find((tool) => tool.name === name) ?? null;
}
function buildToolContext(container, tenantId, organizationId) {
  return {
    tenantId,
    organizationId,
    userId: null,
    container,
    userFeatures: [],
    isSuperAdmin: true,
    apiKeySecret: void 0,
    sessionId: void 0
  };
}
function renderContextBlock(label, payload) {
  return `## Page context \u2014 ${label}
${JSON.stringify(payload, null, 2)}`;
}
const PERSON_ENTITY_TYPES = /* @__PURE__ */ new Set([
  "person",
  "customers.person",
  "customers:customer_entity"
]);
const COMPANY_ENTITY_TYPES = /* @__PURE__ */ new Set([
  "company",
  "customers.company"
]);
const DEAL_ENTITY_TYPES = /* @__PURE__ */ new Set([
  "deal",
  "customers.deal"
]);
async function hydrateWithTool(toolName, inputArgs, toolContext) {
  const tool = findTool(toolName);
  if (!tool) {
    logger.warn("resolvePageContext: tool not registered", { component: "account_assistant", toolName });
    return null;
  }
  try {
    const result = await tool.handler(inputArgs, toolContext);
    if (!result || typeof result !== "object") return null;
    if (result.found === false) return null;
    return result;
  } catch (error) {
    logger.warn("resolvePageContext: tool failed; skipping", { component: "account_assistant", toolName, err: error });
    return null;
  }
}
async function hydrateCustomersAccountContext(input) {
  const tenantId = input.tenantId;
  if (!tenantId) return null;
  if (!isUuid(input.recordId)) return null;
  const entityType = input.entityType.trim().toLowerCase();
  if (!entityType) return null;
  const toolContext = buildToolContext(input.container, tenantId, input.organizationId);
  if (PERSON_ENTITY_TYPES.has(entityType)) {
    const result = await hydrateWithTool(
      "customers.get_person",
      { personId: input.recordId, includeRelated: true },
      toolContext
    );
    if (!result) return null;
    return renderContextBlock(`Person ${input.recordId}`, result);
  }
  if (COMPANY_ENTITY_TYPES.has(entityType)) {
    const result = await hydrateWithTool(
      "customers.get_company",
      { companyId: input.recordId, includeRelated: true },
      toolContext
    );
    if (!result) return null;
    return renderContextBlock(`Company ${input.recordId}`, result);
  }
  if (DEAL_ENTITY_TYPES.has(entityType)) {
    const result = await hydrateWithTool(
      "customers.get_deal",
      { dealId: input.recordId, includeRelated: true },
      toolContext
    );
    if (!result) return null;
    return renderContextBlock(`Deal ${input.recordId}`, result);
  }
  return null;
}
export {
  hydrateCustomersAccountContext
};
//# sourceMappingURL=ai-agents-context.js.map
