"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useUpdateDeviceName = exports.useSwitchSession = exports.useRemoveDevice = exports.useLogoutSession = exports.useLogoutAll = void 0;
var _reactQuery = require("@tanstack/react-query");
var _queryKeys = require("../queries/queryKeys");
var _OxyContext = require("../../context/OxyContext");
var _sonner = require("../../../lib/sonner");
/**
 * Switch active session
 */
const useSwitchSession = () => {
  const {
    switchSession,
    activeSessionId
  } = (0, _OxyContext.useOxy)();
  const queryClient = (0, _reactQuery.useQueryClient)();
  return (0, _reactQuery.useMutation)({
    mutationFn: async sessionId => {
      return await switchSession(sessionId);
    },
    onSuccess: user => {
      // Invalidate all session queries
      (0, _queryKeys.invalidateSessionQueries)(queryClient);

      // Update current user query
      queryClient.setQueryData(_queryKeys.queryKeys.accounts.current(), user);

      // Invalidate account queries
      queryClient.invalidateQueries({
        queryKey: _queryKeys.queryKeys.accounts.all
      });
    },
    onError: error => {
      _sonner.toast.error(error instanceof Error ? error.message : 'Failed to switch session');
    }
  });
};

/**
 * Logout from a session
 */
exports.useSwitchSession = useSwitchSession;
const useLogoutSession = () => {
  const {
    oxyServices,
    activeSessionId,
    sessions
  } = (0, _OxyContext.useOxy)();
  const queryClient = (0, _reactQuery.useQueryClient)();
  return (0, _reactQuery.useMutation)({
    mutationFn: async targetSessionId => {
      if (!activeSessionId) {
        throw new Error('No active session');
      }
      const sessionToLogout = targetSessionId || activeSessionId;
      await oxyServices.logoutSession(activeSessionId, sessionToLogout);
      return sessionToLogout;
    },
    onMutate: async targetSessionId => {
      // Cancel outgoing queries
      await queryClient.cancelQueries({
        queryKey: _queryKeys.queryKeys.sessions.all
      });

      // Snapshot previous sessions
      const previousSessions = queryClient.getQueryData(_queryKeys.queryKeys.sessions.list());

      // Optimistically remove session
      if (previousSessions) {
        const sessionToLogout = targetSessionId || activeSessionId;
        const updatedSessions = previousSessions.filter(s => s.sessionId !== sessionToLogout);
        queryClient.setQueryData(_queryKeys.queryKeys.sessions.list(), updatedSessions);
      }
      return {
        previousSessions
      };
    },
    onError: (error, targetSessionId, context) => {
      // Rollback on error
      if (context?.previousSessions) {
        queryClient.setQueryData(_queryKeys.queryKeys.sessions.list(), context.previousSessions);
      }
      _sonner.toast.error(error instanceof Error ? error.message : 'Failed to logout');
    },
    onSuccess: () => {
      // Invalidate all session queries
      (0, _queryKeys.invalidateSessionQueries)(queryClient);
    },
    onSettled: () => {
      queryClient.invalidateQueries({
        queryKey: _queryKeys.queryKeys.sessions.all
      });
    }
  });
};

/**
 * Logout from all sessions
 */
exports.useLogoutSession = useLogoutSession;
const useLogoutAll = () => {
  const {
    oxyServices,
    activeSessionId,
    clearSessionState
  } = (0, _OxyContext.useOxy)();
  const queryClient = (0, _reactQuery.useQueryClient)();
  return (0, _reactQuery.useMutation)({
    mutationFn: async () => {
      if (!activeSessionId) {
        throw new Error('No active session');
      }
      await oxyServices.logoutAllSessions(activeSessionId);
      await clearSessionState();
    },
    onSuccess: () => {
      // Clear all queries
      queryClient.clear();
      _sonner.toast.success('Logged out from all sessions');
    },
    onError: error => {
      _sonner.toast.error(error instanceof Error ? error.message : 'Failed to logout from all sessions');
    }
  });
};

/**
 * Update device name
 */
exports.useLogoutAll = useLogoutAll;
const useUpdateDeviceName = () => {
  const {
    oxyServices,
    activeSessionId
  } = (0, _OxyContext.useOxy)();
  const queryClient = (0, _reactQuery.useQueryClient)();
  return (0, _reactQuery.useMutation)({
    mutationFn: async deviceName => {
      if (!activeSessionId) {
        throw new Error('No active session');
      }
      return await oxyServices.updateDeviceName(activeSessionId, deviceName);
    },
    onSuccess: () => {
      // Invalidate device and session queries
      queryClient.invalidateQueries({
        queryKey: _queryKeys.queryKeys.devices.all
      });
      queryClient.invalidateQueries({
        queryKey: _queryKeys.queryKeys.sessions.all
      });
      _sonner.toast.success('Device name updated');
    },
    onError: error => {
      _sonner.toast.error(error instanceof Error ? error.message : 'Failed to update device name');
    }
  });
};

/**
 * Remove a device
 */
exports.useUpdateDeviceName = useUpdateDeviceName;
const useRemoveDevice = () => {
  const {
    oxyServices
  } = (0, _OxyContext.useOxy)();
  const queryClient = (0, _reactQuery.useQueryClient)();
  return (0, _reactQuery.useMutation)({
    mutationFn: async deviceId => {
      await oxyServices.removeDevice(deviceId);
      return deviceId;
    },
    onSuccess: () => {
      // Invalidate device queries
      queryClient.invalidateQueries({
        queryKey: _queryKeys.queryKeys.devices.all
      });
      queryClient.invalidateQueries({
        queryKey: _queryKeys.queryKeys.sessions.all
      });
      _sonner.toast.success('Device removed');
    },
    onError: error => {
      _sonner.toast.error(error instanceof Error ? error.message : 'Failed to remove device');
    }
  });
};
exports.useRemoveDevice = useRemoveDevice;
//# sourceMappingURL=useServicesMutations.js.map