'use client';

// AUTO-GENERATED by openapi_processor / ts.hooks
// Delete user account
// DO NOT EDIT — re-run `make gen`.

import useSWRMutation from "swr/mutation";
import type { SWRMutationConfiguration } from "swr/mutation";
import { CfgAccountsProfile } from "../../sdk.gen";
import type { CfgAccountsProfileDeleteCreateData, CfgAccountsProfileDeleteCreateResponses } from "../../types.gen";
import { AccountDeleteResponseSchema } from "../schemas/AccountDeleteResponse";

type Args = Omit<CfgAccountsProfileDeleteCreateData, "url">;
type Result = CfgAccountsProfileDeleteCreateResponses[keyof CfgAccountsProfileDeleteCreateResponses];

export function useCfgAccountsProfileDeleteCreate(
  config?: SWRMutationConfiguration<Result, Error, readonly ["cfg_accounts_profile_delete_create"], Args>,
) {
  return useSWRMutation<Result, Error, readonly ["cfg_accounts_profile_delete_create"], Args>(
    ["cfg_accounts_profile_delete_create"] as const,
    async (_key, { arg }: { arg: Args }) => {
      const res = await CfgAccountsProfile.cfgAccountsProfileDeleteCreate({ ...arg, throwOnError: true } as never);
      const data = res.data as Result;
      const parsed = AccountDeleteResponseSchema.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_accounts_profile_delete_create",
            method: "POST",
            path: "/cfg/accounts/profile/delete/",
            issues: parsed.error.issues,
            data,
          },
        );
        if (typeof window !== "undefined") {
          try {
            window.dispatchEvent(
              new CustomEvent("zod-validation-error", {
                detail: {
                  operation: "cfg_accounts_profile_delete_create",
                  method: "POST",
                  path: "/cfg/accounts/profile/delete/",
                  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,
  );
}
