var W = Object.defineProperty;
var Y = (r, t, e) => t in r ? W(r, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : r[t] = e;
var F = (r, t, e) => Y(r, typeof t != "symbol" ? t + "" : t, e);
const w = "BRIEFING", Z = ["markdown", "html"], V = ["markdown"], ze = 20, l = {
  name: 500,
  description: 2e3,
  instruction: 5e4,
  customMessage: 5e4,
  contentTitle: 500,
  contentSummary: 2e4,
  contentText: 5e3,
  contentSql: 5e4,
  contentItem: 2e3,
  contentCell: 500,
  attachmentTitle: 500,
  presentationAudience: 1e3,
  presentationTone: 500,
  presentationFormat: 2e3,
  id: 500
}, g = {
  maxBlocks: 50,
  maxTableRows: 100,
  maxTableColumns: 20,
  maxTableCellCharacters: l.contentCell,
  maxParagraphCharacters: l.contentText,
  maxFindingCharacters: l.contentText,
  maxListItems: 100,
  // A kpi_grid is rendered as a 2-up tile arrangement in emails. Cap at 8 so
  // the visual stays at most 4 rows (4 columns wide on desktop would force
  // tiny tiles in narrow client panels; 2-up was the chosen layout).
  maxKpiTiles: 8
}, M = new Set(Z), Q = /* @__PURE__ */ new Set([
  "EMAIL",
  "WEBHOOK",
  "SLACK",
  "IN_APP",
  "TELEGRAM"
]), L = {
  maxToolCalls: { min: 1, max: 100 },
  maxRows: { min: 1, max: 1e5 },
  timeoutMs: { min: 1e3, max: 30 * 6e4 }
}, ee = /* @__PURE__ */ new Set([
  "letter",
  "legal",
  "a4",
  "a3",
  "tabloid"
]), te = /* @__PURE__ */ new Set([
  "portrait",
  "landscape"
]), ne = /* @__PURE__ */ new Set([",", ";", "	"]), re = /* @__PURE__ */ new Set([
  "current",
  "all"
]), oe = /* @__PURE__ */ new Set([
  "concise",
  "balanced",
  "detailed"
]), ie = /* @__PURE__ */ new Set([
  "info",
  "positive",
  "warning",
  "critical"
]), ae = /* @__PURE__ */ new Set([
  "text",
  "number",
  "currency",
  "percent",
  "date",
  "boolean"
]), ce = /* @__PURE__ */ new Set([
  "left",
  "center",
  "right"
]), de = [
  "pageSize",
  "orientation",
  "includeFilters",
  "includeTimestamp",
  "delimiter",
  "includeHeaders",
  "useFormattedValues",
  "sheetSelection",
  "currentSheetId",
  "currentSheetName",
  "filterValues",
  "inlineFilterValues",
  "sheetFilterValues",
  "filterLine",
  "controlValues",
  "controlDefinitions",
  "cardControlDefinitions",
  "controlBindings",
  "tablePreferences",
  "dashboardInputExecutionSnapshot",
  "selectedSheetId",
  "cardType",
  "theme"
];
class S extends Error {
  constructor(e) {
    super(e.map((n) => `${n.field}: ${n.message}`).join("; "));
    F(this, "issues");
    this.name = "BriefingContractError", this.issues = e;
  }
}
function P(r) {
  return typeof r == "object" && r !== null && !Array.isArray(r);
}
function c(r, t, e) {
  r.push({ field: t, message: e });
}
function h(r, t, e) {
  return P(r) ? r : (c(e, t, "Expected an object."), {});
}
function m(r, t, e, n) {
  const o = new Set(e);
  Object.keys(r).forEach((i) => {
    o.has(i) || c(
      n,
      t ? `${t}.${i}` : i,
      `Unexpected field "${i}".`
    );
  });
}
function u(r, t, e, n) {
  if (typeof r != "string")
    return c(n, t, "Expected a string."), "";
  const o = r.trim();
  return o ? (o.length > e && c(n, t, `Must be ${e} characters or fewer.`), o) : (c(n, t, "Required value cannot be empty."), "");
}
function y(r, t, e, n) {
  if (r == null)
    return;
  if (typeof r != "string") {
    c(n, t, "Expected a string.");
    return;
  }
  const o = r.trim();
  if (o)
    return o.length > e && c(n, t, `Must be ${e} characters or fewer.`), o;
}
function le(r, t, e) {
  if (r != null) {
    if (!Number.isInteger(r) || Number(r) < 0) {
      c(e, t, "Expected a non-negative integer.");
      return;
    }
    return Number(r);
  }
}
function $(r, t, e, n = {}) {
  if (r == null)
    return n.required ? (c(e, t, "Expected at least one evidence id."), []) : void 0;
  if (!Array.isArray(r))
    return c(e, t, "Expected an array."), n.required ? [] : void 0;
  const o = [];
  return r.forEach((i, a) => {
    const d = u(
      i,
      `${t}.${a}`,
      l.id,
      e
    );
    d && !o.includes(d) && o.push(d);
  }), n.required && o.length === 0 && c(e, t, "Expected at least one evidence id."), o.length > 0 || n.required ? o : void 0;
}
function C(r, t, e, n) {
  return r == null ? t : typeof r != "boolean" ? (c(n, e, "Expected a boolean."), t) : r;
}
function fe(r, t) {
  return r == null ? "ACTIVE" : r === "ACTIVE" || r === "PAUSED" || r === "DISABLED" ? r : (c(t, "status", "Expected ACTIVE, PAUSED, or DISABLED."), "ACTIVE");
}
function se(r) {
  return r.includes("@") && r.includes(".") && !r.includes(" ");
}
function q(r, t) {
  if (r == null)
    return { type: "project" };
  const e = h(r, "source", t), n = e.type;
  return n === "project" ? (m(e, "source", ["type"], t), { type: "project" }) : n === "dashboard" ? (m(e, "source", ["type", "dashboardId"], t), {
    type: "dashboard",
    dashboardId: u(
      e.dashboardId,
      "source.dashboardId",
      l.id,
      t
    )
  }) : (c(t, "source.type", "Expected project or dashboard."), { type: "project" });
}
function j(r, t) {
  const e = h(r, "body", t), n = e.type;
  return n === "generated_analysis" ? (m(e, "body", ["type", "instruction"], t), {
    type: "generated_analysis",
    instruction: u(
      e.instruction,
      "body.instruction",
      l.instruction,
      t
    )
  }) : n === "custom_message" ? (m(e, "body", ["type", "message"], t), {
    type: "custom_message",
    message: u(
      e.message,
      "body.message",
      l.customMessage,
      t
    )
  }) : n === "none" ? (m(e, "body", ["type"], t), { type: "none" }) : (c(
    t,
    "body.type",
    "Expected generated_analysis, custom_message, or none."
  ), { type: "none" });
}
function me(r, t) {
  if (r == null)
    return [...V];
  if (!Array.isArray(r))
    return c(t, "presentation.artifactFormats", "Expected an array."), [...V];
  if (r.length === 0)
    return c(
      t,
      "presentation.artifactFormats",
      "Choose at least one artifact format."
    ), [];
  const e = [];
  return r.forEach((n, o) => {
    if (typeof n != "string" || !M.has(n)) {
      c(
        t,
        `presentation.artifactFormats.${o}`,
        "Expected markdown or html."
      );
      return;
    }
    e.includes(n) || e.push(n);
  }), e;
}
function K(r, t) {
  if (r == null)
    return {
      artifactFormats: [...V],
      includeEvidence: !0,
      includeSql: !0
    };
  const e = h(r, "presentation", t);
  m(
    e,
    "presentation",
    ["artifactFormats", "includeEvidence", "includeSql", "audience", "tone", "format"],
    t
  );
  const n = {
    artifactFormats: me(e.artifactFormats, t),
    includeEvidence: C(
      e.includeEvidence,
      !0,
      "presentation.includeEvidence",
      t
    ),
    includeSql: C(
      e.includeSql,
      !0,
      "presentation.includeSql",
      t
    )
  }, o = y(
    e.audience,
    "presentation.audience",
    l.presentationAudience,
    t
  ), i = y(
    e.tone,
    "presentation.tone",
    l.presentationTone,
    t
  ), a = y(
    e.format,
    "presentation.format",
    l.presentationFormat,
    t
  );
  return o && (n.audience = o), i && (n.tone = i), a && (n.format = a), n;
}
function pe(r, t, e) {
  return Array.isArray(r) ? (r.length === 0 && c(e, t, "Expected at least one item."), r.length > g.maxListItems && c(
    e,
    t,
    `Must have ${g.maxListItems} items or fewer.`
  ), r.slice(0, g.maxListItems).map(
    (n, o) => u(
      n,
      `${t}.${o}`,
      l.contentItem,
      e
    )
  ).filter(Boolean)) : (c(e, t, "Expected an array."), []);
}
function ue(r, t, e) {
  return r === null || typeof r == "string" || typeof r == "number" || typeof r == "boolean" ? typeof r == "string" && r.length > l.contentCell ? (c(
    e,
    t,
    `Must be ${l.contentCell} characters or fewer.`
  ), r.slice(0, l.contentCell)) : typeof r == "number" && !Number.isFinite(r) ? (c(e, t, "Expected a finite number."), null) : r : (c(e, t, "Expected a string, number, boolean, or null."), null);
}
function ye(r, t, e) {
  if (!Array.isArray(r))
    return c(e, t, "Expected an array."), [];
  r.length === 0 && c(e, t, "Expected at least one column."), r.length > g.maxTableColumns && c(
    e,
    t,
    `Must have ${g.maxTableColumns} columns or fewer.`
  );
  const n = /* @__PURE__ */ new Set();
  return r.slice(0, g.maxTableColumns).map((o, i) => {
    const a = `${t}.${i}`, d = h(o, a, e);
    m(d, a, ["key", "label", "kind", "align"], e);
    const s = u(
      d.key,
      `${a}.key`,
      l.id,
      e
    );
    s && (n.has(s) && c(e, `${a}.key`, "Column key must be unique."), n.add(s));
    const f = u(
      d.label,
      `${a}.label`,
      l.contentTitle,
      e
    ), p = E(
      d.kind,
      `${a}.kind`,
      ae,
      "text, number, currency, percent, date, or boolean",
      e
    ), I = E(
      d.align,
      `${a}.align`,
      ce,
      "left, center, or right",
      e
    );
    return {
      key: s,
      label: f,
      ...p ? { kind: p } : {},
      ...I ? { align: I } : {}
    };
  });
}
function ve(r, t, e, n) {
  if (!Array.isArray(r))
    return c(n, t, "Expected an array."), [];
  r.length > g.maxTableRows && c(
    n,
    t,
    `Must have ${g.maxTableRows} rows or fewer.`
  );
  const o = new Set(e.map((i) => i.key).filter(Boolean));
  return r.slice(0, g.maxTableRows).map((i, a) => {
    const d = `${t}.${a}`, s = h(i, d, n);
    return Object.keys(s).forEach((f) => {
      o.has(f) || c(n, `${d}.${f}`, `Unexpected table cell "${f}".`);
    }), Object.fromEntries(
      e.map((f) => [
        f.key,
        ue(s[f.key] ?? null, `${d}.${f.key}`, n)
      ])
    );
  });
}
function he(r, t, e) {
  const n = `content.blocks.${t}`, o = h(r, n, e), i = o.type;
  if (i === "heading") {
    m(o, n, ["type", "text", "level", "evidenceIds"], e);
    const a = o.level === void 0 || o.level === null ? void 0 : o.level === 2 || o.level === 3 ? o.level : void 0;
    o.level !== void 0 && o.level !== null && a === void 0 && c(e, `${n}.level`, "Expected 2 or 3.");
    const d = $(o.evidenceIds, `${n}.evidenceIds`, e);
    return {
      type: "heading",
      text: u(
        o.text,
        `${n}.text`,
        l.contentTitle,
        e
      ),
      ...a ? { level: a } : {},
      ...d ? { evidenceIds: d } : {}
    };
  }
  if (i === "paragraph") {
    m(o, n, ["type", "text", "evidenceIds"], e);
    const a = $(o.evidenceIds, `${n}.evidenceIds`, e);
    return {
      type: "paragraph",
      text: u(
        o.text,
        `${n}.text`,
        l.contentText,
        e
      ),
      ...a ? { evidenceIds: a } : {}
    };
  }
  if (i === "finding") {
    m(o, n, ["type", "title", "text", "severity", "evidenceIds"], e);
    const a = y(
      o.title,
      `${n}.title`,
      l.contentTitle,
      e
    ), d = E(
      o.severity,
      `${n}.severity`,
      ie,
      "info, positive, warning, or critical",
      e
    );
    return {
      type: "finding",
      ...a ? { title: a } : {},
      text: u(
        o.text,
        `${n}.text`,
        l.contentText,
        e
      ),
      ...d ? { severity: d } : {},
      evidenceIds: $(o.evidenceIds, `${n}.evidenceIds`, e, {
        required: !0
      }) ?? []
    };
  }
  if (i === "bullets" || i === "actions" || i === "limitations") {
    m(o, n, ["type", "title", "items", "evidenceIds"], e);
    const a = y(
      o.title,
      `${n}.title`,
      l.contentTitle,
      e
    ), d = $(o.evidenceIds, `${n}.evidenceIds`, e);
    return {
      type: i,
      ...a ? { title: a } : {},
      items: pe(o.items, `${n}.items`, e),
      ...d ? { evidenceIds: d } : {}
    };
  }
  if (i === "metric") {
    m(
      o,
      n,
      ["type", "label", "value", "previousValue", "delta", "unit", "evidenceIds"],
      e
    );
    const a = typeof o.value == "string" || typeof o.value == "number" ? o.value : "";
    a === "" && c(e, `${n}.value`, "Expected a string or number.");
    const d = o.previousValue === void 0 || o.previousValue === null || typeof o.previousValue == "string" || typeof o.previousValue == "number" ? o.previousValue : void 0;
    o.previousValue !== void 0 && o.previousValue !== null && d === void 0 && c(e, `${n}.previousValue`, "Expected a string, number, or null.");
    const s = o.delta === void 0 || o.delta === null || typeof o.delta == "string" || typeof o.delta == "number" ? o.delta : void 0;
    o.delta !== void 0 && o.delta !== null && s === void 0 && c(e, `${n}.delta`, "Expected a string, number, or null.");
    const f = y(
      o.unit,
      `${n}.unit`,
      l.contentTitle,
      e
    ), p = $(o.evidenceIds, `${n}.evidenceIds`, e);
    return {
      type: "metric",
      label: u(
        o.label,
        `${n}.label`,
        l.contentTitle,
        e
      ),
      value: a,
      ...d !== void 0 ? { previousValue: d } : {},
      ...s !== void 0 ? { delta: s } : {},
      ...f ? { unit: f } : {},
      ...p ? { evidenceIds: p } : {}
    };
  }
  if (i === "kpi_grid") {
    m(o, n, ["type", "title", "tiles", "evidenceIds"], e);
    const a = y(
      o.title,
      `${n}.title`,
      l.contentTitle,
      e
    ), d = Array.isArray(o.tiles) ? o.tiles : [];
    Array.isArray(o.tiles) || c(e, `${n}.tiles`, "Expected an array."), d.length === 0 && c(e, `${n}.tiles`, "Expected at least one tile."), d.length > g.maxKpiTiles && c(
      e,
      `${n}.tiles`,
      `Must have ${g.maxKpiTiles} tiles or fewer.`
    );
    const s = d.slice(0, g.maxKpiTiles).map((p, I) => {
      const b = `${n}.tiles.${I}`, v = h(p, b, e);
      m(
        v,
        b,
        ["label", "value", "previousValue", "delta", "unit", "evidenceIds"],
        e
      );
      const D = typeof v.value == "string" || typeof v.value == "number" ? v.value : "";
      D === "" && c(e, `${b}.value`, "Expected a string or number.");
      const k = v.previousValue === void 0 || v.previousValue === null || typeof v.previousValue == "string" || typeof v.previousValue == "number" ? v.previousValue : void 0;
      v.previousValue !== void 0 && v.previousValue !== null && k === void 0 && c(
        e,
        `${b}.previousValue`,
        "Expected a string, number, or null."
      );
      const _ = v.delta === void 0 || v.delta === null || typeof v.delta == "string" || typeof v.delta == "number" ? v.delta : void 0;
      v.delta !== void 0 && v.delta !== null && _ === void 0 && c(e, `${b}.delta`, "Expected a string, number, or null.");
      const O = y(
        v.unit,
        `${b}.unit`,
        l.contentTitle,
        e
      ), R = $(
        v.evidenceIds,
        `${b}.evidenceIds`,
        e
      );
      return {
        label: u(
          v.label,
          `${b}.label`,
          l.contentTitle,
          e
        ),
        value: D,
        ...k !== void 0 ? { previousValue: k } : {},
        ..._ !== void 0 ? { delta: _ } : {},
        ...O ? { unit: O } : {},
        ...R ? { evidenceIds: R } : {}
      };
    }), f = $(
      o.evidenceIds,
      `${n}.evidenceIds`,
      e
    );
    return {
      type: "kpi_grid",
      ...a ? { title: a } : {},
      tiles: s,
      ...f ? { evidenceIds: f } : {}
    };
  }
  if (i === "progress") {
    m(o, n, ["type", "label", "value", "detail", "evidenceIds"], e);
    const a = typeof o.value == "number" && Number.isFinite(o.value) ? o.value : 0;
    (typeof o.value != "number" || !Number.isFinite(o.value)) && c(e, `${n}.value`, "Expected a finite number.");
    const d = y(
      o.detail,
      `${n}.detail`,
      l.contentText,
      e
    ), s = $(o.evidenceIds, `${n}.evidenceIds`, e);
    return {
      type: "progress",
      label: u(
        o.label,
        `${n}.label`,
        l.contentTitle,
        e
      ),
      value: a,
      ...d ? { detail: d } : {},
      ...s ? { evidenceIds: s } : {}
    };
  }
  if (i === "table") {
    m(
      o,
      n,
      ["type", "id", "title", "columns", "rows", "caption", "totalRows", "evidenceIds"],
      e
    );
    const a = ye(o.columns, `${n}.columns`, e), d = y(
      o.title,
      `${n}.title`,
      l.contentTitle,
      e
    ), s = y(
      o.id,
      `${n}.id`,
      l.id,
      e
    ), f = y(
      o.caption,
      `${n}.caption`,
      l.contentItem,
      e
    ), p = le(
      o.totalRows,
      `${n}.totalRows`,
      e
    ), I = $(o.evidenceIds, `${n}.evidenceIds`, e);
    return {
      type: "table",
      ...s ? { id: s } : {},
      ...d ? { title: d } : {},
      columns: a,
      rows: ve(o.rows, `${n}.rows`, a, e),
      ...f ? { caption: f } : {},
      ...p !== void 0 ? { totalRows: p } : {},
      ...I ? { evidenceIds: I } : {}
    };
  }
  if (i === "evidence_appendix") {
    m(o, n, ["type", "title", "evidenceIds"], e);
    const a = y(
      o.title,
      `${n}.title`,
      l.contentTitle,
      e
    );
    return {
      type: "evidence_appendix",
      ...a ? { title: a } : {},
      evidenceIds: $(o.evidenceIds, `${n}.evidenceIds`, e, {
        required: !0
      }) ?? []
    };
  }
  if (i === "sql") {
    m(o, n, ["type", "title", "sql", "evidenceIds"], e);
    const a = y(
      o.title,
      `${n}.title`,
      l.contentTitle,
      e
    ), d = u(
      o.sql,
      `${n}.sql`,
      l.contentSql,
      e
    ), s = $(o.evidenceIds, `${n}.evidenceIds`, e);
    return {
      type: "sql",
      ...a ? { title: a } : {},
      sql: d,
      ...s ? { evidenceIds: s } : {}
    };
  }
  return c(
    e,
    `${n}.type`,
    "Expected heading, paragraph, finding, bullets, metric, kpi_grid, progress, table, actions, limitations, sql, or evidence_appendix."
  ), {
    type: "paragraph",
    text: ""
  };
}
function ge(r) {
  const t = [], e = h(r, "content", t);
  m(e, "content", ["version", "title", "summary", "blocks"], t), e.version !== 1 && c(t, "content.version", "Expected version 1.");
  const n = y(
    e.title,
    "content.title",
    l.contentTitle,
    t
  ), o = y(
    e.summary,
    "content.summary",
    l.contentSummary,
    t
  );
  Array.isArray(e.blocks) || c(t, "content.blocks", "Expected an array.");
  const i = Array.isArray(e.blocks) ? e.blocks : [];
  i.length === 0 && c(t, "content.blocks", "Expected at least one block."), i.length > g.maxBlocks && c(
    t,
    "content.blocks",
    `Must have ${g.maxBlocks} blocks or fewer.`
  );
  const a = i.slice(0, g.maxBlocks).map((d, s) => he(d, s, t));
  return B(t), {
    version: 1,
    ...n ? { title: n } : {},
    ...o ? { summary: o } : {},
    blocks: a
  };
}
function De(r) {
  try {
    return {
      valid: !0,
      value: ge(r),
      issues: []
    };
  } catch (t) {
    if (t instanceof S)
      return { valid: !1, issues: t.issues };
    throw t;
  }
}
function x(r, t, e, n) {
  return typeof r == "string" && e.includes(r) ? r : (c(n, t, `Expected ${e.join(" or ")}.`), e[0]);
}
function E(r, t, e, n, o) {
  if (r != null) {
    if (typeof r == "string" && e.has(r))
      return r;
    c(o, t, `Expected ${n}.`);
  }
}
function Ie(r, t, e) {
  if (r != null) {
    if (!Array.isArray(r)) {
      c(e, t, "Expected an array.");
      return;
    }
    return r;
  }
}
function be(r, t, e) {
  if (r != null) {
    if (!P(r)) {
      c(e, t, "Expected an object.");
      return;
    }
    return r;
  }
}
function $e(r, t, e) {
  if (r == null)
    return;
  const n = h(r, t, e);
  m(n, t, de, e);
  const o = {}, i = E(
    n.pageSize,
    `${t}.pageSize`,
    ee,
    "letter, legal, a4, a3, or tabloid",
    e
  ), a = E(
    n.orientation,
    `${t}.orientation`,
    te,
    "portrait or landscape",
    e
  ), d = E(
    n.delimiter,
    `${t}.delimiter`,
    ne,
    "comma, semicolon, or tab",
    e
  ), s = E(
    n.sheetSelection,
    `${t}.sheetSelection`,
    re,
    "current or all",
    e
  );
  i && (o.pageSize = i), a && (o.orientation = a), d && (o.delimiter = d), s && (o.sheetSelection = s);
  for (const f of [
    "includeFilters",
    "includeTimestamp",
    "includeHeaders",
    "useFormattedValues"
  ])
    if (!(n[f] === void 0 || n[f] === null)) {
      if (typeof n[f] != "boolean") {
        c(e, `${t}.${f}`, "Expected a boolean.");
        continue;
      }
      o[f] = n[f];
    }
  for (const f of [
    "currentSheetId",
    "currentSheetName",
    "filterLine",
    "selectedSheetId",
    "cardType",
    "theme"
  ]) {
    const p = y(
      n[f],
      `${t}.${f}`,
      l.id,
      e
    );
    p && (o[f] = p);
  }
  for (const f of [
    "filterValues",
    "inlineFilterValues",
    "sheetFilterValues",
    "controlDefinitions",
    "cardControlDefinitions",
    "controlBindings"
  ]) {
    const p = Ie(n[f], `${t}.${f}`, e);
    p && (o[f] = p);
  }
  for (const f of [
    "controlValues",
    "tablePreferences",
    "dashboardInputExecutionSnapshot"
  ]) {
    const p = be(n[f], `${t}.${f}`, e);
    p && (o[f] = p);
  }
  return Object.keys(o).length > 0 ? o : void 0;
}
function T(r, t, e, n) {
  const o = y(
    t,
    e,
    l.attachmentTitle,
    n
  );
  return o && (r.title = o), r;
}
function A(r, t, e, n) {
  const o = $e(t, e, n);
  return o && (r.settings = o), r;
}
function Ee(r, t, e) {
  const n = `attachments.${t}`, o = h(r, n, e), i = o.type;
  if (i === "dashboard") {
    m(
      o,
      n,
      ["type", "dashboardId", "format", "title", "settings"],
      e
    );
    const a = T(
      {
        type: "dashboard",
        dashboardId: u(
          o.dashboardId,
          `${n}.dashboardId`,
          l.id,
          e
        ),
        format: x(o.format, `${n}.format`, ["pdf"], e)
      },
      o.title,
      `${n}.title`,
      e
    );
    return A(a, o.settings, `${n}.settings`, e);
  }
  if (i === "dashboard_sheet" || i === "document_sheet") {
    m(
      o,
      n,
      ["type", "dashboardId", "sheetId", "format", "title", "settings"],
      e
    );
    const a = T(
      {
        type: i,
        dashboardId: u(
          o.dashboardId,
          `${n}.dashboardId`,
          l.id,
          e
        ),
        sheetId: u(
          o.sheetId,
          `${n}.sheetId`,
          l.id,
          e
        ),
        format: x(o.format, `${n}.format`, ["pdf"], e)
      },
      o.title,
      `${n}.title`,
      e
    );
    return A(a, o.settings, `${n}.settings`, e);
  }
  if (i === "visual") {
    m(
      o,
      n,
      ["type", "dashboardId", "visualId", "format", "title", "settings"],
      e
    );
    const a = T(
      {
        type: "visual",
        dashboardId: u(
          o.dashboardId,
          `${n}.dashboardId`,
          l.id,
          e
        ),
        visualId: u(
          o.visualId,
          `${n}.visualId`,
          l.id,
          e
        ),
        format: x(
          o.format,
          `${n}.format`,
          ["pdf", "png"],
          e
        )
      },
      o.title,
      `${n}.title`,
      e
    );
    return A(a, o.settings, `${n}.settings`, e);
  }
  if (i === "card") {
    m(
      o,
      n,
      ["type", "dashboardId", "cardId", "format", "title", "settings"],
      e
    );
    const a = T(
      {
        type: "card",
        dashboardId: u(
          o.dashboardId,
          `${n}.dashboardId`,
          l.id,
          e
        ),
        cardId: u(
          o.cardId,
          `${n}.cardId`,
          l.id,
          e
        ),
        format: x(
          o.format,
          `${n}.format`,
          ["csv", "pdf", "png"],
          e
        )
      },
      o.title,
      `${n}.title`,
      e
    );
    return A(a, o.settings, `${n}.settings`, e);
  }
  return c(
    e,
    `${n}.type`,
    "Expected dashboard, dashboard_sheet, document_sheet, visual, or card."
  ), { type: "dashboard", dashboardId: "", format: "pdf" };
}
function Ce(r) {
  switch (r.type) {
    case "dashboard":
      return `${r.type}:${r.dashboardId}:${r.format}`;
    case "dashboard_sheet":
    case "document_sheet":
      return `${r.type}:${r.dashboardId}:${r.sheetId}:${r.format}`;
    case "visual":
      return `${r.type}:${r.dashboardId}:${r.visualId}:${r.format}`;
    case "card":
      return `${r.type}:${r.dashboardId}:${r.cardId}:${r.format}`;
  }
}
function G(r, t) {
  if (r == null)
    return [];
  if (!Array.isArray(r))
    return c(t, "attachments", "Expected an array."), [];
  r.length > 20 && c(
    t,
    "attachments",
    "Choose 20 attachments or fewer."
  );
  const e = r.slice(0, 20).map((o, i) => Ee(o, i, t)), n = /* @__PURE__ */ new Set();
  return e.forEach((o, i) => {
    const a = Ce(o);
    n.has(a) && c(t, `attachments.${i}`, "Duplicate attachment."), n.add(a);
  }), e;
}
function U(r, t) {
  if (r == null)
    return;
  const e = h(r, "limits", t);
  m(e, "limits", Object.keys(L), t);
  const n = {};
  return Object.keys(L).forEach((o) => {
    const i = e[o];
    if (i == null)
      return;
    const a = L[o];
    if (typeof i != "number" || !Number.isInteger(i) || i < a.min || i > a.max) {
      c(
        t,
        `limits.${o}`,
        `Expected an integer between ${a.min} and ${a.max}.`
      );
      return;
    }
    n[o] = i;
  }), Object.keys(n).length > 0 ? n : void 0;
}
function B(r) {
  if (r.length > 0)
    throw new S(r);
}
function H(r, t, e) {
  r.type === "none" && t.length === 0 && c(
    e,
    "body",
    "Add briefing instructions, a custom message, or at least one attachment."
  );
}
function X(r) {
  const t = [], e = h(r, "jobConfig", t);
  m(
    e,
    "jobConfig",
    ["kind", "source", "body", "attachments", "presentation", "limits"],
    t
  ), e.kind !== w && c(t, "kind", "Expected BRIEFING.");
  const n = q(e.source, t), o = j(e.body, t), i = G(e.attachments, t), a = K(e.presentation, t), d = U(e.limits, t);
  return H(o, i, t), B(t), {
    kind: w,
    source: n,
    body: o,
    attachments: i,
    presentation: a,
    ...d ? { limits: d } : {}
  };
}
function Oe(r) {
  return X({
    ...r,
    kind: w
  });
}
function N(r, t, e, n = "deliveryConfig.recipients") {
  const o = `${n}.${t}`, i = h(r, o, e), a = i.type;
  if (a === "email") {
    m(i, o, ["type", "address", "name"], e);
    const d = u(
      i.address,
      `${o}.address`,
      l.id,
      e
    ), s = y(
      i.name,
      `${o}.name`,
      l.attachmentTitle,
      e
    );
    return d && !se(d) && c(e, `${o}.address`, "Email address is invalid."), {
      type: "email",
      address: d,
      ...s ? { name: s } : {}
    };
  }
  return a === "user" ? (m(i, o, ["type", "userId"], e), {
    type: "user",
    userId: u(
      i.userId,
      `${o}.userId`,
      l.id,
      e
    )
  }) : a === "tenant_user" ? (m(i, o, ["type", "tenantUserId"], e), {
    type: "tenant_user",
    tenantUserId: u(
      i.tenantUserId,
      `${o}.tenantUserId`,
      l.id,
      e
    )
  }) : a === "group" ? (m(i, o, ["type", "groupId"], e), {
    type: "group",
    groupId: u(
      i.groupId,
      `${o}.groupId`,
      l.id,
      e
    )
  }) : (c(
    e,
    `${o}.type`,
    "Expected email, user, tenant_user, or group."
  ), { type: "email", address: "" });
}
function xe(r, t) {
  return r == null ? [] : Array.isArray(r) ? r.map(
    (e, n) => N(e, n, t)
  ) : (c(t, "deliveryConfig.recipients", "Expected an array."), []);
}
function Te(r, t) {
  if (!Array.isArray(r))
    return c(t, "deliveryConfig.channels", "Expected an array."), [];
  const e = [];
  return r.forEach((n, o) => {
    if (!J(n)) {
      c(
        t,
        `deliveryConfig.channels.${o}`,
        "Expected EMAIL, WEBHOOK, SLACK, IN_APP, or TELEGRAM."
      );
      return;
    }
    e.includes(n) || e.push(n);
  }), e;
}
function Ae(r, t, e) {
  if (r == null)
    return;
  const n = h(r, t, e), o = {};
  return Object.entries(n).forEach(([i, a]) => {
    if (typeof a != "string") {
      c(e, `${t}.${i}`, "Expected a string.");
      return;
    }
    o[i] = a.trim();
  }), Object.keys(o).length > 0 ? o : void 0;
}
function Se(r, t, e) {
  if (r == null)
    return;
  const n = h(r, t, e);
  m(
    n,
    t,
    ["url", "method", "headers", "secretEncrypted"],
    e
  );
  const o = n.method === void 0 || n.method === null ? void 0 : x(n.method, `${t}.method`, ["POST", "PUT"], e), i = Ae(n.headers, `${t}.headers`, e), a = y(
    n.secretEncrypted,
    `${t}.secretEncrypted`,
    2e3,
    e
  );
  return {
    url: u(n.url, `${t}.url`, 2e3, e),
    ...o ? { method: o } : {},
    ...i ? { headers: i } : {},
    ...a ? { secretEncrypted: a } : {}
  };
}
function ke(r, t, e) {
  const n = h(r, t, e), o = n.type;
  if (o === "channel") {
    m(
      n,
      t,
      ["type", "channelId", "channelName", "isPrivate"],
      e
    );
    const i = y(
      n.channelName,
      `${t}.channelName`,
      l.attachmentTitle,
      e
    ), a = n.isPrivate === void 0 || n.isPrivate === null ? void 0 : C(
      n.isPrivate,
      !1,
      `${t}.isPrivate`,
      e
    );
    return {
      type: "channel",
      channelId: u(
        n.channelId,
        `${t}.channelId`,
        l.id,
        e
      ),
      ...i ? { channelName: i } : {},
      ...a !== void 0 ? { isPrivate: a } : {}
    };
  }
  return o === "dm" ? (m(n, t, ["type", "recipient"], e), c(
    e,
    `${t}.type`,
    "Slack DM delivery is not supported in V1."
  ), {
    type: "dm",
    recipient: N(n.recipient, 0, e, `${t}.recipient`)
  }) : (c(e, `${t}.type`, "Expected channel."), { type: "channel", channelId: "" });
}
function _e(r, t, e) {
  if (r == null)
    return;
  if (!Array.isArray(r)) {
    c(e, t, "Expected an array.");
    return;
  }
  const n = r.map(
    (o, i) => N(o, i, e, t)
  );
  return n.length > 0 ? n : void 0;
}
function Le(r, t, e) {
  if (r == null)
    return;
  const n = h(r, t, e);
  m(
    n,
    t,
    [
      "destination",
      "threadMentions",
      "postThreadDetail",
      "includeDashboardLink",
      "includeBriefingLink"
    ],
    e
  );
  const o = _e(
    n.threadMentions,
    `${t}.threadMentions`,
    e
  ), i = C(
    n.postThreadDetail,
    !0,
    `${t}.postThreadDetail`,
    e
  ), a = C(
    n.includeDashboardLink,
    !0,
    `${t}.includeDashboardLink`,
    e
  ), d = C(
    n.includeBriefingLink,
    !0,
    `${t}.includeBriefingLink`,
    e
  );
  return {
    destination: ke(
      n.destination,
      `${t}.destination`,
      e
    ),
    ...o ? { threadMentions: o } : {},
    ...n.postThreadDetail !== void 0 ? { postThreadDetail: i } : {},
    ...n.includeDashboardLink !== void 0 ? { includeDashboardLink: a } : {},
    ...n.includeBriefingLink !== void 0 ? { includeBriefingLink: d } : {}
  };
}
function we(r, t, e) {
  const n = h(r, t, e);
  m(
    n,
    t,
    [
      "subjectOverride",
      "titleOverride",
      "summaryOverride",
      "actionBlockVisible",
      "formatProfile",
      "slackConfig",
      "webhookConfig"
    ],
    e
  );
  const o = E(
    n.formatProfile,
    `${t}.formatProfile`,
    oe,
    "concise, balanced, or detailed",
    e
  ), i = y(
    n.subjectOverride,
    `${t}.subjectOverride`,
    500,
    e
  ), a = y(
    n.titleOverride,
    `${t}.titleOverride`,
    500,
    e
  ), d = y(
    n.summaryOverride,
    `${t}.summaryOverride`,
    2e3,
    e
  ), s = C(
    n.actionBlockVisible,
    !0,
    `${t}.actionBlockVisible`,
    e
  ), f = Se(
    n.webhookConfig,
    `${t}.webhookConfig`,
    e
  ), p = Le(
    n.slackConfig,
    `${t}.slackConfig`,
    e
  );
  return {
    ...o ? { formatProfile: o } : {},
    ...i ? { subjectOverride: i } : {},
    ...a ? { titleOverride: a } : {},
    ...d ? { summaryOverride: d } : {},
    ...n.actionBlockVisible !== void 0 ? { actionBlockVisible: s } : {},
    ...p ? { slackConfig: p } : {},
    ...f ? { webhookConfig: f } : {}
  };
}
function Ve(r, t) {
  if (r == null)
    return;
  const e = h(r, "deliveryConfig.channelConfigs", t), n = {};
  return Object.entries(e).forEach(([o, i]) => {
    if (!J(o)) {
      c(
        t,
        `deliveryConfig.channelConfigs.${o}`,
        "Expected a supported delivery channel."
      );
      return;
    }
    n[o] = we(
      i,
      `deliveryConfig.channelConfigs.${o}`,
      t
    );
  }), Object.keys(n).length > 0 ? n : void 0;
}
function Be(r, t) {
  var s, f, p;
  if (r == null)
    return null;
  const e = h(r, "deliveryConfig", t);
  m(
    e,
    "deliveryConfig",
    ["recipients", "channels", "channelConfigs"],
    t
  );
  const n = Ve(e.channelConfigs, t), o = xe(e.recipients, t), i = Te(e.channels, t), a = ((f = (s = n == null ? void 0 : n.SLACK) == null ? void 0 : s.slackConfig) == null ? void 0 : f.destination.type) === "channel" && !!n.SLACK.slackConfig.destination.channelId.trim();
  return i.some(
    (I) => I !== "SLACK"
  ) && o.length === 0 && c(
    t,
    "deliveryConfig.recipients",
    "Add at least one recipient or remove delivery config."
  ), i.length === 0 && c(
    t,
    "deliveryConfig.channels",
    "Choose at least one delivery channel or remove delivery config."
  ), i.includes("SLACK") && !a && c(
    t,
    "deliveryConfig.channelConfigs.SLACK.slackConfig.destination.channelId",
    "Choose a Slack channel destination."
  ), !i.includes("SLACK") && ((p = n == null ? void 0 : n.SLACK) != null && p.slackConfig) && c(
    t,
    "deliveryConfig.channelConfigs.SLACK",
    "Slack config requires SLACK in deliveryConfig.channels."
  ), {
    recipients: o,
    channels: i,
    ...n ? { channelConfigs: n } : {}
  };
}
function z(r) {
  const t = [], e = h(r, "draft", t);
  m(
    e,
    "draft",
    [
      "name",
      "description",
      "status",
      "scheduleExpr",
      "timezone",
      "source",
      "body",
      "attachments",
      "presentation",
      "limits",
      "deliveryConfig"
    ],
    t
  );
  const n = u(
    e.name,
    "name",
    l.name,
    t
  ), o = y(
    e.description,
    "description",
    l.description,
    t
  ) ?? null, i = y(e.scheduleExpr, "scheduleExpr", 500, t) ?? null, a = y(e.timezone, "timezone", 200, t) ?? "UTC", d = fe(e.status, t), s = q(e.source, t), f = j(e.body, t), p = G(e.attachments, t), I = K(e.presentation, t), b = U(e.limits, t), v = Be(e.deliveryConfig, t);
  return H(f, p, t), B(t), {
    name: n,
    description: o,
    status: d,
    scheduleExpr: i,
    timezone: a,
    source: s,
    body: f,
    attachments: p,
    presentation: I,
    ...b ? { limits: b } : {},
    deliveryConfig: v
  };
}
function Re(r) {
  return z(r);
}
function Fe(r) {
  return z(r);
}
function Me(r) {
  try {
    return { valid: !0, value: z(r), issues: [] };
  } catch (t) {
    if (t instanceof S)
      return { valid: !1, issues: t.issues };
    throw t;
  }
}
function Pe(r) {
  try {
    return { valid: !0, value: X(r), issues: [] };
  } catch (t) {
    if (t instanceof S)
      return { valid: !1, issues: t.issues };
    throw t;
  }
}
function qe(r) {
  return typeof r == "string" && M.has(r);
}
function J(r) {
  return typeof r == "string" && Q.has(r);
}
export {
  Z as BRIEFING_ARTIFACT_FORMATS,
  g as BRIEFING_CONTENT_LIMITS,
  w as BRIEFING_KIND,
  l as BRIEFING_TEXT_LIMITS,
  S as BriefingContractError,
  V as DEFAULT_BRIEFING_ARTIFACT_FORMATS,
  ze as MAX_BRIEFING_ATTACHMENTS,
  Oe as buildBriefingJobConfig,
  Re as buildCreateBriefingRequest,
  Fe as buildUpdateBriefingRequest,
  qe as isBriefingArtifactFormat,
  J as isBriefingDeliveryChannel,
  ge as normalizeBriefingContentDocument,
  z as normalizeBriefingDraft,
  X as normalizeBriefingJobConfig,
  De as validateBriefingContentDocument,
  Me as validateBriefingDraft,
  Pe as validateBriefingJobConfig
};
