import { jsx as P } from "react/jsx-runtime";
import { useState as t, useCallback as v } from "react";
import $ from "../notification/Notification.js";
import H from "../../hooks/useDraggableElement.js";
import { hasRequiredAnalyticsHeaderRow as q, buildAnalyticsAnalysisData as V } from "../../utils/analytics/analyticsAnalysisUtils.js";
import _ from "../../utils/analytics/useAnalyticsOverlayDom.js";
import { AnalyticsAnalysisBanner as j } from "./AnalyticsAnalysisBanner.js";
const z = {
  top: "70px",
  left: "50%",
  transform: "translateX(-50%)"
}, Z = (b) => {
  const { overlayBackgroundOpacity: A = 0.5, bannerStartCoordinates: y } = b, C = Math.min(1, Math.max(0, A)), r = y?.top, i = y?.left, u = typeof r == "number" && Number.isFinite(r) && typeof i == "number" && Number.isFinite(i), [D, n] = t(() => /* @__PURE__ */ new Map()), [O, s] = t(null), [h, l] = t(null), [w, o] = t(null), [m, L] = t(!0), [d, T] = t(!0), x = u ? { top: r, left: i } : void 0, { draggableRef: k, isDragging: N, position: e, onDragHandleDown: R, renderElement: S } = H({
    initialPosition: x
  }), B = _({
    analyticsLookup: D,
    overlayBackgroundOpacity: C,
    showOverlayDetails: m,
    showOverlayLabelText: d,
    analyticsTotalUsers: h
  }), M = v(async (a) => {
    const f = a.filter((I) => I.name.toLowerCase().endsWith(".csv"));
    if (f.length === 0) {
      n(/* @__PURE__ */ new Map()), s(null), l(null), o(null);
      return;
    }
    const c = f[0], g = await c.text();
    if (!q(g)) {
      $.error(
        'A Google Analytics CSV report based on the "UIKIT AnalyticsAnalysisOverlay Template" is required.',
        {
          title: "Unsupported CSV"
        }
      ), n(/* @__PURE__ */ new Map()), s(null), l(null), o(null);
      return;
    }
    const E = [
      {
        reportName: c.name,
        csvRaw: g
      }
    ], p = V(E);
    n(p.analyticsLookup), s(p.analyticsDateRangeLabel), l(p.analyticsTotalUsers), o(c.name);
  }, []), U = v(() => {
    n(/* @__PURE__ */ new Map()), s(null), l(null), o(null);
  }, []), F = e.hasDragged ? {
    top: `${e.top}px`,
    left: `${e.left}px`
  } : u ? {
    top: `${e.top}px`,
    left: `${e.left}px`
  } : z;
  return S(
    /* @__PURE__ */ P(
      j,
      {
        bannerRef: k,
        bannerStyle: F,
        analyticsDateRangeLabel: O,
        loadedCsvName: w,
        showOverlayDetails: m,
        showOverlayLabelText: d,
        isDragging: N,
        onCsvPick: M,
        onToggleOverlayDetails: () => L((a) => !a),
        onToggleOverlayLabelText: () => T((a) => !a),
        onClearCsv: U,
        onDragHandleDown: R
      }
    ),
    B
  );
};
export {
  Z as AnalyticsAnalysisOverlay
};
//# sourceMappingURL=AnalyticsAnalysisOverlay.js.map
