import {
  formatProvider
} from "./chunk-VW56KWO2.js";
import {
  useComboboxData
} from "./chunk-AEFDAU3Y.js";
import {
  Combobox
} from "./chunk-AW77UWCZ.js";
import "./chunk-TKGWSUEI.js";
import {
  KeyboundForm
} from "./chunk-U2ZFCAPX.js";
import {
  RouteDrawer,
  useRouteModal
} from "./chunk-OV7R2AH5.js";
import {
  Form
} from "./chunk-3QSRE5LS.js";
import "./chunk-3MDRW5MA.js";
import "./chunk-MFL2TZ3W.js";
import "./chunk-YNIIYGL4.js";
import "./chunk-CBHNHLUZ.js";
import {
  useTaxRegion,
  useUpdateTaxRegion
} from "./chunk-VME3CJER.js";
import "./chunk-XMVTX2EN.js";
import "./chunk-5PUHROK4.js";
import "./chunk-TPPONKYW.js";
import "./chunk-DF3OGJAK.js";
import "./chunk-XRDP2SYY.js";
import "./chunk-FNBKEF54.js";
import "./chunk-EHQESDH2.js";
import "./chunk-XKVXLDSJ.js";
import "./chunk-XDZGOUCR.js";
import "./chunk-K4BGK3CO.js";
import "./chunk-YYTI5JIC.js";
import "./chunk-HAFZQK53.js";
import "./chunk-IJWG755C.js";
import "./chunk-M3YP3NX4.js";
import "./chunk-UU2RQMXS.js";
import "./chunk-O4STPJR2.js";
import "./chunk-R74OHYTB.js";
import "./chunk-22XJUSMM.js";
import "./chunk-OFKUNYQJ.js";
import "./chunk-W5UPPL3W.js";
import "./chunk-LC6TM5OT.js";
import "./chunk-7SJC6BWZ.js";
import "./chunk-N4JU7GWK.js";
import "./chunk-52BRBQMY.js";
import "./chunk-BA2HXHTW.js";
import "./chunk-RHKRREUU.js";
import "./chunk-ZA2KFUFR.js";
import {
  sdk
} from "./chunk-RIN4CBRB.js";
import "./chunk-NBMM2TZK.js";

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

// src/pages/tax-regions/tax-region-edit/components/tax-region-edit/tax-region-edit-form.tsx
import { zodResolver } from "@hookform/resolvers/zod";
import { Button, toast } from "@medusajs/ui";
import { useForm } from "react-hook-form";
import { useTranslation } from "react-i18next";
import { z } from "zod";
import { jsx, jsxs } from "react/jsx-runtime";
var TaxRegionEditSchema = z.object({
  provider_id: z.string().min(1)
});
var TaxRegionEditForm = ({ taxRegion }) => {
  const { t } = useTranslation();
  const { handleSuccess } = useRouteModal();
  const taxProviders = useComboboxData({
    queryKey: ["tax_providers"],
    queryFn: (params) => sdk.admin.taxProviders.query(params),
    getOptions: (data) => data.tax_providers.map((provider) => ({
      label: formatProvider(provider.id),
      value: provider.id
    }))
  });
  const form = useForm({
    defaultValues: {
      provider_id: taxRegion.provider_id ?? void 0
    },
    resolver: zodResolver(TaxRegionEditSchema)
  });
  const { mutateAsync, isPending } = useUpdateTaxRegion(taxRegion.id);
  const handleSubmit = form.handleSubmit(async (values) => {
    await mutateAsync(
      {
        provider_id: values.provider_id
      },
      {
        onSuccess: () => {
          toast.success(t("taxRegions.edit.successToast"));
          handleSuccess();
        },
        onError: (error) => {
          toast.error(error.message);
        }
      }
    );
  });
  return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, "data-testid": "tax-region-edit-form", children: /* @__PURE__ */ jsxs(
    KeyboundForm,
    {
      className: "flex flex-1 flex-col overflow-hidden",
      onSubmit: handleSubmit,
      children: [
        /* @__PURE__ */ jsx(RouteDrawer.Body, { className: "flex flex-1 flex-col gap-y-6 overflow-auto", "data-testid": "tax-region-edit-form-body", children: /* @__PURE__ */ jsx("div", { className: "flex flex-col gap-y-4", children: /* @__PURE__ */ jsx(
          Form.Field,
          {
            control: form.control,
            name: "provider_id",
            render: ({ field }) => /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "tax-region-edit-form-provider-item", children: [
              /* @__PURE__ */ jsx(Form.Label, { "data-testid": "tax-region-edit-form-provider-label", children: t("taxRegions.fields.taxProvider") }),
              /* @__PURE__ */ jsx(Form.Control, { "data-testid": "tax-region-edit-form-provider-control", children: /* @__PURE__ */ jsx(
                Combobox,
                {
                  ...field,
                  options: taxProviders.options,
                  searchValue: taxProviders.searchValue,
                  onSearchValueChange: taxProviders.onSearchValueChange,
                  fetchNextPage: taxProviders.fetchNextPage,
                  "data-testid": "tax-region-edit-form-provider-combobox"
                }
              ) }),
              /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "tax-region-edit-form-provider-error" })
            ] })
          }
        ) }) }),
        /* @__PURE__ */ jsx(RouteDrawer.Footer, { className: "shrink-0", "data-testid": "tax-region-edit-form-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, { size: "small", variant: "secondary", "data-testid": "tax-region-edit-form-cancel-button", children: t("actions.cancel") }) }),
          /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, "data-testid": "tax-region-edit-form-save-button", children: t("actions.save") })
        ] }) })
      ]
    }
  ) });
};

// src/pages/tax-regions/tax-region-edit/tax-region-edit.tsx
import { jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
var TaxRegionEdit = () => {
  const { t } = useTranslation2();
  const { id } = useParams();
  const { tax_region, isPending, isError, error } = useTaxRegion(id);
  const ready = !isPending && !!tax_region;
  if (isError) {
    throw error;
  }
  return /* @__PURE__ */ jsxs2(RouteDrawer, { "data-testid": "tax-region-edit-drawer", children: [
    /* @__PURE__ */ jsxs2(RouteDrawer.Header, { "data-testid": "tax-region-edit-drawer-header", children: [
      /* @__PURE__ */ jsx2(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx2(Heading, { "data-testid": "tax-region-edit-drawer-heading", children: t("taxRegions.edit.header") }) }),
      /* @__PURE__ */ jsx2(RouteDrawer.Description, { className: "sr-only", children: t("taxRegions.edit.hint") })
    ] }),
    ready && /* @__PURE__ */ jsx2(TaxRegionEditForm, { taxRegion: tax_region })
  ] });
};
export {
  TaxRegionEdit as Component
};
