import { b as q } from "./source-identity-Dj3dryN9.js";
function te(e) {
  const i = j(e);
  return B(i) ? i : void 0;
}
function j(e) {
  if (typeof e != "string" || e.trim().length === 0)
    return;
  const i = e.trim().toLowerCase().replace(/_/g, " ");
  switch (i) {
    case "eq":
    case "equals":
    case "=":
      return "=";
    case "neq":
    case "not equals":
    case "not equal":
    case "!=":
      return "!=";
    case "in":
      return "in";
    case "not in":
      return "not_in";
    case "contains":
    case "like":
      return "contains";
    case "not contains":
    case "not like":
      return "not_contains";
    case "between":
      return "between";
    case "not between":
      return "not_between";
    case ">":
    case ">=":
    case "<":
    case "<=":
      return i;
    case "is null":
      return "is_null";
    case "is not null":
      return "is_not_null";
    default:
      return O(e) ? e : void 0;
  }
}
function B(e) {
  return e === "=" || e === "!=" || e === "in" || e === "not_in" || e === "contains" || e === "not_contains" || e === "between" || e === "not_between" || e === ">" || e === ">=" || e === "<" || e === "<=";
}
function O(e) {
  return B(e) || e === "not_contains" || e === "is_null" || e === "is_not_null";
}
function t(e, i, n) {
  return n ? { code: e, message: i, path: n } : { code: e, message: i };
}
function P(e) {
  for (let i = 0; i < e.length; i += 1) {
    const n = e[i];
    if (e.some(
      (o, r) => r !== i && R(o, n)
    ))
      return !1;
  }
  return !0;
}
function x(e, i) {
  return q(e, i);
}
function R(e, i) {
  return x(e, i) && y(e) === y(i);
}
function E(e) {
  return `Metric intent has duplicate metric "${e.name}" with the same aggregate (${y(e)}). Use different aggregates to request both, or remove one entry.`;
}
function S(e) {
  return typeof e == "string" || typeof e == "number" || typeof e == "boolean" || e === null;
}
function C(e, i, n) {
  if (e !== void 0) {
    if (!e || typeof e != "object" || Array.isArray(e)) {
      n.push(
        t(
          "invalid_sql_default_parameters",
          "SQL defaultParameters must be an object keyed by parameter name.",
          i
        )
      );
      return;
    }
    Object.entries(e).forEach(([o, r]) => {
      if (!o.trim()) {
        n.push(
          t(
            "invalid_sql_default_parameter_name",
            "SQL defaultParameters names must be non-empty strings.",
            `${i}.${o}`
          )
        );
        return;
      }
      (Array.isArray(r) ? r.every(S) : S(r)) || n.push(
        t(
          "invalid_sql_default_parameter_value",
          "SQL defaultParameters values must be strings, numbers, booleans, nulls, or arrays of those scalar values.",
          `${i}.${o}`
        )
      );
    });
  }
}
function U(e, i) {
  return v(e, i).length === 1;
}
function L(e, i) {
  return v(e, i).length === 1;
}
function v(e, i) {
  return i.filter(
    (o) => z(e, o)
  );
}
function z(e, i) {
  return G(e, i) ? e.source ? x(i, e) : !0 : !1;
}
function G(e, i) {
  return !(i.name !== e.name || e.role !== void 0 && i.role !== void 0 && i.role !== e.role || Q(e, i) && y(i) !== y(e));
}
function Q(e, i) {
  return e.role === "measure" || i.role === "measure" || e.aggregate !== void 0 || i.aggregate !== void 0;
}
function y(e) {
  return (e.aggregate || "SUM").toUpperCase();
}
const V = /^(\d{4})-(\d{2})-(\d{2})$/, H = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2})(?::(\d{2})(?:\.(\d{1,3}))?)?(Z|[+-]\d{2}:\d{2})$/;
function $(e) {
  if (typeof e != "string")
    return null;
  const i = e.trim(), n = V.exec(i);
  if (n) {
    const [, h, w, A] = n;
    return I(h, w, A) ? new Date(
      Date.UTC(Number(h), Number(w) - 1, Number(A))
    ) : null;
  }
  const o = H.exec(i);
  if (!o)
    return null;
  const [, r, a, s, m, u, l = "00", , f] = o;
  if (!I(r, a, s) || !_(m, 0, 23) || !_(u, 0, 59) || !_(l, 0, 59) || !X(f))
    return null;
  const g = new Date(i);
  return Number.isFinite(g.getTime()) ? g : null;
}
function I(e, i, n) {
  const o = Number(e), r = Number(i), a = Number(n);
  if (!Number.isInteger(o) || !Number.isInteger(r) || !Number.isInteger(a) || r < 1 || r > 12)
    return !1;
  const s = new Date(Date.UTC(o, r - 1, a));
  return s.getUTCFullYear() === o && s.getUTCMonth() === r - 1 && s.getUTCDate() === a;
}
function _(e, i, n) {
  const o = Number(e);
  return Number.isInteger(o) && o >= i && o <= n;
}
function X(e) {
  if (e === "Z")
    return !0;
  const i = /^([+-])(\d{2}):(\d{2})$/.exec(e);
  return !!(i && _(i[2], 0, 23) && _(i[3], 0, 59));
}
function d(e) {
  return typeof e == "string" && e.trim().length > 0;
}
function c(e) {
  return !!(e && typeof e == "object" && !Array.isArray(e));
}
function M(e, i, n) {
  if (e !== void 0) {
    if (!c(e)) {
      n.push(
        t(
          "invalid_pagination",
          "Pagination must be an object with positive integer page and pageSize.",
          i
        )
      );
      return;
    }
    (!Number.isInteger(e.page) || typeof e.page != "number" || e.page < 1) && n.push(
      t(
        "invalid_pagination_page",
        "Pagination page must be a positive integer.",
        `${i}.page`
      )
    ), (!Number.isInteger(e.pageSize) || typeof e.pageSize != "number" || e.pageSize < 1) && n.push(
      t(
        "invalid_pagination_page_size",
        "Pagination pageSize must be a positive integer.",
        `${i}.pageSize`
      )
    );
  }
}
function k(e) {
  const i = /* @__PURE__ */ new Set();
  for (const n of e) {
    if (i.has(n))
      return n;
    i.add(n);
  }
  return null;
}
const Y = /* @__PURE__ */ new Set([
  "SUM",
  "COUNT",
  "AVG",
  "MIN",
  "MAX",
  "MEDIAN",
  "DISTINCT"
]);
function F(e, i, n) {
  if (!e || typeof e != "object") {
    n.push(t("missing_source", "Analytics intent needs a source.", i));
    return;
  }
  if (e.kind === "semantic") {
    d(e.domainId) || n.push(
      t(
        "missing_semantic_domain",
        "Semantic source needs a domainId.",
        `${i}.domainId`
      )
    ), d(e.datasetName) || n.push(
      t(
        "missing_dataset_name",
        "Semantic source needs a datasetName.",
        `${i}.datasetName`
      )
    );
    return;
  }
  if (e.kind === "physical") {
    d(e.connectionId) || n.push(
      t(
        "missing_connection_id",
        "Physical source needs a connectionId.",
        `${i}.connectionId`
      )
    ), d(e.tableName) || n.push(
      t(
        "missing_table_name",
        "Physical source needs a tableName.",
        `${i}.tableName`
      )
    );
    return;
  }
  if (e.kind === "sql") {
    d(e.connectionId) || n.push(
      t(
        "missing_connection_id",
        "SQL source needs a connectionId.",
        `${i}.connectionId`
      )
    );
    return;
  }
  n.push(t("invalid_source_kind", "Source kind is not supported.", i));
}
function p(e, i, n, o = {}) {
  if (!e || !c(e)) {
    o.required && n.push(
      t("missing_field_ref", "Field reference needs a name.", i)
    );
    return;
  }
  d(e.name) || n.push(
    t("missing_field_ref", "Field reference needs a name.", `${i}.name`)
  ), e.source !== void 0 && F(e.source, `${i}.source`, n), Z(e.aggregate, `${i}.aggregate`, n);
}
function Z(e, i, n) {
  e !== void 0 && (typeof e != "string" || !Y.has(e)) && n.push(
    t(
      "invalid_aggregate",
      "Field aggregate must be SUM, COUNT, AVG, MIN, MAX, MEDIAN, or DISTINCT.",
      i
    )
  );
}
function J(e, i) {
  if (e.analysis === void 0)
    return;
  if (!c(e.analysis)) {
    i.push(
      t(
        "invalid_metric_analysis",
        "Metric analysis must be a structured object.",
        "analysis"
      )
    );
    return;
  }
  if (e.analysis.kind !== "period_change") {
    i.push(
      t(
        "invalid_metric_analysis",
        "Metric analysis kind is not supported.",
        "analysis.kind"
      )
    );
    return;
  }
  e.dateField || i.push(
    t(
      "missing_analysis_date_field",
      "Period-change analysis needs a dateField.",
      "dateField"
    )
  ), e.timeGrain || i.push(
    t(
      "missing_analysis_time_grain",
      "Period-change analysis needs a timeGrain.",
      "timeGrain"
    )
  ), e.analysis.orderBy !== void 0 && e.analysis.orderBy !== "absolute_change" && e.analysis.orderBy !== "positive_change" && e.analysis.orderBy !== "negative_change" && e.analysis.orderBy !== "period" && i.push(
    t(
      "invalid_metric_analysis",
      "Period-change analysis orderBy must be absolute_change, positive_change, negative_change, or period.",
      "analysis.orderBy"
    )
  );
  const { timeWindow: n } = e.analysis;
  n !== void 0 && b(n, "analysis.timeWindow", i);
}
function b(e, i, n) {
  if (!c(e)) {
    n.push(
      t(
        "invalid_time_window",
        "Time window must be a structured object.",
        i
      )
    );
    return;
  }
  if (e.kind !== void 0 && e.kind !== "relative" && e.kind !== "absolute" && n.push(
    t(
      "invalid_time_window",
      "Time window kind must be relative or absolute.",
      `${i}.kind`
    )
  ), e.kind === "absolute") {
    if (d(e.start) || n.push(
      t(
        "invalid_time_window",
        "Absolute time window start must be a non-empty ISO date or timestamp string.",
        `${i}.start`
      )
    ), d(e.end) || n.push(
      t(
        "invalid_time_window",
        "Absolute time window end must be a non-empty ISO date or timestamp string.",
        `${i}.end`
      )
    ), d(e.start) && d(e.end)) {
      const o = $(e.start), r = $(e.end);
      o || n.push(
        t(
          "invalid_time_window",
          "Absolute time window start must be a valid ISO date or timestamp.",
          `${i}.start`
        )
      ), r || n.push(
        t(
          "invalid_time_window",
          "Absolute time window end must be a valid ISO date or timestamp.",
          `${i}.end`
        )
      ), o && r && o.getTime() > r.getTime() && n.push(
        t(
          "invalid_time_window",
          "Absolute time window start must be before or equal to end.",
          i
        )
      );
    }
    return;
  }
  e.unit !== "second" && e.unit !== "minute" && e.unit !== "hour" && e.unit !== "day" && e.unit !== "week" && e.unit !== "month" && e.unit !== "quarter" && e.unit !== "year" && n.push(
    t(
      "invalid_time_window",
      "Time window unit must be second, minute, hour, day, week, month, quarter, or year.",
      `${i}.unit`
    )
  ), (typeof e.value != "number" || !Number.isFinite(e.value) || e.value <= 0) && n.push(
    t(
      "invalid_time_window",
      "Time window value must be a positive number.",
      `${i}.value`
    )
  ), e.anchor !== void 0 && e.anchor !== "now" && e.anchor !== "latest_available" && n.push(
    t(
      "invalid_time_window",
      "Time window anchor must be now or latest_available.",
      `${i}.anchor`
    )
  ), e.completeness !== void 0 && e.completeness !== "include_partial" && e.completeness !== "complete_periods" && n.push(
    t(
      "invalid_time_window",
      "Time window completeness must be include_partial or complete_periods.",
      `${i}.completeness`
    )
  );
}
function N(e, i, n) {
  if (e !== void 0) {
    if (!Array.isArray(e)) {
      n.push(
        t(
          "invalid_analytics_filters",
          "Analytics filters must be an array.",
          i
        )
      );
      return;
    }
    e.forEach((o, r) => {
      var u;
      const a = `${i}.${r}`;
      if (!c(o)) {
        n.push(
          t(
            "invalid_analytics_filter",
            "Analytics filter must be a structured object.",
            a
          )
        );
        return;
      }
      const s = o;
      p(s.field, `${a}.field`, n, {
        required: !0
      }), s.scope !== void 0 && s.scope !== "row" && s.scope !== "aggregate" && n.push(
        t(
          "invalid_analytics_filter_scope",
          "Analytics filter scope must be row or aggregate.",
          `${a}.scope`
        )
      ), s.scope === "aggregate" && ((u = s.field) == null ? void 0 : u.role) !== void 0 && s.field.role !== "measure" && n.push(
        t(
          "invalid_analytics_filter_scope",
          "Aggregate-scope analytics filters must target a measure field.",
          `${a}.scope`
        )
      );
      const m = s.operator === "is_null" || s.operator === "is_not_null";
      if (s.operator !== void 0 && s.operator !== "=" && s.operator !== "!=" && s.operator !== "in" && s.operator !== "not_in" && s.operator !== "contains" && s.operator !== "not_contains" && s.operator !== "between" && s.operator !== "not_between" && s.operator !== ">" && s.operator !== ">=" && s.operator !== "<" && s.operator !== "<=" && s.operator !== "is_null" && s.operator !== "is_not_null" && n.push(
        t(
          "invalid_analytics_filter_operator",
          "Analytics filter operator is not supported.",
          `${a}.operator`
        )
      ), s.values === void 0 && !m && n.push(
        t(
          "missing_analytics_filter_value",
          "Analytics filter needs values unless it is a null check.",
          a
        )
      ), m && s.values !== void 0) {
        n.push(
          t(
            "invalid_analytics_filter_values",
            "Null-check analytics filters must not include values.",
            `${a}.values`
          )
        );
        return;
      }
      if (s.values !== void 0 && !Array.isArray(s.values)) {
        n.push(
          t(
            "invalid_analytics_filter_values",
            "Analytics filter values must be an array when provided.",
            `${a}.values`
          )
        );
        return;
      }
      if (s.values !== void 0) {
        const l = s.values.length;
        if (s.operator === "between" || s.operator === "not_between") {
          l !== 2 && n.push(
            t(
              "invalid_analytics_filter_values",
              "Between analytics filters must include exactly two values.",
              `${a}.values`
            )
          );
          return;
        }
        (s.operator === "contains" || s.operator === "not_contains" || s.operator === "=" || s.operator === "!=" || s.operator === ">" || s.operator === ">=" || s.operator === "<" || s.operator === "<=" || s.operator === void 0) && l !== 1 && n.push(
          t(
            "invalid_analytics_filter_values",
            "Scalar analytics filters must include exactly one value. Use in/not_in for multiple values.",
            `${a}.values`
          )
        );
      }
    });
  }
}
function T(e) {
  var o;
  const i = [], n = [];
  if (!e || typeof e != "object")
    return {
      ok: !1,
      errors: [
        t(
          "invalid_analytics_intent",
          "Analytics intent must be a structured object."
        )
      ],
      warnings: n,
      repairHints: [
        {
          code: "invalid_analytics_intent",
          recommendedNextStep: "Send one structured analytics intent object with kind, source, and required fields."
        }
      ]
    };
  if (e.version !== void 0 && e.version !== 1 && i.push(
    t("invalid_version", "Analytics intent version must be 1.", "version")
  ), F(e.source, "source", i), e.kind === "metric") {
    const r = Array.isArray(e.metrics) ? e.metrics.filter(
      (a) => c(a) && d(a.name)
    ) : [];
    if (!Array.isArray(e.metrics) || e.metrics.length === 0 ? i.push(
      t("missing_metric", "Metric intent needs at least one metric.", "metrics")
    ) : e.metrics.some(
      (a) => !c(a) || !d(a.name)
    ) && i.push(
      t(
        "invalid_metric_list",
        "Metric intent metrics must be an array of field references with names.",
        "metrics"
      )
    ), r.forEach((a, s) => {
      p(a, `metrics.${s}`, i, {
        required: !0
      });
    }), r.length > 0 && !P(r)) {
      const a = r.find(
        (s, m) => r.some(
          (u, l) => l !== m && R(u, s)
        )
      );
      a && i.push(
        t(
          "duplicate_metric",
          E(a),
          "metrics"
        )
      );
    }
    if (c(e.primaryMetric) && d(e.primaryMetric.name) && v(
      e.primaryMetric,
      r
    ).length > 1 && i.push(
      t(
        "ambiguous_primary_metric",
        "Metric intent primaryMetric must identify exactly one selected metric.",
        "primaryMetric"
      )
    ), e.primaryMetric !== void 0 && (p(e.primaryMetric, "primaryMetric", i, {
      required: !0
    }), c(e.primaryMetric) && d(e.primaryMetric.name) && r.length > 0 && !U(
      e.primaryMetric,
      r
    ) && i.push(
      t(
        "invalid_primary_metric",
        "Metric intent primaryMetric must match one metric field reference.",
        "primaryMetric"
      )
    )), e.dimensions !== void 0 && !Array.isArray(e.dimensions) ? i.push(
      t(
        "invalid_metric_dimensions",
        "Metric intent dimensions must be an array of field references.",
        "dimensions"
      )
    ) : Array.isArray(e.dimensions) && e.dimensions.some(
      (a) => !c(a) || !d(a.name)
    ) && i.push(
      t(
        "invalid_metric_dimensions",
        "Metric intent dimensions must be an array of field references with names.",
        "dimensions"
      )
    ), e.dateField !== void 0 && p(e.dateField, "dateField", i), e.timeWindow !== void 0 && (b(e.timeWindow, "timeWindow", i), e.dateField || i.push(
      t(
        "missing_time_window_date_field",
        "Metric timeWindow needs a dateField.",
        "dateField"
      )
    )), N(e.filters, "filters", i), Array.isArray(e.dimensions) && e.dimensions.forEach((a, s) => {
      p(a, `dimensions.${s}`, i);
    }), e.orderBy !== void 0) {
      if (!c(e.orderBy))
        i.push(
          t(
            "invalid_metric_order_by",
            "Metric intent orderBy must be a structured object.",
            "orderBy"
          )
        );
      else if (p(e.orderBy.field, "orderBy.field", i, {
        required: !0
      }), e.orderBy.direction !== "asc" && e.orderBy.direction !== "desc" && i.push(
        t(
          "invalid_metric_order_direction",
          "Metric intent orderBy direction must be asc or desc.",
          "orderBy.direction"
        )
      ), c(e.orderBy.field) && d(e.orderBy.field.name)) {
        const a = [
          ...r,
          ...e.timeGrain && e.dateField && c(e.dateField) ? [e.dateField] : [],
          ...Array.isArray(e.dimensions) ? e.dimensions.filter(
            (s) => c(s) && d(s.name)
          ) : []
        ];
        a.length > 0 && !L(
          e.orderBy.field,
          a
        ) && i.push(
          t(
            "invalid_metric_order_by",
            "Metric intent orderBy.field must match one selected metric, grouped dateField, or dimension.",
            "orderBy.field"
          )
        );
      }
    }
    J(e, i);
  } else e.kind === "records" ? (!Array.isArray(e.fields) || e.fields.length === 0 ? i.push(
    t(
      "missing_record_fields",
      "Records intent needs at least one field.",
      "fields"
    )
  ) : e.fields.some(
    (r) => !c(r) || !d(r.name)
  ) && i.push(
    t(
      "invalid_record_fields",
      "Records intent fields must be field references with names.",
      "fields"
    )
  ), Array.isArray(e.fields) && e.fields.forEach((r, a) => {
    p(r, `fields.${a}`, i);
  }), e.dateField !== void 0 && p(e.dateField, "dateField", i), M(e.pagination, "pagination", i), e.timeWindow !== void 0 && (b(e.timeWindow, "timeWindow", i), e.dateField || i.push(
    t(
      "missing_time_window_date_field",
      "Records timeWindow needs a dateField.",
      "dateField"
    )
  )), N(e.filters, "filters", i), Array.isArray(e.filters) && e.filters.forEach((r, a) => {
    c(r) && r.scope === "aggregate" && i.push(
      t(
        "unsupported_record_filter_scope",
        "Records intents support row-scope filters only. Use a metric intent for aggregate-scope filters.",
        `filters.${a}.scope`
      )
    );
  }), e.orderBy !== void 0 && (c(e.orderBy) ? (p(e.orderBy.field, "orderBy.field", i, {
    required: !0
  }), e.orderBy.direction !== "asc" && e.orderBy.direction !== "desc" && i.push(
    t(
      "invalid_record_order_direction",
      "Records intent orderBy direction must be asc or desc.",
      "orderBy.direction"
    )
  )) : i.push(
    t(
      "invalid_record_order_by",
      "Records intent orderBy must be a structured object.",
      "orderBy"
    )
  ))) : e.kind === "inputOptions" ? ((!e.field || !d(e.field.name)) && i.push(
    t(
      "missing_input_options_field",
      "Input options intent needs a field.",
      "field"
    )
  ), p(e.field, "field", i, { required: !0 })) : e.kind === "sql" ? (((o = e.source) == null ? void 0 : o.kind) !== "sql" && i.push(
    t(
      "invalid_sql_source",
      "SQL analytics intent must use a SQL execution source.",
      "source"
    )
  ), d(e.sql) || i.push(t("missing_sql", "SQL analytics intent needs SQL text.", "sql")), C(
    e.defaultParameters,
    "defaultParameters",
    i
  ), Array.isArray(e.fields) && e.fields.forEach((r, a) => {
    p(r, `fields.${a}`, i);
  }), M(e.pagination, "pagination", i)) : i.push(
    t("invalid_analytics_kind", "Analytics kind is not supported.", "kind")
  );
  return {
    ok: i.length === 0,
    errors: i,
    warnings: n,
    repairHints: W(i)
  };
}
function K(e) {
  const i = [], n = [];
  if (!e || typeof e != "object")
    return {
      ok: !1,
      errors: [
        t(
          "invalid_operation_intent",
          "Operation intent must be a structured object."
        )
      ],
      warnings: n,
      repairHints: [
        {
          code: "invalid_operation_intent",
          recommendedNextStep: "Send one typed operation intent with version, kind, and required operation fields."
        }
      ]
    };
  if (e.version !== 1 && i.push(
    t("invalid_version", "Operation intent version must be 1.", "version")
  ), e.kind === "answer_obligations")
    !Array.isArray(e.obligations) || e.obligations.length === 0 ? i.push(
      t(
        "missing_obligations",
        "Answer-obligations operation intent needs at least one obligation.",
        "obligations"
      )
    ) : e.obligations.forEach((o, r) => {
      d(o.id) || i.push(
        t(
          "missing_obligation_id",
          "Each analytics obligation needs an id.",
          `obligations.${r}.id`
        )
      ), d(o.prompt) || i.push(
        t(
          "missing_obligation_prompt",
          "Each analytics obligation needs a prompt.",
          `obligations.${r}.prompt`
        )
      );
    });
  else if (e.kind === "dashboard_change" || e.kind === "data_app_change") {
    if (d(e.instruction) || i.push(
      t(
        "missing_instruction",
        "Change operation intent needs an instruction.",
        "instruction"
      )
    ), e.analyticsIntent) {
      const o = T(
        e.analyticsIntent
      );
      i.push(...o.errors), n.push(...o.warnings);
    }
  } else
    i.push(
      t(
        "invalid_operation_kind",
        "Operation intent kind is not supported.",
        "kind"
      )
    );
  return {
    ok: i.length === 0,
    errors: i,
    warnings: n,
    repairHints: D(i)
  };
}
function ne(e) {
  const i = [], n = [];
  if (!e || typeof e != "object")
    return {
      ok: !1,
      errors: [
        t(
          "invalid_recovery_plan",
          "Analytics recovery plan must be a structured object."
        )
      ],
      warnings: n,
      repairHints: [
        {
          code: "invalid_recovery_plan",
          recommendedNextStep: "Return a typed recovery plan with operationIntent and plannedToolCalls."
        }
      ]
    };
  e.version !== 1 && i.push(
    t("invalid_version", "Recovery plan version must be 1.", "version")
  ), e.kind !== "analytics_recovery_plan" && i.push(
    t(
      "invalid_recovery_plan_kind",
      "Recovery plan kind must be analytics_recovery_plan.",
      "kind"
    )
  );
  const o = K(e.operationIntent);
  return i.push(...o.errors), n.push(...o.warnings), Array.isArray(e.plannedToolCalls) || i.push(
    t(
      "invalid_planned_tool_calls",
      "Recovery plan plannedToolCalls must be an array.",
      "plannedToolCalls"
    )
  ), {
    ok: i.length === 0,
    errors: i,
    warnings: n,
    repairHints: D(i)
  };
}
function D(e) {
  return e.map((i) => ({
    code: i.code,
    recommendedNextStep: i.code === "missing_obligations" ? "Normalize the user request into one or more typed analytics obligations before recovery planning." : "Correct the operation/recovery contract shape before planning execution."
  }));
}
function W(e) {
  return e.map((i) => {
    switch (i.code) {
      case "missing_source":
        return {
          code: i.code,
          fieldRole: "source",
          recommendedNextStep: "Provide a semantic, physical, or SQL source before executing analytics."
        };
      case "missing_metric":
        return {
          code: i.code,
          fieldRole: "metric",
          recommendedNextStep: "Choose one exact metric from the grounded schema candidates."
        };
      case "missing_record_fields":
        return {
          code: i.code,
          fieldRole: "dimension",
          recommendedNextStep: "Provide at least one field for the records intent."
        };
      case "missing_input_options_field":
        return {
          code: i.code,
          fieldRole: "input",
          recommendedNextStep: "Provide the exact field whose option values should be listed."
        };
      case "missing_sql":
        return {
          code: i.code,
          fieldRole: "sql",
          recommendedNextStep: "Provide bounded read-only SQL with an explicit outer LIMIT."
        };
      case "conflicting_sql":
        return {
          code: i.code,
          fieldRole: "sql",
          recommendedNextStep: "Use one canonical SQL text location for the intent; prefer top-level sql."
        };
      case "missing_semantic_domain":
      case "missing_dataset_name":
      case "missing_connection_id":
      case "missing_table_name":
        return {
          code: i.code,
          fieldRole: "source",
          recommendedNextStep: "Use grounded catalog metadata to fill the missing source reference."
        };
      default:
        return;
    }
  }).filter((i) => !!i);
}
function ee(e, i, n, o) {
  if (!e || typeof e != "object") {
    n.push(t("invalid_view", "Dashboard view must be an object.", i));
    return;
  }
  if (d(e.title) || n.push(t("missing_view_title", "Dashboard view needs a title.", i)), !e.presentation || typeof e.presentation != "object") {
    n.push(
      t(
        "missing_presentation",
        "Dashboard view needs a presentation.",
        `${i}.presentation`
      )
    );
    return;
  }
  if (e.presentation.kind === "text") {
    d(e.text) || o.push(
      t(
        "missing_text_content",
        "Text views should include text content.",
        `${i}.text`
      )
    );
    return;
  }
  if (!e.analytics) {
    n.push(
      t(
        "missing_view_analytics",
        "Non-text dashboard views need analytics intent.",
        `${i}.analytics`
      )
    );
    return;
  }
  const r = T(e.analytics);
  for (const a of r.errors)
    n.push(
      t(a.code, a.message, `${i}.analytics${a.path ? `.${a.path}` : ""}`)
    );
  for (const a of r.warnings)
    o.push(
      t(
        a.code,
        a.message,
        `${i}.analytics${a.path ? `.${a.path}` : ""}`
      )
    );
}
function se(e) {
  const i = [], n = [];
  if (!e || typeof e != "object")
    return {
      ok: !1,
      errors: [
        t(
          "invalid_dashboard_intent",
          "Dashboard intent must be a structured object."
        )
      ],
      warnings: n
    };
  e.version !== 1 && i.push(t("invalid_version", "Dashboard intent version must be 1.")), e.kind !== "dashboard" && i.push(t("invalid_kind", "Experience intent kind must be dashboard.")), d(e.title) || i.push(t("missing_title", "Dashboard intent needs a title.", "title"));
  const o = Array.isArray(e.inputs) ? e.inputs : [], r = k(
    o.filter(c).map((u) => u.id).filter((u) => typeof u == "string")
  );
  r && i.push(
    t(
      "duplicate_input_id",
      `Duplicate input id: ${r}.`,
      "inputs"
    )
  );
  for (const [u, l] of o.entries()) {
    const f = `inputs.${u}`;
    if (!c(l)) {
      i.push(t("invalid_input", "Dashboard input must be an object.", f));
      continue;
    }
    d(l.id) || i.push(t("missing_input_id", "Input needs an id.", `${f}.id`)), d(l.label) || i.push(
      t("missing_input_label", "Input needs a label.", `${f}.label`)
    ), !l.field || !d(l.field.name) ? i.push(
      t("missing_input_field", "Input needs a field.", `${f}.field`)
    ) : p(l.field, `${f}.field`, i);
  }
  const a = Array.isArray(e.sections) ? e.sections : [];
  a.length === 0 && i.push(
    t(
      "missing_sections",
      "Dashboard intent needs at least one section.",
      "sections"
    )
  );
  const s = a.flatMap(
    (u) => u && typeof u == "object" && Array.isArray(u.views) ? u.views.filter(c).map((l) => l.id).filter((l) => !!l) : []
  ), m = k(s);
  m && i.push(
    t(
      "duplicate_view_id",
      `Duplicate dashboard view id: ${m}.`,
      "sections"
    )
  );
  for (const [u, l] of a.entries()) {
    const f = `sections.${u}`;
    if (!l || typeof l != "object") {
      i.push(
        t("invalid_section", "Dashboard section must be an object.", f)
      );
      continue;
    }
    if (d(l.title) || i.push(
      t("missing_section_title", "Dashboard section needs a title.", f)
    ), !Array.isArray(l.views) || l.views.length === 0) {
      i.push(
        t(
          "missing_section_views",
          "Dashboard section needs at least one view.",
          `${f}.views`
        )
      );
      continue;
    }
    for (const [g, h] of l.views.entries())
      ee(
        h,
        `${f}.views.${g}`,
        i,
        n
      );
  }
  return { ok: i.length === 0, errors: i, warnings: n };
}
export {
  j as a,
  K as b,
  ne as c,
  se as d,
  te as n,
  $ as p,
  T as v
};
