import { createRequire as __createRequire } from "node:module"; const require = __createRequire(import.meta.url);

// src/posthog-products.ts
var POSTHOG_PRODUCTS = {
  product_analytics: "Product analytics",
  web_analytics: "Web analytics",
  feature_flags: "Feature flags",
  experiments: "Experiments",
  error_tracking: "Error tracking",
  session_replay: "Session replay",
  surveys: "Surveys",
  llm_analytics: "AI observability",
  data_warehouse: "Data warehouse",
  cdp: "Data pipelines",
  logs: "Logs",
  apm: "APM",
  sql: "SQL"
};
var DOMAIN_PRODUCT = {
  // Experiments
  experiment: "experiments",
  // Feature flags
  "feature-flag": "feature_flags",
  "early-access-feature": "feature_flags",
  "scheduled-changes": "feature_flags",
  // Error tracking
  "error-tracking": "error_tracking",
  // Session replay
  "session-recording": "session_replay",
  "visual-review": "session_replay",
  // Surveys
  survey: "surveys",
  // Session replay (Replay Vision)
  vision: "session_replay",
  // LLM analytics. `llm` covers `llm-total-costs`; `llma` covers the whole
  // `llma-*` family (evaluation, clustering, prompt, sentiment, trace, …) in
  // one token rather than one entry per sub-tool.
  llm: "llm_analytics",
  llma: "llm_analytics",
  "agent-feedback": "llm_analytics",
  // Data warehouse. `external-data` covers sources/schemas/sync-logs.
  "external-data": "data_warehouse",
  "read-data-warehouse-schema": "data_warehouse",
  "read-data-schema": "data_warehouse",
  "batch-export": "data_warehouse",
  // Data pipelines (CDP). `cdp-function` covers `cdp-function-templates` too.
  "cdp-function": "cdp",
  "hog-flows-logs": "cdp",
  "hog-flows-metrics": "cdp",
  workflows: "cdp",
  // Logs / APM
  logs: "logs",
  apm: "apm",
  // SQL
  "execute-sql": "sql",
  // Web analytics
  "web-analytics": "web_analytics",
  // Product analytics
  insight: "product_analytics",
  dashboard: "product_analytics",
  action: "product_analytics",
  cohorts: "product_analytics",
  persons: "product_analytics",
  annotation: "product_analytics",
  "event-definition": "product_analytics",
  "custom-property-definition": "product_analytics",
  endpoint: "product_analytics",
  view: "product_analytics",
  "usage-metrics": "product_analytics",
  subscriptions: "product_analytics",
  alert: "product_analytics",
  notebook: "product_analytics",
  // Admin / meta / introspection — recognized but not surfaced.
  project: null,
  user: null,
  accounts: null,
  integration: null,
  "activity-log": null,
  "advanced-activity-logs": null,
  "approval-policy": null,
  "approval-policies": null,
  "change-request": null,
  "docs-search": null,
  "sdk-doctor": null,
  tasks: null,
  "inbox-reports": null,
  "inbox-source-configs": null,
  "signals-scout-runs": null,
  "signals-scout-scratchpad-search": null,
  comment: null
};
var KNOWN_DOMAINS = Object.keys(DOMAIN_PRODUCT);
var escapeRe = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
var DOMAIN_PATTERNS = KNOWN_DOMAINS.map(
  (d) => [d, new RegExp(`(^|-)${escapeRe(d)}s?($|-)`)]
);
var TABLE_PRODUCT = {
  feature_flags: "feature_flags",
  experiments: "experiments",
  events: "product_analytics",
  person: "product_analytics",
  persons: "product_analytics",
  person_distinct_ids: "product_analytics",
  groups: "product_analytics",
  cohort_people: "product_analytics",
  cohortpeople: "product_analytics",
  sessions: "product_analytics",
  raw_sessions: "product_analytics",
  session_replay_events: "session_replay",
  raw_session_replay_events: "session_replay",
  surveys: "surveys",
  logs: "logs"
};
var POSTHOG_SCHEMAS = /* @__PURE__ */ new Set(["system"]);
function normalizePostHogTableRef(ref) {
  const parts = ref.toLowerCase().split(".");
  if (parts.length === 1) return parts[0] ?? null;
  const schema = parts[0] ?? "";
  const table = parts[parts.length - 1] ?? "";
  return POSTHOG_SCHEMAS.has(schema) ? table : null;
}
function extractSqlTables(sql) {
  const tables = [];
  const re = /\b(?:from|join)\s+(["'`]?)([a-zA-Z_][a-zA-Z0-9_.]*)\1/gi;
  let match = re.exec(sql);
  while (match !== null) {
    const ref = match[2];
    if (ref) {
      const table = normalizePostHogTableRef(ref);
      if (table) tables.push(table);
    }
    match = re.exec(sql);
  }
  return tables;
}
function classifyPostHogSqlQuery(sql) {
  const products = /* @__PURE__ */ new Set();
  for (const table of extractSqlTables(sql)) {
    const product = TABLE_PRODUCT[table];
    if (product) products.add(product);
  }
  return [...products];
}
var ACTIVITY_SCOPE_PRODUCT = {
  FeatureFlag: "feature_flags",
  EarlyAccessFeature: "feature_flags",
  Experiment: "experiments",
  ExperimentHoldout: "experiments",
  ExperimentSavedMetric: "experiments",
  ErrorTrackingIssue: "error_tracking",
  Replay: "session_replay",
  SessionRecordingPlaylist: "session_replay",
  Survey: "surveys",
  LLMTrace: "llm_analytics",
  Evaluation: "llm_analytics",
  DataWarehouseSavedQuery: "data_warehouse",
  ExternalDataSource: "data_warehouse",
  ExternalDataSchema: "data_warehouse",
  BatchExport: "data_warehouse",
  BatchImport: "data_warehouse",
  HogFunction: "cdp",
  HogFlow: "cdp",
  Log: "logs",
  LogsAlertConfiguration: "logs",
  LogsExclusionRule: "logs",
  WebAnalyticsFilterPreset: "web_analytics",
  Insight: "product_analytics",
  Dashboard: "product_analytics",
  DashboardWidget: "product_analytics",
  Cohort: "product_analytics",
  Person: "product_analytics",
  Group: "product_analytics",
  Notebook: "product_analytics",
  Action: "product_analytics",
  EventDefinition: "product_analytics",
  PropertyDefinition: "product_analytics",
  Annotation: "product_analytics",
  Endpoint: "product_analytics",
  EndpointVersion: "product_analytics",
  Subscription: "product_analytics",
  AlertConfiguration: "product_analytics",
  Threshold: "product_analytics",
  AlertSubscription: "product_analytics"
};
function classifyPostHogActivityLog(commandText) {
  const start = commandText.indexOf("{");
  const end = commandText.lastIndexOf("}");
  if (start === -1 || end <= start) return [];
  let body;
  try {
    body = JSON.parse(commandText.slice(start, end + 1));
  } catch {
    return [];
  }
  if (!body || typeof body !== "object") return [];
  const scopesField = body.scopes;
  const raw = [
    body.scope,
    ...Array.isArray(scopesField) ? scopesField : []
  ];
  const products = /* @__PURE__ */ new Set();
  for (const s of raw) {
    if (typeof s === "string") {
      const product = ACTIVITY_SCOPE_PRODUCT[s];
      if (product) products.add(product);
    }
  }
  return [...products];
}
function isQueryCall(name) {
  return name === "query" || name.startsWith("query-");
}
function isExecuteSqlCall(name) {
  return name === "execute-sql" || name === "execute_sql";
}
function isActivityLogCall(name) {
  return name === "activity-log" || name.startsWith("activity-log-") || name.startsWith("advanced-activity-logs");
}
function classifyPostHogExecCall(subTool, commandText) {
  const name = subTool.trim().toLowerCase();
  if (isExecuteSqlCall(name)) {
    const fromTables = commandText ? classifyPostHogSqlQuery(commandText) : [];
    return fromTables.length > 0 ? fromTables : ["sql"];
  }
  if (isActivityLogCall(name)) {
    return commandText ? classifyPostHogActivityLog(commandText) : [];
  }
  const product = classifyPostHogSubTool(subTool);
  return product ? [product] : [];
}
function classifyQuery(type) {
  if (type.startsWith("error-tracking")) return "error_tracking";
  if (type.startsWith("session-recording")) return "session_replay";
  if (type.startsWith("llm")) return "llm_analytics";
  if (type === "logs") return "logs";
  if (type.startsWith("apm")) return "apm";
  return "product_analytics";
}
function classifyPostHogSubTool(subTool) {
  const name = subTool.trim().toLowerCase();
  if (!name) return null;
  if (isQueryCall(name)) {
    return classifyQuery(name.slice("query-".length));
  }
  const best = matchDomain(name);
  if (best === null) return null;
  return DOMAIN_PRODUCT[best];
}
function matchDomain(name) {
  let best = null;
  for (const [domain, re] of DOMAIN_PATTERNS) {
    if (re.test(name)) {
      if (best === null || domain.length > best.length) best = domain;
    }
  }
  return best;
}
function isUnclassifiedPostHogSubTool(subTool) {
  const name = subTool.trim().toLowerCase();
  if (!name) return false;
  if (isQueryCall(name) || isExecuteSqlCall(name) || isActivityLogCall(name)) {
    return false;
  }
  return matchDomain(name) === null;
}
export {
  POSTHOG_PRODUCTS,
  classifyPostHogExecCall,
  classifyPostHogSqlQuery,
  classifyPostHogSubTool,
  isUnclassifiedPostHogSubTool
};
//# sourceMappingURL=posthog-products.js.map