"use client";
import { jsx, jsxs } from "react/jsx-runtime";
import { useMemo } from "react";
import { useQuery } from "@tanstack/react-query";
import { apiCall } from "../utils/apiCall.js";
function EventSelect({
  value,
  onChange,
  placeholder = "Select an event...",
  className,
  disabled,
  categories,
  modules,
  excludeTriggerExcluded = true
}) {
  const { data: allEvents = [], isLoading } = useQuery({
    queryKey: ["declared-events", excludeTriggerExcluded],
    queryFn: async () => {
      const result = await apiCall(
        `/api/events?excludeTriggerExcluded=${excludeTriggerExcluded}`
      );
      if (!result.ok) return [];
      return result.result?.data || [];
    },
    staleTime: 5 * 60 * 1e3
    // Cache for 5 minutes
  });
  const filteredEvents = useMemo(() => {
    let events = allEvents;
    if (categories?.length) {
      events = events.filter((e) => e.category && categories.includes(e.category));
    }
    if (modules?.length) {
      events = events.filter((e) => e.module && modules.includes(e.module));
    }
    return events;
  }, [allEvents, categories, modules]);
  const eventsByModule = useMemo(() => {
    const grouped = {};
    for (const event of filteredEvents) {
      const module = event.module || "other";
      if (!grouped[module]) grouped[module] = [];
      grouped[module].push(event);
    }
    return Object.fromEntries(
      Object.entries(grouped).sort(([a], [b]) => a.localeCompare(b))
    );
  }, [filteredEvents]);
  const formatModuleName = (module) => {
    return module.charAt(0).toUpperCase() + module.slice(1).replace(/_/g, " ");
  };
  const isEmpty = !isLoading && filteredEvents.length === 0;
  return /* @__PURE__ */ jsxs(
    "select",
    {
      value,
      onChange: (e) => onChange(e.target.value),
      className: `h-10 rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50 ${className || ""}`,
      disabled: disabled || isLoading,
      children: [
        /* @__PURE__ */ jsx("option", { value: "", disabled: true, children: isLoading ? "Loading..." : isEmpty ? "No events available" : placeholder }),
        Object.entries(eventsByModule).map(([module, moduleEvents]) => /* @__PURE__ */ jsx("optgroup", { label: formatModuleName(module), children: moduleEvents.map((event) => /* @__PURE__ */ jsx("option", { value: event.id, children: event.label }, event.id)) }, module))
      ]
    }
  );
}
function useAvailableEvents(options) {
  const excludeTriggerExcluded = options?.excludeTriggerExcluded !== false;
  const { data: allEvents = [], isLoading, error, refetch } = useQuery({
    queryKey: ["declared-events", excludeTriggerExcluded],
    queryFn: async () => {
      const result = await apiCall(
        `/api/events?excludeTriggerExcluded=${excludeTriggerExcluded}`
      );
      if (!result.ok) return [];
      return result.result?.data || [];
    },
    staleTime: 5 * 60 * 1e3
  });
  const filteredEvents = useMemo(() => {
    let events = allEvents;
    if (options?.categories?.length) {
      events = events.filter((e) => e.category && options.categories.includes(e.category));
    }
    if (options?.modules?.length) {
      events = events.filter((e) => e.module && options.modules.includes(e.module));
    }
    return events;
  }, [allEvents, options]);
  const eventsByModule = useMemo(() => {
    const grouped = {};
    for (const event of filteredEvents) {
      const module = event.module || "other";
      if (!grouped[module]) grouped[module] = [];
      grouped[module].push(event);
    }
    return Object.fromEntries(
      Object.entries(grouped).sort(([a], [b]) => a.localeCompare(b))
    );
  }, [filteredEvents]);
  return {
    events: filteredEvents,
    eventsByModule,
    isLoading,
    error,
    refetch
  };
}
var EventSelect_default = EventSelect;
export {
  EventSelect,
  EventSelect_default as default,
  useAvailableEvents
};
//# sourceMappingURL=EventSelect.js.map
