"use client";

import { Plus, RefreshCw, 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;
  /** 合并编辑页由底部操作栏承接保存/发布时隐藏 */
  suppressDraftActions?: boolean;
  onRefresh: () => void;
  onNewDraft: () => void;
  onSaveDraft: () => void;
  onPublish: () => void;
  className?: string;
};

export function ConfigVersionActions({
  isDraft,
  canManage = false,
  loadingList = false,
  loadingDetail = false,
  saving = false,
  publishLabel,
  suppressDraftActions = false,
  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-1", className)}>
      <Button
        type="button"
        variant="ghost"
        size="icon-sm"
        disabled={loadingList}
        onClick={onRefresh}
        aria-label={loadingList ? t("versionActions.refreshing") : t("versionActions.refresh")}
        title={loadingList ? t("versionActions.refreshing") : t("versionActions.refresh")}
      >
        <RefreshCw className={cn("size-4", loadingList && "animate-spin")} aria-hidden />
      </Button>

      {canManage ? (
        <>
          <span className="mx-0.5 hidden h-5 w-px bg-border/60 sm:block" aria-hidden />
          <Button type="button" variant="outline" size="sm" disabled={saving} onClick={onNewDraft}>
            <Plus className="size-3.5" aria-hidden />
            {t("versionActions.newDraft")}
          </Button>
          {isDraft && !suppressDraftActions ? (
            <>
              <Button
                type="button"
                variant="outline"
                size="sm"
                disabled={draftActionBusy}
                onClick={onSaveDraft}
              >
                <Save className="size-3.5" aria-hidden />
                {t("versionActions.saveDraft")}
              </Button>
              <Button type="button" size="sm" disabled={draftActionBusy} onClick={onPublish}>
                {resolvedPublishLabel}
              </Button>
            </>
          ) : null}
        </>
      ) : null}
    </div>
  );
}
