import { type MenuProps } from '@lobehub/ui';
import { Icon } from '@lobehub/ui';
import { confirmModal } from '@lobehub/ui/base-ui';
import { PencilLine, Trash } from 'lucide-react';
import { useCallback } from 'react';
import { useTranslation } from 'react-i18next';

import { useKnowledgeBaseStore } from '@/store/library';

interface ProjectItemDropdownMenuProps {
  id: string;
  toggleEditing: (visible?: boolean) => void;
}

export const useProjectItemDropdownMenu = ({
  id,
  toggleEditing,
}: ProjectItemDropdownMenuProps): (() => MenuProps['items']) => {
  const { t } = useTranslation(['home', 'common']);
  const [removeKnowledgeBase] = useKnowledgeBaseStore((s) => [s.removeKnowledgeBase]);

  return useCallback(
    () => [
      {
        icon: <Icon icon={PencilLine} />,
        key: 'rename',
        label: t('rename', { ns: 'common' }),
        onClick: () => {
          toggleEditing(true);
        },
      },
      {
        type: 'divider',
      },
      {
        danger: true,
        icon: <Icon icon={Trash} />,
        key: 'delete',
        label: t('delete', { ns: 'common' }),
        onClick: () => {
          if (!id) return;
          confirmModal({
            cancelText: t('cancel', { ns: 'common' }),
            content: t('project.deleteConfirm'),
            okButtonProps: { danger: true },
            okText: t('delete', { ns: 'common' }),
            onOk: async () => {
              await removeKnowledgeBase(id);
            },
            title: t('delete', { ns: 'common' }),
          });
        },
      },
    ],
    [t, id, removeKnowledgeBase, toggleEditing],
  );
};
