"use client";
import { useQuery } from "@tanstack/react-query";
import { useAuth } from "./useAuth.js";
import { useTeam } from "./useTeam.js";
function usePatternUsages(patternId, options = {}) {
  var _a, _b, _c, _d, _e;
  const {
    entityType,
    limit = 50,
    page = 1,
    enabled = true,
    staleTime = 1e3 * 60 * 5
    // 5 minutes
  } = options;
  const { user } = useAuth();
  const { teamId } = useTeam();
  const queryKey = [
    "pattern-usages",
    patternId,
    { entityType, limit, page }
  ];
  const query = useQuery({
    queryKey,
    queryFn: async () => {
      var _a2, _b2, _c2, _d2, _e2, _f, _g;
      const queryParams = {
        limit: String(limit),
        page: String(page)
      };
      if (entityType) {
        queryParams.entityType = entityType;
      }
      const params = new URLSearchParams(queryParams);
      const url = `/api/v1/patterns/${patternId}/usages?${params.toString()}`;
      const headers = {
        "Content-Type": "application/json"
      };
      if (teamId) {
        headers["x-team-id"] = teamId;
      }
      const response = await fetch(url, { headers });
      if (!response.ok) {
        if (response.status === 404) {
          throw new Error("Pattern not found");
        }
        throw new Error("Failed to fetch pattern usages");
      }
      const data = await response.json();
      return {
        usages: ((_a2 = data.data) == null ? void 0 : _a2.usages) || [],
        counts: ((_b2 = data.data) == null ? void 0 : _b2.counts) || [],
        total: ((_c2 = data.data) == null ? void 0 : _c2.total) || ((_d2 = data.meta) == null ? void 0 : _d2.total) || 0,
        page: ((_e2 = data.meta) == null ? void 0 : _e2.page) || page,
        totalPages: ((_f = data.meta) == null ? void 0 : _f.totalPages) || Math.ceil((((_g = data.data) == null ? void 0 : _g.total) || 0) / limit)
      };
    },
    enabled: enabled && !!user && !!patternId,
    staleTime,
    gcTime: 1e3 * 60 * 60
    // 1 hour
  });
  return {
    ...query,
    usages: ((_a = query.data) == null ? void 0 : _a.usages) || [],
    counts: ((_b = query.data) == null ? void 0 : _b.counts) || [],
    total: ((_c = query.data) == null ? void 0 : _c.total) || 0,
    page: ((_d = query.data) == null ? void 0 : _d.page) || 1,
    totalPages: ((_e = query.data) == null ? void 0 : _e.totalPages) || 1
  };
}
function usePatternUsageCount(patternId, options = {}) {
  const { enabled = true, staleTime = 1e3 * 60 * 5 } = options;
  const { user } = useAuth();
  const { teamId } = useTeam();
  return useQuery({
    queryKey: ["pattern-usage-count", patternId],
    queryFn: async () => {
      var _a;
      const headers = {
        "Content-Type": "application/json"
      };
      if (teamId) {
        headers["x-team-id"] = teamId;
      }
      const response = await fetch(`/api/v1/patterns/${patternId}/usages?limit=1`, { headers });
      if (!response.ok) {
        return 0;
      }
      const data = await response.json();
      return ((_a = data.data) == null ? void 0 : _a.total) || 0;
    },
    enabled: enabled && !!user && !!patternId,
    staleTime,
    gcTime: 1e3 * 60 * 60
  });
}
export {
  usePatternUsageCount,
  usePatternUsages
};
