import { useElevasisServices } from './chunk-2FTX4WO2.js';
import { useQuery, useQueryClient, useMutation } from '@tanstack/react-query';

var PRIORITY_ORDER = {
  urgent: 0,
  high: 1,
  normal: 2,
  low: 3
};
function sortNotes(notes) {
  return [...notes].sort((a, b) => {
    if (a.pinned !== b.pinned) return a.pinned ? -1 : 1;
    const pa = PRIORITY_ORDER[a.priority] ?? 2;
    const pb = PRIORITY_ORDER[b.priority] ?? 2;
    if (pa !== pb) return pa - pb;
    return new Date(b.updatedAt).getTime() - new Date(a.updatedAt).getTime();
  });
}
var userNotesKeys = {
  all: ["user-notes"],
  list: (workOSOrgId) => [...userNotesKeys.all, "list", workOSOrgId]
};
function useUserNotes() {
  const { apiRequest, isReady, workOSOrganizationId } = useElevasisServices();
  return useQuery({
    queryKey: userNotesKeys.list(workOSOrganizationId),
    queryFn: async () => {
      const response = await apiRequest("/user-notes");
      return sortNotes(response.notes);
    },
    enabled: isReady
  });
}
function useCreateUserNote() {
  const queryClient = useQueryClient();
  const { apiRequest, workOSOrganizationId } = useElevasisServices();
  return useMutation({
    mutationFn: (data) => apiRequest("/user-notes", {
      method: "POST",
      body: JSON.stringify(data)
    }),
    onSuccess: () => {
      queryClient.invalidateQueries({
        queryKey: userNotesKeys.list(workOSOrganizationId)
      });
    }
  });
}
function useUpdateUserNote() {
  const queryClient = useQueryClient();
  const { apiRequest, workOSOrganizationId } = useElevasisServices();
  return useMutation({
    mutationFn: ({ id, ...data }) => apiRequest(`/user-notes/${id}`, {
      method: "PATCH",
      body: JSON.stringify(data)
    }),
    onSuccess: () => {
      queryClient.invalidateQueries({
        queryKey: userNotesKeys.list(workOSOrganizationId)
      });
    }
  });
}
function useDeleteUserNote() {
  const queryClient = useQueryClient();
  const { apiRequest, workOSOrganizationId } = useElevasisServices();
  return useMutation({
    mutationFn: (id) => apiRequest(`/user-notes/${id}`, {
      method: "DELETE"
    }),
    onSuccess: () => {
      queryClient.invalidateQueries({
        queryKey: userNotesKeys.list(workOSOrganizationId)
      });
    }
  });
}

export { useCreateUserNote, useDeleteUserNote, useUpdateUserNote, useUserNotes, userNotesKeys };
