'use client';

// AUTO-GENERATED by openapi_processor / ts.hooks
// POST /cfg/totp/backup-codes/regenerate/
// DO NOT EDIT — re-run `make gen`.

import useSWRMutation from "swr/mutation";
import type { SWRMutationConfiguration } from "swr/mutation";
import { CfgTotpBackupCodes } from "../../sdk.gen";
import type { CfgTotpBackupCodesRegenerateCreateData, CfgTotpBackupCodesRegenerateCreateResponses } from "../../types.gen";
import { BackupCodesRegenerateResponseSchema } from "../schemas/BackupCodesRegenerateResponse";

type Args = Omit<CfgTotpBackupCodesRegenerateCreateData, "url">;
type Result = CfgTotpBackupCodesRegenerateCreateResponses[keyof CfgTotpBackupCodesRegenerateCreateResponses];

export function useCfgTotpBackupCodesRegenerateCreate(
  config?: SWRMutationConfiguration<Result, Error, readonly ["cfg_totp_backup_codes_regenerate_create"], Args>,
) {
  return useSWRMutation<Result, Error, readonly ["cfg_totp_backup_codes_regenerate_create"], Args>(
    ["cfg_totp_backup_codes_regenerate_create"] as const,
    async (_key, { arg }: { arg: Args }) => {
      const res = await CfgTotpBackupCodes.cfgTotpBackupCodesRegenerateCreate({ ...arg, throwOnError: true } as never);
      const data = res.data as Result;
      const parsed = BackupCodesRegenerateResponseSchema.safeParse(data);
      if (!parsed.success) {
        // Log first so the failure is always visible, even if the
        // event handler swallows it.
        console.warn(
          "[zod] response did not match schema",
          {
            operation: "cfg_totp_backup_codes_regenerate_create",
            method: "POST",
            path: "/cfg/totp/backup-codes/regenerate/",
            issues: parsed.error.issues,
            data,
          },
        );
        if (typeof window !== "undefined") {
          try {
            window.dispatchEvent(
              new CustomEvent("zod-validation-error", {
                detail: {
                  operation: "cfg_totp_backup_codes_regenerate_create",
                  method: "POST",
                  path: "/cfg/totp/backup-codes/regenerate/",
                  issues: parsed.error.issues,
                  data,
                  timestamp: new Date(),
                },
                bubbles: true,
                cancelable: false,
              }),
            );
          } catch {
            // Event dispatch is best-effort.
          }
        }
        return data;
      }
      return parsed.data as unknown as Result;
    },
    config,
  );
}
