const T = "# Start date:", U = "# End date:", g = /^#\s*(\d{8})\s*-\s*(\d{8})\s*$/, $ = 100, b = [
  "event_action",
  "event_label",
  "Event count",
  "Total users",
  "Event count per active user"
], I = ["event_action", "event_label"], O = ["event count"], M = ["total users", "active users"], w = ["event count per active user", "event countactive user"], X = b.join(","), G = "[data-track-ga-event-action][data-track-ga-event-label]", Q = {
  seen: "rioglyph-eye-open",
  clicked: "rioglyph-click"
}, V = (t) => t.slice(0, 100), P = (t, e) => `${t}::${V(e)}`, W = (t) => t === "visibility" ? "seen" : t === "click" ? "clicked" : null, f = (t) => {
  const e = t.trim();
  if (!/^\d{8}$/.test(e))
    return null;
  const s = Number(e.slice(0, 4)), n = Number(e.slice(4, 6)), r = Number(e.slice(6, 8)), c = new Date(Date.UTC(s, n - 1, r));
  return c.getUTCFullYear() !== s || c.getUTCMonth() !== n - 1 || c.getUTCDate() !== r ? null : c;
}, y = (t) => new Intl.DateTimeFormat("en-US", {
  month: "long",
  day: "numeric",
  year: "numeric",
  timeZone: "UTC"
}).format(t), k = (t) => {
  const e = t.split(/\r?\n/), s = e.find((a) => a.startsWith(T)), n = e.find((a) => a.startsWith(U)), r = s?.slice(T.length).trim() ?? "", c = n?.slice(U.length).trim() ?? "", i = f(r), o = f(c);
  if (i !== null && o !== null)
    return { startDate: i, endDate: o };
  const l = e.find((a) => g.test(a.trim()));
  if (!l)
    return { startDate: i, endDate: o };
  const u = l.trim().match(g);
  if (!u)
    return { startDate: i, endDate: o };
  const d = f(u[1]), v = f(u[2]);
  return { startDate: d, endDate: v };
}, D = (t) => t.split(/\r?\n/).map((e) => e.trim()).filter((e) => e && !e.startsWith("#")), A = (t) => {
  const e = [];
  let s = "", n = !1;
  for (let r = 0; r < t.length; r += 1) {
    const c = t[r];
    if (c === '"') {
      const i = t[r + 1];
      if (n && i === '"') {
        s += '"', r += 1;
        continue;
      }
      n = !n;
      continue;
    }
    if (c === "," && !n) {
      e.push(s.trim()), s = "";
      continue;
    }
    s += c;
  }
  return e.push(s.trim()), e;
}, R = (t) => t.trim().toLowerCase().replace(/\s+/g, " "), E = (t, e) => {
  const s = new Set(e.map((n) => R(n)));
  return t.findIndex((n) => s.has(R(n)));
}, _ = (t) => !t || !t.trim() ? !1 : Number.isFinite(Number(t)), h = (t) => {
  const e = t?.trim() ?? "";
  return e !== "" && !_(e);
}, H = (t) => {
  let e = E(t, [I[0]]), s = E(t, [I[1]]), n = E(t, O), r = E(t, M), c = E(t, w);
  return e === -1 && t.length >= 1 && (e = 0), s === -1 && t.length >= 2 && (s = e + 1), n === -1 && t.length >= 3 && (n = s + 1), r === -1 && t.length >= 4 && (r = n + 1), c === -1 && t.length >= 5 && (c = r + 1), e < 0 || s < 0 || n < 0 || r < 0 || c < 0 ? null : {
    eventActionIndex: e,
    eventLabelIndex: s,
    eventCountIndex: n,
    totalUsersIndex: r,
    eventCountPerUserIndex: c
  };
}, x = (t, e) => {
  const s = t.slice(1);
  let n = 0, r = 0;
  for (const c of s) {
    const i = A(c), o = i[e.eventActionIndex], l = i[e.eventLabelIndex];
    if (!o && !l)
      continue;
    n += 1;
    const u = i[e.eventCountIndex], d = i[e.totalUsersIndex];
    if (h(o) && h(l) && _(u) && _(d) && (r += 1), n >= 5)
      break;
  }
  return n === 0 ? !0 : r >= Math.ceil(n / 2);
}, C = (t) => {
  if (t.length < 2)
    return null;
  const e = A(t[0]), s = H(e);
  if (s && x(t, s))
    return s;
  const n = {
    eventActionIndex: 0,
    eventLabelIndex: 1,
    eventCountIndex: 2,
    totalUsersIndex: 3,
    eventCountPerUserIndex: 4
  };
  return x(t, n) ? n : null;
}, q = (t) => {
  const e = D(t);
  if (e.length === 0)
    return !1;
  const s = A(e[0]);
  return I.every(
    (r) => s.some((c) => R(c) === r)
  ) && C(e) !== null;
}, F = (t) => {
  const e = Number(t);
  return Number.isFinite(e) ? e : 0;
}, S = (t) => !(t?.trim().toLowerCase() ?? ""), N = (t, e, s, n) => {
  if (t !== 0)
    return !1;
  const r = e[s], c = e[n];
  return S(r) && S(c);
}, Y = (t) => {
  const e = D(t);
  if (e.length < 2)
    return null;
  const s = C(e);
  if (!s)
    return null;
  const n = A(e[1]);
  return N(0, n, s.eventActionIndex, s.eventLabelIndex) ? F(n[s.totalUsersIndex]) : null;
}, z = (t, e) => {
  const s = D(t);
  if (s.length < 2)
    return [];
  const n = C(s);
  return n ? s.slice(1).reduce((r, c, i) => {
    const o = A(c);
    if (N(i, o, n.eventActionIndex, n.eventLabelIndex))
      return r;
    const l = o[n.eventActionIndex], u = o[n.eventLabelIndex];
    return !l || !u || r.push({
      reportName: e,
      eventAction: l,
      eventLabel: u,
      eventCount: o[n.eventCountIndex] ?? "0",
      totalUsers: o[n.totalUsersIndex] ?? "0",
      eventCountPerActiveUser: o[n.eventCountPerUserIndex] ?? "0"
    }), r;
  }, []) : [];
}, K = (t) => {
  const e = t.map((a) => ({
    analyticsEntries: z(a.csvRaw, a.reportName),
    analyticsTotalUsers: Y(a.csvRaw)
  })), s = e.flatMap((a) => a.analyticsEntries), n = e.map((a) => a.analyticsTotalUsers).filter((a) => a !== null), r = n.length > 0 ? n.reduce((a, m) => a + m, 0) : null, c = s.reduce((a, m) => {
    const L = P(m.eventAction, m.eventLabel), p = a.get(L) ?? [];
    return p.push(m), a.set(L, p), a;
  }, /* @__PURE__ */ new Map()), i = t.map((a) => k(a.csvRaw)), o = i.map((a) => a.startDate).filter((a) => a !== null), l = i.map((a) => a.endDate).filter((a) => a !== null), u = o.length > 0 ? new Date(Math.min(...o.map((a) => a.getTime()))) : null, d = l.length > 0 ? new Date(Math.max(...l.map((a) => a.getTime()))) : null, v = u && d ? `${y(u)} - ${y(d)}` : null;
  return { analyticsLookup: c, analyticsDateRangeLabel: v, analyticsTotalUsers: r };
};
export {
  G as ANALYTICS_OVERLAY_SELECTOR,
  Q as EVENT_TYPE_ICON_CLASS,
  $ as LOOKUP_MAX_LENGTH,
  X as REQUIRED_ANALYTICS_HEADER_ROW,
  K as buildAnalyticsAnalysisData,
  P as createAnalyticsLookupKey,
  q as hasRequiredAnalyticsHeaderRow,
  z as parseAnalyticsReportCsv,
  W as toEventType
};
//# sourceMappingURL=analyticsAnalysisUtils.js.map
