import {
  getShippingProfileName,
  isOptionEnabledInStore
} from "./chunk-E3MNEQID.js";
import {
  ShippingOptionPriceType
} from "./chunk-BCU3FKP2.js";
import {
  useComboboxData
} from "./chunk-OVPAOBZB.js";
import {
  Combobox
} from "./chunk-VBRTC2VU.js";
import "./chunk-TKGWSUEI.js";
import {
  KeyboundForm
} from "./chunk-SITRHMZZ.js";
import {
  RouteDrawer,
  useRouteModal
} from "./chunk-GDWBOQI5.js";
import {
  Form
} from "./chunk-3QSRE5LS.js";
import {
  useShippingOptions,
  useUpdateShippingOptions
} from "./chunk-VWPNMEOE.js";
import "./chunk-O72AIZUN.js";
import "./chunk-RHKRREUU.js";
import "./chunk-ZA2KFUFR.js";
import {
  fetchQuery
} from "./chunk-DTGEFKK4.js";
import "./chunk-NBMM2TZK.js";

// src/pages/settings/locations/[location_id]/fulfillment-set/[fset_id]/service-zone/[zone_id]/shipping-option/[so_id]/edit/index.tsx
import { Heading } from "@medusajs/ui";
import { useTranslation as useTranslation2 } from "react-i18next";
import { useParams } from "react-router-dom";

// src/pages/settings/locations/[location_id]/fulfillment-set/[fset_id]/service-zone/[zone_id]/shipping-option/[so_id]/edit/_components/edit-shipping-option-form.tsx
import { zodResolver } from "@hookform/resolvers/zod";
import { Button, Input, RadioGroup, 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 EditShippingOptionSchema = zod.object({
  name: zod.string().min(1),
  price_type: zod.nativeEnum(ShippingOptionPriceType),
  enabled_in_store: zod.boolean().optional(),
  shipping_profile_id: zod.string()
});
var EditShippingOptionForm = ({
  locationId,
  shippingOption,
  type
}) => {
  const { t } = useTranslation();
  const { handleSuccess } = useRouteModal();
  const isPickup = type === "pickup" /* Pickup */;
  const shippingProfiles = useComboboxData({
    queryFn: async () => {
      const { shipping_profiles } = await fetchQuery(
        "/vendor/shipping-profiles",
        {
          method: "GET"
        }
      );
      return shipping_profiles;
    },
    queryKey: ["shipping_profiles_edit_shipping_option"],
    getOptions: (data) => data?.map((profile) => ({
      label: getShippingProfileName(profile.shipping_profile?.name ?? profile.name ?? ""),
      value: profile.shipping_profile?.id ?? profile.id
    })),
    defaultValue: shippingOption.shipping_profile_id
  });
  const form = useForm({
    defaultValues: {
      name: shippingOption.name,
      price_type: shippingOption.price_type,
      enabled_in_store: isOptionEnabledInStore(shippingOption),
      shipping_profile_id: shippingOption.shipping_profile_id
    },
    resolver: zodResolver(EditShippingOptionSchema)
  });
  const { mutateAsync, isPending: isLoading } = useUpdateShippingOptions(
    shippingOption.id
  );
  const handleSubmit = form.handleSubmit(async (values) => {
    await mutateAsync(
      {
        name: values.name,
        shipping_profile_id: values.shipping_profile_id,
        provider_id: shippingOption.provider_id
      },
      {
        onSuccess: ({ shipping_option }) => {
          toast.success(
            t("stockLocations.shippingOptions.edit.successToast", {
              name: shipping_option.name
            })
          );
          handleSuccess(`/settings/locations/${locationId}`);
        },
        onError: (e) => {
          toast.error(e.message);
        }
      }
    );
  });
  return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(KeyboundForm, { onSubmit: handleSubmit, className: "flex flex-1 flex-col", children: [
    /* @__PURE__ */ jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsx("div", { className: "flex flex-col gap-y-8", children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-8", children: [
      !isPickup && /* @__PURE__ */ jsx(
        Form.Field,
        {
          control: form.control,
          name: "price_type",
          render: ({ field }) => {
            return /* @__PURE__ */ jsxs(Form.Item, { children: [
              /* @__PURE__ */ jsx(Form.Label, { children: t(
                "stockLocations.shippingOptions.fields.priceType.label"
              ) }),
              /* @__PURE__ */ jsx(Form.Control, { children: /* @__PURE__ */ jsxs(RadioGroup, { ...field, onValueChange: field.onChange, children: [
                /* @__PURE__ */ jsx(
                  RadioGroup.ChoiceBox,
                  {
                    className: "flex-1",
                    value: "flat" /* FlatRate */,
                    label: t(
                      "stockLocations.shippingOptions.fields.priceType.options.fixed.label"
                    ),
                    description: t(
                      "stockLocations.shippingOptions.fields.priceType.options.fixed.hint"
                    )
                  }
                ),
                /* @__PURE__ */ jsx(
                  RadioGroup.ChoiceBox,
                  {
                    className: "flex-1",
                    value: "calculated" /* Calculated */,
                    label: t(
                      "stockLocations.shippingOptions.fields.priceType.options.calculated.label"
                    ),
                    description: t(
                      "stockLocations.shippingOptions.fields.priceType.options.calculated.hint"
                    )
                  }
                )
              ] }) }),
              /* @__PURE__ */ jsx(Form.ErrorMessage, {})
            ] });
          }
        }
      ),
      /* @__PURE__ */ jsxs("div", { className: "grid gap-y-4", children: [
        /* @__PURE__ */ jsx(
          Form.Field,
          {
            control: form.control,
            name: "name",
            render: ({ field }) => {
              return /* @__PURE__ */ jsxs(Form.Item, { children: [
                /* @__PURE__ */ jsx(Form.Label, { children: t("fields.name") }),
                /* @__PURE__ */ jsx(Form.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
                /* @__PURE__ */ jsx(Form.ErrorMessage, {})
              ] });
            }
          }
        ),
        /* @__PURE__ */ jsx(
          Form.Field,
          {
            control: form.control,
            name: "shipping_profile_id",
            render: ({ field }) => {
              return /* @__PURE__ */ jsxs(Form.Item, { children: [
                /* @__PURE__ */ jsx(Form.Label, { children: t("stockLocations.shippingOptions.fields.profile") }),
                /* @__PURE__ */ jsx(Form.Control, { children: /* @__PURE__ */ jsx(
                  Combobox,
                  {
                    ...field,
                    options: shippingProfiles.options,
                    searchValue: shippingProfiles.searchValue,
                    onSearchValueChange: shippingProfiles.onSearchValueChange,
                    disabled: shippingProfiles.disabled
                  }
                ) }),
                /* @__PURE__ */ jsx(Form.ErrorMessage, {})
              ] });
            }
          }
        )
      ] })
    ] }) }) }),
    /* @__PURE__ */ jsx(RouteDrawer.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", children: t("actions.cancel") }) }),
      /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading, children: t("actions.save") })
    ] }) })
  ] }) });
};

