import { f as h, a as V, b as w } from "../chunks/date-formatter-CzcPZx39.js";
import { d as G, i as O, c as W, h as I, e as J, g as K, j as Q, k as U, p as X, r as Y } from "../chunks/date-formatter-CzcPZx39.js";
function z(r, e, n, s) {
  const u = r instanceof Date ? r.toISOString() : String(r), l = (s == null ? void 0 : s.browserTimezone) || n, t = (s == null ? void 0 : s.isSqlCard) ?? !1;
  if (e != null && e.useRelativeTime)
    return V(
      u,
      (e == null ? void 0 : e.sourceTimezone) || "auto"
    );
  const o = e != null && e.useCustomFormat && (e != null && e.customFormat) ? e.customFormat : (e == null ? void 0 : e.format) || "MM/dd/yyyy";
  return w(
    u,
    o,
    e != null && e.timezone && (e == null ? void 0 : e.timezone) !== "auto" ? e.timezone : t ? l : n,
    (e == null ? void 0 : e.sourceTimezone) || "auto"
  );
}
function L(r, e, n) {
  if (r == null)
    return "";
  if (!n.useFormattedValues || !e)
    return String(r);
  switch (e.type) {
    case "number":
      return typeof r == "number" ? h(
        r,
        e.numberFormat,
        n.locale
      ) : String(r);
    case "date":
      return z(
        r,
        e.dateFormat,
        n.timezone,
        {
          browserTimezone: n.browserTimezone,
          isSqlCard: n.isSqlCard
        }
      );
    case "progress":
      return typeof r == "number" ? `${r}%` : String(r);
    case "badge":
    case "link":
    case "text":
    case "none":
    default:
      return String(r);
  }
}
function R(r, e) {
  return e.visibleColumns.map((n) => {
    const s = r[n], u = e.columnSettings[n];
    return L(s, u, {
      useFormattedValues: e.useFormattedValues,
      timezone: e.timezone,
      locale: e.locale
    });
  });
}
function p(r, e = ",") {
  if (r == null)
    return "";
  const n = String(r);
  return n.includes(e) || n.includes('"') || n.includes(`
`) || n.includes("\r") ? `"${n.replace(/"/g, '""')}"` : n;
}
function f(r, e = ",") {
  return r.map((n) => p(n, e)).join(e);
}
function E(r, e = {}) {
  const { delimiter: n = ",", lineEnding: s = `
` } = e;
  return r.map((u) => f(u, n)).join(s);
}
function N(r, e = {}) {
  if (r.length === 0)
    return "";
  const {
    delimiter: n = ",",
    lineEnding: s = `
`,
    includeHeaders: u = !0,
    columns: l
  } = e, t = l || Object.keys(r[0]), o = [];
  u && o.push(t);
  for (const i of r) {
    const a = t.map((m) => {
      const c = i[m];
      return c == null ? "" : String(c);
    });
    o.push(a);
  }
  return E(o, { delimiter: n, lineEnding: s });
}
function H(r, e) {
  const n = [];
  e.includeHeaders && n.push(f(e.visibleColumns, e.delimiter));
  for (const s of r) {
    const u = R(s, e);
    n.push(
      u.map((l) => p(l, e.delimiter)).join(e.delimiter)
    );
  }
  return n;
}
function P(r, e, n = `
`) {
  return H(r, e).join(n);
}
function j(r, e, n) {
  return r == null ? "" : n.useFormattedValues ? typeof r == "number" && (e == null ? void 0 : e.type) === "number" ? h(
    r,
    e.numberFormat,
    n.locale
  ) : typeof r == "number" ? r.toLocaleString(n.locale) : String(r) : String(r);
}
function A(r, e, n) {
  var u, l;
  const s = [];
  if (r.isSubtotal && !r.isGrandTotal) {
    const t = e.groupByColumns.findIndex(
      (o) => o.alias === r.subtotalLevel
    );
    e.groupByColumns.forEach((o, i) => {
      var a, m, c, b;
      if (i === t) {
        const d = (m = (a = e.rowAggregates) == null ? void 0 : a.find(
          (T) => T.groupLevel === r.subtotalLevel
        )) == null ? void 0 : m.label, y = (b = (c = r.subtotalContext) == null ? void 0 : c.groupByValues) == null ? void 0 : b[o.alias];
        s.push(d ?? `Subtotal (${y ?? "Total"})`);
      } else i < t ? s.push(String(r[o.alias] ?? "")) : s.push("");
    });
  } else if (r.isGrandTotal) {
    const t = (l = (u = e.rowAggregates) == null ? void 0 : u.find(
      (o) => o.groupLevel === "ALL"
    )) == null ? void 0 : l.label;
    s.push(t || "Grand Total"), e.groupByColumns.slice(1).forEach(() => s.push(""));
  } else
    e.groupByColumns.forEach((t) => {
      s.push(String(r[t.alias] ?? ""));
    });
  return e.pivotSchema.filter((t) => t.isMetricColumn).forEach((t) => {
    const o = r[t.alias], i = n.columnSettings[t.alias];
    s.push(j(o, i, n));
  }), s;
}
function B(r) {
  const e = r.groupByColumns.map(
    (s) => s.label || s.name
  ), n = r.pivotSchema.filter((s) => s.isMetricColumn).map((s) => s.displayLabel || s.metricLabel || s.metricName || s.alias);
  return [...e, ...n];
}
function q(r, e, n) {
  const s = [];
  if (n.includeHeaders) {
    const u = B(e);
    s.push(f(u, n.delimiter));
  }
  for (const u of r) {
    const l = A(
      u,
      e,
      n
    );
    s.push(
      l.map((t) => p(t, n.delimiter)).join(n.delimiter)
    );
  }
  return s;
}
function S(r, e, n) {
  for (const s of r) {
    if (s.level < n) {
      e[s.level].push(s.name);
      for (let u = 1; u < s.colspan; u++)
        e[s.level].push("");
    }
    s.children ? S(s.children, e, n) : s.columnKey && e[n].push(s.name);
  }
}
function C(r) {
  const e = r.pivotByColumns.length, n = r.groupByColumns.length, s = Array.from(
    { length: e + 1 },
    () => []
  );
  for (let u = 0; u < e; u++)
    s[u].push(...Array(n).fill(""));
  if (s[e].push(
    ...r.groupByColumns.map((u) => u.label || u.name)
  ), S(r.columnHeadersTree, s, e), r.showRowTotals) {
    for (let u = 0; u < e; u++)
      s[u].push("");
    s[e].push("Total");
  }
  return s;
}
function D(r, e, n) {
  if (r == null)
    return "";
  if (!n.useFormattedValues)
    return String(r);
  const s = n.columnSettings[e];
  return typeof r == "number" && (s == null ? void 0 : s.type) === "number" ? h(
    r,
    s.numberFormat,
    n.locale
  ) : typeof r == "number" ? r.toLocaleString(n.locale) : String(r);
}
function x(r, e, n) {
  const s = [];
  r.fieldValues.forEach((l, t) => {
    r.isSubtotal && t === 0 ? s.push(`${l} Total`) : r.isSubtotal ? s.push("") : s.push(String(l ?? ""));
  });
  let u = 0;
  return e.columnHeaders.forEach((l) => {
    var i;
    const t = (i = r.cells[l]) == null ? void 0 : i.value, o = D(t, l, n);
    s.push(o), e.showRowTotals && typeof t == "number" && (u += t);
  }), e.showRowTotals && s.push(u.toLocaleString(n.locale)), s;
}
function $(r, e, n) {
  const s = [];
  if (n.includeHeaders) {
    const u = C(e);
    for (const l of u)
      s.push(f(l, n.delimiter));
  }
  for (const u of r) {
    const l = x(
      u,
      e,
      n
    );
    s.push(
      l.map((t) => p(t, n.delimiter)).join(n.delimiter)
    );
  }
  return s;
}
export {
  E as arrayToCSV,
  f as arrayToCSVRow,
  p as escapeCSVValue,
  A as formatAggregateRow,
  q as formatAggregateTableForCSV,
  L as formatCellValue,
  G as formatCurrency,
  w as formatDate,
  O as formatDuration,
  W as formatNumber,
  I as formatNumberCustom,
  h as formatNumberWithColumnSettings,
  J as formatPercent,
  x as formatPivotDataRow,
  $ as formatPivotTableForCSV,
  V as formatRelativeTime,
  R as formatRowForExport,
  K as formatScientific,
  H as formatTableForCSV,
  B as generateAggregateHeaders,
  C as generatePivotHeaders,
  P as generateTableCSV,
  Q as getTimezoneAbbreviation,
  U as getTimezoneName,
  N as objectsToCSV,
  X as parseWithSourceTimezone,
  Y as resolveTimezone
};
