import {
  PercentageInput
} from "./chunk-MAEP2UCK.js";
import {
  formatProvider
} from "./chunk-VW56KWO2.js";
import "./chunk-NXDFDEKP.js";
import {
  CountrySelect
} from "./chunk-V74Y5NIV.js";
import "./chunk-FMFVTERD.js";
import "./chunk-JRTZWK77.js";
import {
  useComboboxData
} from "./chunk-AEFDAU3Y.js";
import {
  Combobox
} from "./chunk-AW77UWCZ.js";
import {
  i18n
} from "./chunk-VGM5UQRA.js";
import "./chunk-TKGWSUEI.js";
import {
  KeyboundForm
} from "./chunk-U2ZFCAPX.js";
import {
  RouteFocusModal,
  useRouteModal
} from "./chunk-FWJLS7XD.js";
import {
  Form
} from "./chunk-3QSRE5LS.js";
import "./chunk-PBQ5M44A.js";
import "./chunk-OH4AABUT.js";
import "./chunk-YKDEE7ZL.js";
import "./chunk-AEMDNTW5.js";
import {
  useCreateTaxRegion
} from "./chunk-LYD3C73Z.js";
import "./chunk-7O5NDICV.js";
import "./chunk-46M463BR.js";
import "./chunk-YFRC6GEN.js";
import "./chunk-N6SI4IIT.js";
import "./chunk-JPNPTILP.js";
import "./chunk-DAXXLQKB.js";
import "./chunk-OHOWEMHO.js";
import "./chunk-GKGLVDDV.js";
import "./chunk-WJJXUXSO.js";
import "./chunk-LAPEZATU.js";
import "./chunk-DBNPVU3L.js";
import "./chunk-U7WZYWGA.js";
import "./chunk-ZN7LWXVC.js";
import "./chunk-VYMODH6P.js";
import "./chunk-LEG67A2R.js";
import "./chunk-PRBE5CM3.js";
import "./chunk-5TRY5IMZ.js";
import "./chunk-TAKEQAZZ.js";
import "./chunk-B6FG7A6W.js";
import "./chunk-EPHVGD5Z.js";
import "./chunk-ESPLVOFC.js";
import "./chunk-FXWKGNRV.js";
import "./chunk-VDKOJ5WD.js";
import "./chunk-DL2ZIXBO.js";
import "./chunk-Z2QQZOG3.js";
import "./chunk-5CK6XEFV.js";
import "./chunk-RHKRREUU.js";
import "./chunk-ZA2KFUFR.js";
import {
  sdk
} from "./chunk-ZBWL72TB.js";
import "./chunk-NBMM2TZK.js";

