/**
 * Copyright (c) 2023-present Pi Dash Software, Inc. and contributors
 * SPDX-License-Identifier: AGPL-3.0-only
 * See the LICENSE file for details.
 */

import { useCallback } from "react";
import { LogOut, Mails } from "lucide-react";
// pi dash imports
import { useTranslation } from "@pi-dash/i18n";
import { setToast, TOAST_TYPE } from "@pi-dash/propel/toast";
// components
import type { TPowerKCommandConfig } from "@/components/power-k/core/types";
// hooks
import { useUser } from "@/hooks/store/user";
import { useAppRouter } from "@/hooks/use-app-router";

/**
 * Account commands - Account related commands
 */
export const usePowerKAccountCommands = (): TPowerKCommandConfig[] => {
  // navigation
  const router = useAppRouter();
  // store
  const { signOut } = useUser();
  // translation
  const { t } = useTranslation();

  const handleSignOut = useCallback(() => {
    signOut().catch(() =>
      setToast({
        type: TOAST_TYPE.ERROR,
        title: t("Error!"),
        message: t("Failed to sign out. Please try again."),
      })
    );
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [signOut]);

  return [
    {
      id: "workspace_invites",
      type: "action",
      group: "account",
      i18n_title: "Workspace invites",
      icon: Mails,
      action: () => router.push("/invitations"),
      isEnabled: () => true,
      isVisible: () => true,
      closeOnSelect: true,
    },
    {
      id: "sign_out",
      type: "action",
      group: "account",
      i18n_title: "Sign out",
      icon: LogOut,
      action: handleSignOut,
      isEnabled: () => true,
      isVisible: () => true,
      closeOnSelect: true,
    },
  ];
};
