"use client";

import { Trash2 } from "lucide-react";
import { SubmitButton } from "@/components/forms/submit-button";

export function ConfirmActionForm({
  action,
  fields,
  label,
  pendingText = "جارٍ الحذف...",
  confirmMessage,
  className
}: {
  action: (formData: FormData) => void | Promise<void>;
  fields: Record<string, string>;
  label: string;
  pendingText?: string;
  confirmMessage: string;
  className?: string;
}) {
  return (
    <form
      action={action}
      className={className}
      onSubmit={(event) => {
        if (!window.confirm(confirmMessage)) event.preventDefault();
      }}
    >
      {Object.entries(fields).map(([name, value]) => (
        <input key={name} type="hidden" name={name} value={value} />
      ))}
      <SubmitButton pendingText={pendingText} variant="danger" size="sm">
        <Trash2 size={15} aria-hidden />
        {label}
      </SubmitButton>
    </form>
  );
}
