import {
  VisuallyHidden
} from "./chunk-54TMLQH3.js";
import "./chunk-TKGWSUEI.js";
import {
  KeyboundForm
} from "./chunk-U2ZFCAPX.js";
import {
  RouteDrawer,
  useRouteModal
} from "./chunk-FWJLS7XD.js";
import {
  Form
} from "./chunk-3QSRE5LS.js";
import {
  useApiKey,
  useUpdateApiKey
} from "./chunk-YKDEE7ZL.js";
import "./chunk-DL2ZIXBO.js";
import "./chunk-5CK6XEFV.js";
import "./chunk-RHKRREUU.js";
import "./chunk-ZA2KFUFR.js";
import "./chunk-ZBWL72TB.js";
import "./chunk-NBMM2TZK.js";

// src/pages/api-key-management/api-key-management-edit/api-key-management-edit.tsx
import { Heading } from "@medusajs/ui";
import { useTranslation as useTranslation2 } from "react-i18next";
import { useParams } from "react-router-dom";

// src/pages/api-key-management/api-key-management-edit/components/edit-api-key-form/edit-api-key-form.tsx
import { zodResolver } from "@hookform/resolvers/zod";
import { Button, Input, toast } from "@medusajs/ui";
import { useForm } from "react-hook-form";
import { useTranslation } from "react-i18next";
import * as zod from "zod";
import { jsx, jsxs } from "react/jsx-runtime";
var EditApiKeySchema = zod.object({
  title: zod.string().min(1)
});
var EditApiKeyForm = ({ apiKey }) => {
  const { t } = useTranslation();
  const { handleSuccess } = useRouteModal();
  const form = useForm({
    defaultValues: {
      title: apiKey.title
    },
    resolver: zodResolver(EditApiKeySchema)
  });
  const { mutateAsync, isPending } = useUpdateApiKey(apiKey.id);
  const handleSubmit = form.handleSubmit(async (data) => {
    await mutateAsync(data, {
      onSuccess: ({ api_key }) => {
        toast.success(
          t("apiKeyManagement.edit.successToast", {
            title: api_key.title
          })
        );
        handleSuccess();
      },
      onError: (err) => {
        toast.error(err.message);
      }
    });
  });
  return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, "data-testid": `${apiKey.type}-api-key-edit-form`, children: /* @__PURE__ */ jsxs(KeyboundForm, { onSubmit: handleSubmit, className: "flex flex-1 flex-col", children: [
    /* @__PURE__ */ jsx(RouteDrawer.Body, { "data-testid": `${apiKey.type}-api-key-edit-body`, children: /* @__PURE__ */ jsx("div", { className: "flex flex-col gap-y-4", children: /* @__PURE__ */ jsx(
      Form.Field,
      {
        control: form.control,
        name: "title",
        render: ({ field }) => {
          return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": `${apiKey.type}-api-key-edit-title-item`, children: [
            /* @__PURE__ */ jsx(Form.Label, { "data-testid": `${apiKey.type}-api-key-edit-title-label`, children: t("fields.title") }),
            /* @__PURE__ */ jsx(Form.Control, { "data-testid": `${apiKey.type}-api-key-edit-title-control`, children: /* @__PURE__ */ jsx(Input, { ...field, "data-testid": `${apiKey.type}-api-key-edit-title-input` }) }),
            /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": `${apiKey.type}-api-key-edit-title-error` })
          ] });
        }
      }
    ) }) }),
    /* @__PURE__ */ jsx(RouteDrawer.Footer, { "data-testid": `${apiKey.type}-api-key-edit-footer`, children: /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-2", children: [
      /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", "data-testid": `${apiKey.type}-api-key-edit-cancel-button`, children: t("actions.cancel") }) }),
      /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, "data-testid": `${apiKey.type}-api-key-edit-save-button`, children: t("actions.save") })
    ] }) })
  ] }) });
};

// src/pages/api-key-management/api-key-management-edit/api-key-management-edit.tsx
import { jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
var ApiKeyManagementEdit = () => {
  const { id } = useParams();
  const { t } = useTranslation2();
  const { api_key, isLoading, isError, error } = useApiKey(id);
  if (isError) {
    throw error;
  }
  return /* @__PURE__ */ jsxs2(RouteDrawer, { "data-testid": `${api_key?.type || "api"}-api-key-edit-drawer`, children: [
    /* @__PURE__ */ jsxs2(RouteDrawer.Header, { "data-testid": `${api_key?.type || "api"}-api-key-edit-drawer-header`, children: [
      /* @__PURE__ */ jsx2(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx2(Heading, { "data-testid": `${api_key?.type || "api"}-api-key-edit-drawer-title`, children: t("apiKeyManagement.edit.header") }) }),
      /* @__PURE__ */ jsx2(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx2(VisuallyHidden, { "data-testid": `${api_key?.type || "api"}-api-key-edit-drawer-description`, children: t("apiKeyManagement.edit.description") }) })
    ] }),
    !isLoading && !!api_key && /* @__PURE__ */ jsx2(EditApiKeyForm, { apiKey: api_key })
  ] });
};
export {
  ApiKeyManagementEdit as Component
};
