import {
  CountrySelect
} from "./chunk-V74Y5NIV.js";
import "./chunk-JRTZWK77.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 {
  useSeller,
  useUpdateSellerPaymentDetails
} from "./chunk-LEG67A2R.js";
import "./chunk-RHKRREUU.js";
import "./chunk-ZA2KFUFR.js";
import "./chunk-ZBWL72TB.js";
import "./chunk-NBMM2TZK.js";

// src/pages/stores/store-payment-details-edit/store-payment-details-edit.tsx
import { Heading } from "@medusajs/ui";
import { useTranslation as useTranslation2 } from "react-i18next";
import { useParams } from "react-router-dom";

// src/pages/stores/store-payment-details-edit/components/store-payment-details-form.tsx
import { zodResolver } from "@hookform/resolvers/zod";
import i18n from "i18next";
import { Button, Input, toast } from "@medusajs/ui";
import { useForm } from "react-hook-form";
import { useTranslation } from "react-i18next";
import * as zod from "zod";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
var StorePaymentDetailsSchema = zod.object({
  country_code: zod.string().min(1, {
    message: i18n.t("store.paymentDetails.validation.countryRequired")
  }),
  holder_name: zod.string().min(1, {
    message: i18n.t("store.paymentDetails.validation.accountNameRequired")
  }),
  iban: zod.string().optional().or(zod.literal("")),
  bic: zod.string().optional().or(zod.literal("")),
  routing_number: zod.string().optional().or(zod.literal("")),
  account_number: zod.string().optional().or(zod.literal(""))
});
var StorePaymentDetailsForm = ({
  seller
}) => {
  const { t } = useTranslation();
  const { handleSuccess } = useRouteModal();
  const details = seller.payment_details;
  const form = useForm({
    defaultValues: {
      country_code: details?.country_code ?? "",
      holder_name: details?.holder_name ?? "",
      iban: details?.iban ?? "",
      bic: details?.bic ?? "",
      routing_number: details?.routing_number ?? "",
      account_number: details?.account_number ?? ""
    },
    resolver: zodResolver(StorePaymentDetailsSchema)
  });
  const selectedCountry = form.watch("country_code");
  const { mutateAsync, isPending } = useUpdateSellerPaymentDetails(seller.id);
  const handleSubmit = form.handleSubmit(async (values) => {
    const isABA = values.country_code === "us";
    await mutateAsync(
      {
        country_code: values.country_code,
        holder_name: values.holder_name,
        iban: isABA ? null : values.iban || null,
        bic: isABA ? null : values.bic || null,
        routing_number: isABA ? values.routing_number || null : null,
        account_number: values.account_number || null
      },
      {
        onSuccess: () => {
          toast.success(t("store.paymentDetails.edit.successToast"));
          handleSuccess();
        },
        onError: (error) => {
          toast.error(error.message);
        }
      }
    );
  });
  return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
    KeyboundForm,
    {
      onSubmit: handleSubmit,
      className: "flex flex-1 flex-col overflow-hidden",
      children: [
        /* @__PURE__ */ jsxs(RouteDrawer.Body, { className: "flex flex-col gap-y-4 overflow-y-auto", children: [
          /* @__PURE__ */ jsx(
            Form.Field,
            {
              control: form.control,
              name: "country_code",
              render: ({ field: { onChange, ref: _ref, ...field } }) => /* @__PURE__ */ jsxs(Form.Item, { children: [
                /* @__PURE__ */ jsx(Form.Label, { children: t("store.paymentDetails.fields.countryCode") }),
                /* @__PURE__ */ jsx(Form.Control, { children: /* @__PURE__ */ jsx(CountrySelect, { ...field, onChange }) }),
                /* @__PURE__ */ jsx(Form.ErrorMessage, {})
              ] })
            }
          ),
          /* @__PURE__ */ jsx(
            Form.Field,
            {
              control: form.control,
              name: "holder_name",
              render: ({ field }) => /* @__PURE__ */ jsxs(Form.Item, { children: [
                /* @__PURE__ */ jsx(Form.Label, { children: t("store.paymentDetails.fields.holderName") }),
                /* @__PURE__ */ jsx(Form.Control, { children: /* @__PURE__ */ jsx(Input, { size: "small", ...field }) }),
                /* @__PURE__ */ jsx(Form.ErrorMessage, {})
              ] })
            }
          ),
          selectedCountry === "us" ? /* @__PURE__ */ jsxs(Fragment, { children: [
            /* @__PURE__ */ jsx(
              Form.Field,
              {
                control: form.control,
                name: "account_number",
                render: ({ field }) => /* @__PURE__ */ jsxs(Form.Item, { children: [
                  /* @__PURE__ */ jsx(Form.Label, { optional: true, children: t("store.paymentDetails.fields.accountNumber") }),
                  /* @__PURE__ */ jsx(Form.Control, { children: /* @__PURE__ */ jsx(Input, { size: "small", ...field }) }),
                  /* @__PURE__ */ jsx(Form.ErrorMessage, {})
                ] })
              }
            ),
            /* @__PURE__ */ jsx(
              Form.Field,
              {
                control: form.control,
                name: "routing_number",
                render: ({ field }) => /* @__PURE__ */ jsxs(Form.Item, { children: [
                  /* @__PURE__ */ jsx(Form.Label, { optional: true, children: t("store.paymentDetails.fields.achRoutingNumber") }),
                  /* @__PURE__ */ jsx(Form.Control, { children: /* @__PURE__ */ jsx(Input, { size: "small", ...field }) }),
                  /* @__PURE__ */ jsx(Form.ErrorMessage, {})
                ] })
              }
            )
          ] }) : /* @__PURE__ */ jsxs(Fragment, { children: [
            /* @__PURE__ */ jsx(
              Form.Field,
              {
                control: form.control,
                name: "iban",
                render: ({ field }) => /* @__PURE__ */ jsxs(Form.Item, { children: [
                  /* @__PURE__ */ jsx(Form.Label, { optional: true, children: t("store.paymentDetails.fields.iban") }),
                  /* @__PURE__ */ jsx(Form.Control, { children: /* @__PURE__ */ jsx(Input, { size: "small", ...field }) }),
                  /* @__PURE__ */ jsx(Form.ErrorMessage, {})
                ] })
              }
            ),
            /* @__PURE__ */ jsx(
              Form.Field,
              {
                control: form.control,
                name: "account_number",
                render: ({ field }) => /* @__PURE__ */ jsxs(Form.Item, { children: [
                  /* @__PURE__ */ jsx(Form.Label, { optional: true, children: t("store.paymentDetails.fields.accountNumber") }),
                  /* @__PURE__ */ jsx(Form.Control, { children: /* @__PURE__ */ jsx(Input, { size: "small", ...field }) }),
                  /* @__PURE__ */ jsx(Form.ErrorMessage, {})
                ] })
              }
            ),
            /* @__PURE__ */ jsx(
              Form.Field,
              {
                control: form.control,
                name: "bic",
                render: ({ field }) => /* @__PURE__ */ jsxs(Form.Item, { children: [
                  /* @__PURE__ */ jsx(Form.Label, { optional: true, children: t("store.paymentDetails.fields.swiftBic") }),
                  /* @__PURE__ */ jsx(Form.Control, { children: /* @__PURE__ */ jsx(Input, { size: "small", ...field }) }),
                  /* @__PURE__ */ jsx(Form.ErrorMessage, {})
                ] })
              }
            )
          ] })
        ] }),
        /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
          /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { variant: "secondary", size: "small", children: t("actions.cancel") }) }),
          /* @__PURE__ */ jsx(Button, { type: "submit", size: "small", isLoading: isPending, children: t("actions.save") })
        ] }) })
      ]
    }
  ) });
};

// src/pages/stores/store-payment-details-edit/store-payment-details-edit.tsx
import { jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
var StorePaymentDetailsEdit = () => {
  const { id } = useParams();
  const { t } = useTranslation2();
  const { seller, isLoading, isError, error } = useSeller(id);
  if (isError) {
    throw error;
  }
  return /* @__PURE__ */ jsxs2(RouteDrawer, { children: [
    /* @__PURE__ */ jsxs2(RouteDrawer.Header, { children: [
      /* @__PURE__ */ jsx2(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx2(Heading, { children: t("store.paymentDetails.edit.header") }) }),
      /* @__PURE__ */ jsx2(RouteDrawer.Description, { className: "sr-only", children: t("store.paymentDetails.edit.description") })
    ] }),
    !isLoading && seller && /* @__PURE__ */ jsx2(StorePaymentDetailsForm, { seller })
  ] });
};
export {
  StorePaymentDetailsEdit as Component
};
