import {
  raiseEvent
} from "./chunk-H7IGB2WO.js";
import {
  countResources,
  serializeCounts
} from "./chunk-DAOXTQVS.js";
import {
  getEventCatalogConfigFile,
  verifyRequiredFieldsAreInCatalogConfigFile
} from "./chunk-6QENHZZP.js";

// src/analytics/log-build.js
var getFeatures = async (configFile) => {
  return {
    llmsTxt: configFile.llmsTxt?.enabled || false,
    rss: configFile.rss?.enabled || false,
    chat: configFile.chat?.enabled || false,
    output: configFile.output || "static"
  };
};
var getDirectoryProvider = (source) => {
  if (!source?.name || typeof source.name !== "string") return "unknown";
  return source.name.split(":")[0] || "unknown";
};
var serializeDirectorySources = (configFile) => {
  const sources = configFile.directory?.sources;
  if (!Array.isArray(sources) || sources.length === 0) return "none";
  const providerCounts = sources.reduce((counts, source) => {
    const provider = getDirectoryProvider(source);
    counts[provider] = (counts[provider] || 0) + 1;
    return counts;
  }, {});
  const providers = Object.keys(providerCounts).sort().map((provider) => `${provider}:${providerCounts[provider]}`).join(",");
  const hasUsers = sources.some((source) => typeof source?.loadUsers === "function");
  const hasTeams = sources.some((source) => typeof source?.loadTeams === "function");
  return `sources:${sources.length},providers:${providers},users:${hasUsers},teams:${hasTeams}`;
};
var CLOUD_ANALYTICS_ENDPOINT = "https://api.ecingest.dev/v1/analytics/ingest";
var toCloudResourceCounts = (counts) => ({
  adrs: counts.adrs || 0,
  agents: counts.agents || 0,
  domains: counts.domains || 0,
  services: counts.services || 0,
  events: counts.events || 0,
  commands: counts.commands || 0,
  queries: counts.queries || 0,
  flows: counts.flows || 0,
  channels: counts.channels || 0,
  entities: counts.entities || 0,
  containers: counts.containers || 0,
  dataProducts: counts["data-products"] || 0,
  teams: counts.teams || 0,
  users: counts.users || 0,
  designs: counts.designs || 0,
  diagrams: counts.diagrams || 0,
  ubiquitousLanguages: counts.ubiquitousLanguages || 0
});
var reportCloudResourceInventory = async (configFile, resourceCounts) => {
  const analytics = configFile.cloud?.analytics;
  if (!analytics?.enabled || !analytics.trackingId || !analytics.writeKey) return;
  const endpoint = analytics.endpoint || CLOUD_ANALYTICS_ENDPOINT;
  await fetch(endpoint, {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      "X-EventCatalog-Analytics-Key": analytics.writeKey
    },
    body: JSON.stringify({
      trackingId: analytics.trackingId,
      event: "catalog.resource_inventory_reported",
      timestamp: (/* @__PURE__ */ new Date()).toISOString(),
      counts: toCloudResourceCounts(resourceCounts)
    })
  });
};
var main = async (projectDir, { isEventCatalogStarterEnabled, isEventCatalogScaleEnabled, isBackstagePluginEnabled }) => {
  try {
    await verifyRequiredFieldsAreInCatalogConfigFile(projectDir);
    const configFile = await getEventCatalogConfigFile(projectDir);
    const { cId, organizationName, generators = [] } = configFile;
    let generatorNames = generators.length > 0 ? generators.map((generator) => generator[0]) : ["none"];
    if (isEventCatalogStarterEnabled) {
      generatorNames.push("@eventcatalog/eventcatalog-starter");
    }
    if (isEventCatalogScaleEnabled) {
      generatorNames.push("@eventcatalog/eventcatalog-scale");
    }
    if (isBackstagePluginEnabled) {
      generatorNames.push("@eventcatalog/backstage-plugin-eventcatalog");
    }
    const features = await getFeatures(configFile);
    const resourceCounts = await countResources(projectDir);
    await reportCloudResourceInventory(configFile, resourceCounts);
    await raiseEvent({
      command: "build",
      org: organizationName,
      cId,
      generators: generatorNames.toString(),
      features: Object.keys(features).map((feature) => `${feature}:${features[feature]}`).join(","),
      directorySources: serializeDirectorySources(configFile),
      resources: serializeCounts(resourceCounts)
    });
  } catch (error) {
  }
};
var log_build_default = main;

export {
  log_build_default
};