// src/pages/tax-regions/tax-region-create/components/tax-region-create-form/tax-region-create-form.tsx
import { zodResolver } from "@hookform/resolvers/zod";
import { useForm } from "react-hook-form";
import { z } from "zod";
import { InformationCircleSolid } from "@medusajs/icons";
import { Button, Heading, Input, Text, toast, Tooltip } from "@medusajs/ui";
import { useTranslation } from "react-i18next";
import { jsx, jsxs } from "react/jsx-runtime";
var TaxRegionCreateSchema = z.object({
  name: z.string().optional(),
  code: z.string().optional(),
  rate: z.object({
    float: z.number().optional(),
    value: z.string().optional()
  }),
  country_code: z.string(),
  provider_id: z.string()
}).superRefine(({ provider_id, country_code }, ctx) => {
  if (!provider_id) {
    ctx.addIssue({
      code: z.ZodIssueCode.custom,
      message: i18n.t("taxRegions.create.errors.missingProvider"),
      path: ["provider_id"]
    });
  }
  if (!country_code) {
    ctx.addIssue({
      code: z.ZodIssueCode.custom,
      message: i18n.t("taxRegions.create.errors.missingCountry"),
      path: ["country_code"]
    });
  }
});
var TaxRegionCreateForm = ({ parentId }) => {
  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: {
      name: "",
      rate: {
        value: ""
      },
      code: "",
      country_code: "",
      provider_id: ""
    },
    resolver: zodResolver(TaxRegionCreateSchema)
  });
  const { mutateAsync, isPending } = useCreateTaxRegion();
  const handleSubmit = form.handleSubmit(async (values) => {
    const defaultRate = values.name ? {
      name: values.name,
      rate: values.rate?.value === "" ? void 0 : parseFloat(values.rate.value),
      // TODO: Check if optional region's tax code is correct - was it intentionally set as optional? It's same for vendor panel.
      // Leaving it like this, previously it was sent as empty string too if not provided
      code: values.code ?? ""
    } : void 0;
    await mutateAsync(
      {
        country_code: values.country_code,
        parent_id: parentId,
        default_tax_rate: defaultRate,
        provider_id: values.provider_id
      },
      {
        onSuccess: ({ tax_region }) => {
          toast.success(t("taxRegions.create.successToast"));
          handleSuccess(`../${tax_region.id}`);
        },
        onError: (error) => {
          toast.error(error.message);
        }
      }
    );
  });
  return /* @__PURE__ */ jsx(RouteFocusModal.Form, { form, "data-testid": "tax-region-create-form", children: /* @__PURE__ */ jsxs(
    KeyboundForm,
    {
      onSubmit: handleSubmit,
      className: "flex h-full flex-col overflow-hidden",
      children: [
        /* @__PURE__ */ jsx(RouteFocusModal.Header, { "data-testid": "tax-region-create-form-header" }),
        /* @__PURE__ */ jsx(RouteFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", "data-testid": "tax-region-create-form-body", children: /* @__PURE__ */ jsx("div", { className: "flex flex-1 flex-col items-center overflow-y-auto", children: /* @__PURE__ */ jsxs("div", { className: "flex w-full max-w-[720px] flex-col gap-y-8 px-2 py-16", children: [
          /* @__PURE__ */ jsxs("div", { "data-testid": "tax-region-create-form-header-section", children: [
            /* @__PURE__ */ jsx(Heading, { className: "capitalize", "data-testid": "tax-region-create-form-heading", children: t("taxRegions.create.header") }),
            /* @__PURE__ */ jsx(Text, { size: "small", className: "text-ui-fg-subtle", "data-testid": "tax-region-create-form-hint", children: t("taxRegions.create.hint") })
          ] }),
          /* @__PURE__ */ jsx("div", { className: "flex flex-col gap-y-4", children: /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-1 gap-4 md:grid-cols-2", children: [
            /* @__PURE__ */ jsx(
              Form.Field,
              {
                control: form.control,
                name: "country_code",
                render: ({ field }) => {
                  return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "tax-region-create-form-country-item", children: [
                    /* @__PURE__ */ jsx(Form.Label, { "data-testid": "tax-region-create-form-country-label", children: t("fields.country") }),
                    /* @__PURE__ */ jsx(Form.Control, { "data-testid": "tax-region-create-form-country-control", children: /* @__PURE__ */ jsx(CountrySelect, { ...field, "data-testid": "tax-region-create-form-country-select" }) }),
                    /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "tax-region-create-form-country-error" })
                  ] });
                }
              }
            ),
            /* @__PURE__ */ jsx(
              Form.Field,
              {
                control: form.control,
                name: "provider_id",
                render: ({ field }) => /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "tax-region-create-form-provider-item", children: [
                  /* @__PURE__ */ jsx(Form.Label, { "data-testid": "tax-region-create-form-provider-label", children: t("taxRegions.fields.taxProvider") }),
                  /* @__PURE__ */ jsx(Form.Control, { "data-testid": "tax-region-create-form-provider-control", children: /* @__PURE__ */ jsx(
                    Combobox,
                    {
                      ...field,
                      options: taxProviders.options,
                      searchValue: taxProviders.searchValue,
                      onSearchValueChange: taxProviders.onSearchValueChange,
                      fetchNextPage: taxProviders.fetchNextPage,
                      "data-testid": "tax-region-create-form-provider-combobox"
                    }
                  ) }),
                  /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "tax-region-create-form-provider-error" })
                ] })
              }
            )
          ] }) }),
          /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-4", "data-testid": "tax-region-create-form-default-tax-rate-section", children: [
            /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-1", "data-testid": "tax-region-create-form-default-tax-rate-header", children: [
              /* @__PURE__ */ jsx(Heading, { level: "h2", className: "!txt-compact-small-plus", "data-testid": "tax-region-create-form-default-tax-rate-label", children: t("taxRegions.fields.defaultTaxRate.label") }),
              /* @__PURE__ */ jsxs(
                Text,
                {
                  size: "small",
                  leading: "compact",
                  className: "text-ui-fg-muted",
                  "data-testid": "tax-region-create-form-default-tax-rate-optional",
                  children: [
                    "(",
                    t("fields.optional"),
                    ")"
                  ]
                }
              ),
              /* @__PURE__ */ jsx(
                Tooltip,
                {
                  content: t("taxRegions.fields.defaultTaxRate.tooltip"),
                  children: /* @__PURE__ */ jsx(InformationCircleSolid, { className: "text-ui-fg-muted", "data-testid": "tax-region-create-form-default-tax-rate-tooltip" })
                }
              )
            ] }),
            /* @__PURE__ */ jsx("div", { className: "flex flex-col gap-y-4", children: /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-1 gap-4 md:grid-cols-2", children: [
              /* @__PURE__ */ jsx(
                Form.Field,
                {
                  control: form.control,
                  name: "name",
                  render: ({ field }) => {
                    return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "tax-region-create-form-name-item", children: [
                      /* @__PURE__ */ jsx(Form.Label, { "data-testid": "tax-region-create-form-name-label", children: t("fields.name") }),
                      /* @__PURE__ */ jsx(Form.Control, { "data-testid": "tax-region-create-form-name-control", children: /* @__PURE__ */ jsx(Input, { ...field, "data-testid": "tax-region-create-form-name-input" }) }),
                      /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "tax-region-create-form-name-error" })
                    ] });
                  }
                }
              ),
              /* @__PURE__ */ jsx(
                Form.Field,
                {
                  control: form.control,
                  name: "rate",
                  render: ({ field: { value, onChange, ...field } }) => {
                    return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "tax-region-create-form-rate-item", children: [
                      /* @__PURE__ */ jsx(Form.Label, { "data-testid": "tax-region-create-form-rate-label", children: t("taxRegions.fields.taxRate") }),
                      /* @__PURE__ */ jsx(Form.Control, { "data-testid": "tax-region-create-form-rate-control", children: /* @__PURE__ */ jsx(
                        PercentageInput,
                        {
                          ...field,
                          value: value?.value,
                          decimalsLimit: 4,
                          onValueChange: (value2, _name, values) => onChange({
                            value: value2,
                            float: values?.float
                          }),
                          "data-testid": "tax-region-create-form-rate-input"
                        }
                      ) }),
                      /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "tax-region-create-form-rate-error" })
                    ] });
                  }
                }
              ),
              /* @__PURE__ */ jsx(
                Form.Field,
                {
                  control: form.control,
                  name: "code",
                  render: ({ field }) => {
                    return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "tax-region-create-form-code-item", children: [
                      /* @__PURE__ */ jsx(Form.Label, { "data-testid": "tax-region-create-form-code-label", children: t("taxRegions.fields.taxCode") }),
                      /* @__PURE__ */ jsx(Form.Control, { "data-testid": "tax-region-create-form-code-control", children: /* @__PURE__ */ jsx(Input, { ...field, "data-testid": "tax-region-create-form-code-input" }) }),
                      /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "tax-region-create-form-code-error" })
                    ] });
                  }
                }
              )
            ] }) })
          ] })
        ] }) }) }),
        /* @__PURE__ */ jsx(RouteFocusModal.Footer, { "data-testid": "tax-region-create-form-footer", children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
          /* @__PURE__ */ jsx(RouteFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", "data-testid": "tax-region-create-form-cancel-button", children: t("actions.cancel") }) }),
          /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, "data-testid": "tax-region-create-form-save-button", children: t("actions.save") })
        ] }) })
      ]
    }
  ) });
};

// src/pages/tax-regions/tax-region-create/tax-region-create.tsx
import { jsx as jsx2 } from "react/jsx-runtime";
var TaxRegionCreate = () => {
  return /* @__PURE__ */ jsx2(RouteFocusModal, { "data-testid": "tax-region-create-modal", children: /* @__PURE__ */ jsx2(TaxRegionCreateForm, {}) });
};
export {
  TaxRegionCreate as Component,
  TaxRegionCreate
};