// src/pages/settings/locations/[location_id]/fulfillment-set/[fset_id]/service-zone/[zone_id]/shipping-option/[so_id]/edit/index.tsx
import { jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
var LocationServiceZoneShippingOptionEdit = () => {
  const { t } = useTranslation2();
  const { location_id, so_id } = useParams();
  const { shipping_options, isPending, isFetching, isError, error } = useShippingOptions({
    fields: "+service_zone.fulfillment_set.type"
  });
  const shippingOption = shipping_options?.find((so) => so?.id === so_id);
  if (!isPending && !isFetching && !shippingOption) {
    throw new Response(
      JSON.stringify({
        message: `Shipping option with ID ${so_id} was not found`
      }),
      { status: 404, headers: { "Content-Type": "application/json" } }
    );
  }
  if (isError) {
    throw error;
  }
  const isPickup = shippingOption?.service_zone.fulfillment_set.type === "pickup" /* Pickup */;
  return /* @__PURE__ */ jsxs2(RouteDrawer, { children: [
    /* @__PURE__ */ jsx2(RouteDrawer.Header, { children: /* @__PURE__ */ jsx2(Heading, { children: t(
      `stockLocations.${isPickup ? "pickupOptions" : "shippingOptions"}.edit.header`
    ) }) }),
    shippingOption && /* @__PURE__ */ jsx2(
      EditShippingOptionForm,
      {
        shippingOption,
        locationId: location_id,
        type: shippingOption.service_zone.fulfillment_set.type
      }
    )
  ] });
};
var Component = LocationServiceZoneShippingOptionEdit;
export {
  Component
};
