"use client";
import * as React from "react";
import { apiCall } from "@open-mercato/ui/backend/utils/apiCall";
import { expandOccurrences } from "../../lib/calendar/recurrence.js";
import { getFetchWindow } from "../../lib/calendar/range.js";
import { mapInteractionToCalendarItem } from "../../lib/calendar/mapItem.js";
import {
  calendarInteractionPayloadSchema
} from "./types.js";
const PAGE_LIMIT = 100;
const MAX_WINDOW_ITEMS = 500;
async function fetchInteractionWindow(window, signal) {
  const collected = [];
  let cursor;
  let truncated = false;
  do {
    const params = new URLSearchParams({
      from: window.from.toISOString(),
      to: window.to.toISOString(),
      limit: String(PAGE_LIMIT)
    });
    if (cursor) params.set("cursor", cursor);
    const call = await apiCall(
      `/api/customers/interactions?${params.toString()}`,
      { signal }
    );
    if (!call.ok) throw new Error(`[internal] calendar interactions fetch failed (${call.status})`);
    const pageItems = Array.isArray(call.result?.items) ? call.result.items : [];
    for (const rawItem of pageItems) {
      const parsed = calendarInteractionPayloadSchema.safeParse(rawItem);
      if (parsed.success) collected.push(parsed.data);
    }
    cursor = typeof call.result?.nextCursor === "string" ? call.result.nextCursor : void 0;
    if (cursor && collected.length >= MAX_WINDOW_ITEMS) {
      truncated = true;
      cursor = void 0;
    }
  } while (cursor);
  return { payloads: collected.slice(0, MAX_WINDOW_ITEMS), truncated };
}
function useCalendarItems(range) {
  const [payloads, setPayloads] = React.useState([]);
  const [isLoading, setIsLoading] = React.useState(true);
  const [error, setError] = React.useState(null);
  const [truncated, setTruncated] = React.useState(false);
  const [typeLabels, setTypeLabels] = React.useState({});
  const [typeColors, setTypeColors] = React.useState({});
  const [typeIcons, setTypeIcons] = React.useState({});
  const [reloadToken, setReloadToken] = React.useState(0);
  const fromTime = range.from.getTime();
  const toTime = range.to.getTime();
  React.useEffect(() => {
    const controller = new AbortController();
    let cancelled = false;
    async function loadActivityTypes() {
      const call = await apiCall(
        "/api/customers/dictionaries/activity-types",
        { signal: controller.signal }
      );
      if (cancelled || !call.ok) return;
      const entries = Array.isArray(call.result?.items) ? call.result.items : [];
      const labels = {};
      const colors = {};
      const icons = {};
      for (const entry of entries) {
        if (typeof entry?.value !== "string" || entry.value.length === 0) continue;
        labels[entry.value] = typeof entry.label === "string" && entry.label.length > 0 ? entry.label : entry.value;
        colors[entry.value] = typeof entry.color === "string" && entry.color.length > 0 ? entry.color : null;
        icons[entry.value] = typeof entry.icon === "string" && entry.icon.length > 0 ? entry.icon : null;
      }
      setTypeLabels(labels);
      setTypeColors(colors);
      setTypeIcons(icons);
    }
    loadActivityTypes().catch(() => {
      if (cancelled || controller.signal.aborted) return;
      setTypeLabels({});
      setTypeColors({});
      setTypeIcons({});
    });
    return () => {
      cancelled = true;
      controller.abort();
    };
  }, []);
  React.useEffect(() => {
    const controller = new AbortController();
    let cancelled = false;
    async function loadInteractions() {
      setIsLoading(true);
      setError(null);
      try {
        const fetchWindow = getFetchWindow({ from: new Date(fromTime), to: new Date(toTime) });
        const { payloads: collected, truncated: windowTruncated } = await fetchInteractionWindow(
          fetchWindow,
          controller.signal
        );
        if (cancelled) return;
        setPayloads(collected);
        setTruncated(windowTruncated);
      } catch (err) {
        if (cancelled || controller.signal.aborted) return;
        setPayloads([]);
        setTruncated(false);
        setError(err instanceof Error ? err.message : "[internal] calendar interactions fetch failed");
      } finally {
        if (!cancelled) setIsLoading(false);
      }
    }
    void loadInteractions();
    return () => {
      cancelled = true;
      controller.abort();
    };
  }, [fromTime, toTime, reloadToken]);
  const items = React.useMemo(() => {
    const expansionWindow = getFetchWindow({ from: new Date(fromTime), to: new Date(toTime) });
    const mapped = [];
    for (const payload of payloads) {
      const item = mapInteractionToCalendarItem(payload, typeColors);
      if (!item) continue;
      mapped.push(...expandOccurrences(item, expansionWindow));
    }
    return mapped;
  }, [payloads, typeColors, fromTime, toTime]);
  const refetch = React.useCallback(() => {
    setReloadToken((token) => token + 1);
  }, []);
  return { items, isLoading, error, truncated, typeLabels, typeColors, typeIcons, refetch };
}
export {
  MAX_WINDOW_ITEMS,
  fetchInteractionWindow,
  useCalendarItems
};
//# sourceMappingURL=useCalendarItems.js.map
