import {
  useComboboxData
} from "./chunk-AEFDAU3Y.js";
import {
  Combobox
} from "./chunk-AW77UWCZ.js";
import "./chunk-TKGWSUEI.js";
import {
  KeyboundForm
} from "./chunk-U2ZFCAPX.js";
import {
  useDocumentDirection
} from "./chunk-Y7QKP6QU.js";
import {
  RouteDrawer,
  useRouteModal
} from "./chunk-OV7R2AH5.js";
import {
  Form
} from "./chunk-3QSRE5LS.js";
import {
  useStore,
  useUpdateStore
} from "./chunk-LC6TM5OT.js";
import "./chunk-7SJC6BWZ.js";
import "./chunk-RHKRREUU.js";
import "./chunk-ZA2KFUFR.js";
import {
  sdk
} from "./chunk-RIN4CBRB.js";
import "./chunk-NBMM2TZK.js";

// src/pages/marketplace/marketplace-edit/marketplace-edit.tsx
import { Heading } from "@medusajs/ui";
import { useTranslation as useTranslation2 } from "react-i18next";

// src/pages/marketplace/marketplace-edit/components/edit-marketplace-form/edit-marketplace-form.tsx
import { zodResolver } from "@hookform/resolvers/zod";
import { Button, Input, Select, 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 EditStoreSchema = z.object({
  name: z.string().min(1),
  default_currency_code: z.string().optional(),
  default_region_id: z.string().optional(),
  default_sales_channel_id: z.string().optional(),
  default_location_id: z.string().optional()
});
var EditMarketplaceForm = ({ store }) => {
  const { t } = useTranslation();
  const { handleSuccess } = useRouteModal();
  const direction = useDocumentDirection();
  const form = useForm({
    defaultValues: {
      name: store.name,
      default_region_id: store.default_region_id || void 0,
      default_currency_code: store.supported_currencies?.find((c) => c.is_default)?.currency_code || void 0,
      default_sales_channel_id: store.default_sales_channel_id || void 0,
      default_location_id: store.default_location_id || void 0
    },
    resolver: zodResolver(EditStoreSchema)
  });
  const { mutateAsync, isPending } = useUpdateStore(store.id);
  const regionsCombobox = useComboboxData({
    queryKey: ["regions", "default_region_id"],
    queryFn: (params) => sdk.admin.regions.query({ ...params, fields: "id,name" }),
    defaultValue: store.default_region_id || void 0,
    getOptions: (data) => data.regions.map((r) => ({ label: r.name, value: r.id }))
  });
  const salesChannelsCombobox = useComboboxData({
    queryFn: (params) => sdk.admin.salesChannels.query({ ...params, fields: "id,name" }),
    getOptions: (data) => data.sales_channels.map((sc) => ({ label: sc.name, value: sc.id })),
    queryKey: ["sales_channels", "default_sales_channel_id"],
    defaultValue: store.default_sales_channel_id || void 0
  });
  const locationsCombobox = useComboboxData({
    queryFn: (params) => sdk.admin.stockLocations.query({ ...params, fields: "id,name" }),
    getOptions: (data) => data.stock_locations.map((l) => ({ label: l.name, value: l.id })),
    queryKey: ["stock_locations", "default_location_id"],
    defaultValue: store.default_location_id || void 0
  });
  const handleSubmit = form.handleSubmit(async (values) => {
    const { default_currency_code, ...rest } = values;
    const normalizedMutation = {
      ...rest,
      supported_currencies: store.supported_currencies?.map((c) => ({
        ...c,
        is_default: c.currency_code === default_currency_code
      }))
    };
    await mutateAsync(normalizedMutation, {
      onSuccess: () => {
        toast.success(t("store.toast.update"));
        handleSuccess();
      },
      onError: (error) => {
        toast.error(error.message);
      }
    });
  });
  return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, "data-testid": "store-edit-form", children: /* @__PURE__ */ jsxs(KeyboundForm, { onSubmit: handleSubmit, className: "flex h-full flex-col overflow-hidden", children: [
    /* @__PURE__ */ jsx(RouteDrawer.Body, { className: "overflow-y-auto", "data-testid": "store-edit-form-body", children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-8", children: [
      /* @__PURE__ */ jsx(
        Form.Field,
        {
          control: form.control,
          name: "name",
          render: ({ field }) => /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "store-edit-form-name-item", children: [
            /* @__PURE__ */ jsx(Form.Label, { "data-testid": "store-edit-form-name-label", children: t("fields.name") }),
            /* @__PURE__ */ jsx(Form.Control, { "data-testid": "store-edit-form-name-control", children: /* @__PURE__ */ jsx(Input, { placeholder: "ACME", ...field, "data-testid": "store-edit-form-name-input" }) }),
            /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "store-edit-form-name-error" })
          ] })
        }
      ),
      /* @__PURE__ */ jsx(
        Form.Field,
        {
          control: form.control,
          name: "default_currency_code",
          render: ({ field: { onChange, ...field } }) => {
            return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "store-edit-form-currency-item", children: [
              /* @__PURE__ */ jsx(Form.Label, { "data-testid": "store-edit-form-currency-label", children: t("store.defaultCurrency") }),
              /* @__PURE__ */ jsx(Form.Control, { "data-testid": "store-edit-form-currency-control", children: /* @__PURE__ */ jsxs(
                Select,
                {
                  dir: direction,
                  ...field,
                  onValueChange: onChange,
                  "data-testid": "store-edit-form-currency-select",
                  children: [
                    /* @__PURE__ */ jsx(Select.Trigger, { ref: field.ref, "data-testid": "store-edit-form-currency-select-trigger", children: /* @__PURE__ */ jsx(Select.Value, {}) }),
                    /* @__PURE__ */ jsx(Select.Content, { "data-testid": "store-edit-form-currency-select-content", children: store.supported_currencies?.map((currency) => /* @__PURE__ */ jsx(
                      Select.Item,
                      {
                        value: currency.currency_code,
                        "data-testid": `store-edit-form-currency-select-option-${currency.currency_code}`,
                        children: currency.currency_code.toUpperCase()
                      },
                      currency.currency_code
                    )) })
                  ]
                }
              ) }),
              /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "store-edit-form-currency-error" })
            ] });
          }
        }
      ),
      /* @__PURE__ */ jsx(
        Form.Field,
        {
          control: form.control,
          name: "default_region_id",
          render: ({ field }) => {
            return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "store-edit-form-region-item", children: [
              /* @__PURE__ */ jsx(Form.Label, { "data-testid": "store-edit-form-region-label", children: t("store.defaultRegion") }),
              /* @__PURE__ */ jsx(Form.Control, { "data-testid": "store-edit-form-region-control", children: /* @__PURE__ */ jsx(
                Combobox,
                {
                  ...field,
                  options: regionsCombobox.options,
                  searchValue: regionsCombobox.searchValue,
                  onSearchValueChange: regionsCombobox.onSearchValueChange,
                  disabled: regionsCombobox.disabled,
                  "data-testid": "store-edit-form-region-combobox"
                }
              ) }),
              /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "store-edit-form-region-error" })
            ] });
          }
        }
      ),
      /* @__PURE__ */ jsx(
        Form.Field,
        {
          control: form.control,
          name: "default_sales_channel_id",
          render: ({ field }) => {
            return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "store-edit-form-sales-channel-item", children: [
              /* @__PURE__ */ jsx(Form.Label, { "data-testid": "store-edit-form-sales-channel-label", children: t("store.defaultSalesChannel") }),
              /* @__PURE__ */ jsx(Form.Control, { "data-testid": "store-edit-form-sales-channel-control", children: /* @__PURE__ */ jsx(
                Combobox,
                {
                  ...field,
                  options: salesChannelsCombobox.options,
                  searchValue: salesChannelsCombobox.searchValue,
                  onSearchValueChange: salesChannelsCombobox.onSearchValueChange,
                  disabled: salesChannelsCombobox.disabled,
                  "data-testid": "store-edit-form-sales-channel-combobox"
                }
              ) }),
              /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "store-edit-form-sales-channel-error" })
            ] });
          }
        }
      ),
      /* @__PURE__ */ jsx(
        Form.Field,
        {
          control: form.control,
          name: "default_location_id",
          render: ({ field }) => {
            return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "store-edit-form-location-item", children: [
              /* @__PURE__ */ jsx(Form.Label, { "data-testid": "store-edit-form-location-label", children: t("store.defaultLocation") }),
              /* @__PURE__ */ jsx(Form.Control, { "data-testid": "store-edit-form-location-control", children: /* @__PURE__ */ jsx(
                Combobox,
                {
                  ...field,
                  options: locationsCombobox.options,
                  searchValue: locationsCombobox.searchValue,
                  onSearchValueChange: locationsCombobox.onSearchValueChange,
                  disabled: locationsCombobox.disabled,
                  "data-testid": "store-edit-form-location-combobox"
                }
              ) }),
              /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "store-edit-form-location-error" })
            ] });
          }
        }
      )
    ] }) }),
    /* @__PURE__ */ jsx(RouteDrawer.Footer, { "data-testid": "store-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": "store-edit-form-cancel-button", children: t("actions.cancel") }) }),
      /* @__PURE__ */ jsx(Button, { size: "small", isLoading: isPending, type: "submit", "data-testid": "store-edit-form-save-button", children: t("actions.save") })
    ] }) })
  ] }) });
};

// src/pages/marketplace/marketplace-edit/marketplace-edit.tsx
import { jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
var MarketplaceEdit = () => {
  const { t } = useTranslation2();
  const { store, isPending: isLoading, isError, error } = useStore();
  if (isError) {
    throw error;
  }
  const ready = !!store && !isLoading;
  return /* @__PURE__ */ jsxs2(RouteDrawer, { "data-testid": "store-edit", children: [
    /* @__PURE__ */ jsx2(RouteDrawer.Header, { "data-testid": "store-edit-header", children: /* @__PURE__ */ jsx2(Heading, { "data-testid": "store-edit-heading", children: t("marketplace.edit.header") }) }),
    ready && /* @__PURE__ */ jsx2(EditMarketplaceForm, { store })
  ] });
};
export {
  MarketplaceEdit as Component
};
