"use client";

import { Plus, RefreshCw, Rocket, Save } from "lucide-react";
import { useTranslation } from "react-i18next";

import { Button } from "@/components/ui/button";
import { cn } from "@/lib/utils";

type ConfigVersionActionsProps = {
  isDraft: boolean;
  /** 为 false 时仅保留刷新，隐藏新建/保存/发布（只读权限） */
  canManage?: boolean;
  loadingList?: boolean;
  loadingDetail?: boolean;
  saving?: boolean;
  publishLabel?: string;
  onRefresh: () => void;
  onNewDraft: () => void;
  onSaveDraft: () => void;
  onPublish: () => void;
  className?: string;
};

export function ConfigVersionActions({
  isDraft,
  canManage = true,
  loadingList = false,
  loadingDetail = false,
  saving = false,
  publishLabel,
  onRefresh,
  onNewDraft,
  onSaveDraft,
  onPublish,
  className,
}: ConfigVersionActionsProps) {
  const { t } = useTranslation("config");
  const draftActionBusy = saving || loadingDetail;
  const resolvedPublishLabel = publishLabel ?? t("versionActions.publishCurrent");

  return (
    <div className={cn("flex flex-wrap items-center gap-2", className)}>
      <Button type="button" variant="outline" disabled={loadingList} onClick={onRefresh}>
        <RefreshCw className={loadingList ? "size-4 animate-spin" : "size-4"} aria-hidden />
        {loadingList ? t("versionActions.refreshing") : t("versionActions.refresh")}
      </Button>
      {canManage ? (
        <Button type="button" disabled={saving} onClick={onNewDraft}>
          <Plus className="size-4" aria-hidden />
          {t("versionActions.newDraft")}
        </Button>
      ) : null}
      {canManage && isDraft ? (
        <>
          <Button
            type="button"
            variant="secondary"
            disabled={draftActionBusy}
            onClick={onSaveDraft}
          >
            <Save className="size-4" aria-hidden />
            {t("versionActions.saveDraft")}
          </Button>
          <Button type="button" disabled={draftActionBusy} onClick={onPublish}>
            <Rocket className="size-4" aria-hidden />
            {resolvedPublishLabel}
          </Button>
        </>
      ) : null}
    </div>
  );
}
