import {
  transformNullableFormData,
  transformNullableFormNumber,
  transformNullableFormNumbers
} from "./chunk-7RPRNVBS.js";
import {
  CountrySelect
} from "./chunk-V74Y5NIV.js";
import {
  DataGrid,
  createDataGridHelper
} from "./chunk-MWX5JCOM.js";
import {
  optionalInt
} from "./chunk-UXD3VNBO.js";
import {
  TabbedForm,
  defineTabMeta,
  useTabbedForm
} from "./chunk-4LXBQTFN.js";
import {
  SwitchBox
} from "./chunk-2EME4AWX.js";
import {
  RouteFocusModal,
  useRouteModal
} from "./chunk-FWJLS7XD.js";
import {
  Form
} from "./chunk-3QSRE5LS.js";
import {
  useStockLocations
} from "./chunk-EPHVGD5Z.js";
import {
  inventoryItemsQueryKeys,
  useCreateInventoryItem
} from "./chunk-5CK6XEFV.js";
import {
  queryClient
} from "./chunk-RHKRREUU.js";
import {
  sdk
} from "./chunk-ZBWL72TB.js";

// src/pages/inventory/inventory-create/components/inventory-create-form/schema.ts
import { z } from "zod";
var CreateInventoryItemSchema = z.object({
  title: z.string().min(1),
  description: z.string().optional(),
  sku: z.string().optional(),
  hs_code: z.string().optional(),
  weight: optionalInt,
  length: optionalInt,
  height: optionalInt,
  width: optionalInt,
  origin_country: z.string().optional(),
  mid_code: z.string().optional(),
  material: z.string().optional(),
  requires_shipping: z.boolean().optional(),
  thumbnail: z.string().optional(),
  locations: z.record(z.string(), optionalInt).optional()
});

// src/pages/inventory/inventory-create/inventory-create.tsx
import { Children as Children2 } from "react";

// src/pages/inventory/inventory-create/components/inventory-create-form/inventory-create-form.tsx
import { zodResolver } from "@hookform/resolvers/zod";
import { toast } from "@medusajs/ui";
import { Children, useMemo as useMemo2 } from "react";
import { useForm } from "react-hook-form";
import { useTranslation as useTranslation3 } from "react-i18next";

// src/pages/inventory/inventory-create/components/inventory-create-form/inventory-availability-form.tsx
import { useMemo } from "react";
import { useTranslation } from "react-i18next";
import { jsx } from "react/jsx-runtime";
var Root = ({ locations }) => {
  const form = useTabbedForm();
  const { setCloseOnEscape } = useRouteModal();
  const columns = useColumns();
  return /* @__PURE__ */ jsx("div", { className: "size-full", "data-testid": "inventory-create-form-availability", children: /* @__PURE__ */ jsx(
    DataGrid,
    {
      columns,
      data: locations,
      state: form,
      onEditingChange: (editing) => setCloseOnEscape(!editing)
    }
  ) });
};
var columnHelper = createDataGridHelper();
var useColumns = () => {
  const { t } = useTranslation();
  return useMemo(
    () => [
      columnHelper.column({
        id: "location",
        header: () => /* @__PURE__ */ jsx("div", { className: "flex size-full items-center overflow-hidden", children: /* @__PURE__ */ jsx("span", { className: "truncate", children: t("locations.domain") }) }),
        cell: (context) => {
          return /* @__PURE__ */ jsx(DataGrid.ReadonlyCell, { context, children: context.row.original.name });
        },
        disableHiding: true
      }),
      columnHelper.column({
        id: "in-stock",
        name: t("fields.inStock"),
        header: t("fields.inStock"),
        field: (context) => `locations.${context.row.original.id}`,
        type: "number",
        cell: (context) => {
          return /* @__PURE__ */ jsx(DataGrid.NumberCell, { placeholder: "0", context });
        },
        disableHiding: true
      })
    ],
    [t]
  );
};
Root._tabMeta = defineTabMeta({
  id: "availability",
  labelKey: "inventory.create.availability",
  validationFields: ["locations"]
});
var InventoryAvailabilityForm = Root;

// src/pages/inventory/inventory-create/components/inventory-create-form/inventory-create-details-tab.tsx
import {
  Divider,
  Heading,
  Input,
  Textarea
} from "@medusajs/ui";
import { useTranslation as useTranslation2 } from "react-i18next";
import { jsx as jsx2, jsxs } from "react/jsx-runtime";
var Root2 = () => {
  const { t } = useTranslation2();
  const form = useTabbedForm();
  return /* @__PURE__ */ jsx2("div", { className: "flex flex-1 flex-col items-center overflow-y-auto px-3", children: /* @__PURE__ */ jsxs("div", { className: "mx-auto flex w-full max-w-[720px] flex-col gap-y-8 px-px py-16", "data-testid": "inventory-create-form-content", children: [
    /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-8", children: [
      /* @__PURE__ */ jsx2("div", { "data-testid": "inventory-create-form-header", children: /* @__PURE__ */ jsx2(Heading, { "data-testid": "inventory-create-form-heading", children: t("inventory.create.title") }) }),
      /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-6", children: [
        /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-1 gap-4 lg:grid-cols-2", children: [
          /* @__PURE__ */ jsx2(
            Form.Field,
            {
              control: form.control,
              name: "title",
              render: ({ field }) => /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "inventory-create-form-title-item", children: [
                /* @__PURE__ */ jsx2(Form.Label, { "data-testid": "inventory-create-form-title-label", children: t("fields.title") }),
                /* @__PURE__ */ jsx2(Form.Control, { "data-testid": "inventory-create-form-title-control", children: /* @__PURE__ */ jsx2(Input, { ...field, placeholder: t("fields.title"), "data-testid": "inventory-create-form-title-input" }) }),
                /* @__PURE__ */ jsx2(Form.ErrorMessage, { "data-testid": "inventory-create-form-title-error" })
              ] })
            }
          ),
          /* @__PURE__ */ jsx2(
            Form.Field,
            {
              control: form.control,
              name: "sku",
              render: ({ field }) => /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "inventory-create-form-sku-item", children: [
                /* @__PURE__ */ jsx2(Form.Label, { "data-testid": "inventory-create-form-sku-label", children: t("fields.sku") }),
                /* @__PURE__ */ jsx2(Form.Control, { "data-testid": "inventory-create-form-sku-control", children: /* @__PURE__ */ jsx2(Input, { ...field, placeholder: "sku-123", "data-testid": "inventory-create-form-sku-input" }) }),
                /* @__PURE__ */ jsx2(Form.ErrorMessage, { "data-testid": "inventory-create-form-sku-error" })
              ] })
            }
          )
        ] }),
        /* @__PURE__ */ jsx2(
          Form.Field,
          {
            control: form.control,
            name: "description",
            render: ({ field }) => /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "inventory-create-form-description-item", children: [
              /* @__PURE__ */ jsx2(Form.Label, { optional: true, "data-testid": "inventory-create-form-description-label", children: t("products.fields.description.label") }),
              /* @__PURE__ */ jsx2(Form.Control, { "data-testid": "inventory-create-form-description-control", children: /* @__PURE__ */ jsx2(Textarea, { ...field, placeholder: "The item description", "data-testid": "inventory-create-form-description-textarea" }) }),
              /* @__PURE__ */ jsx2(Form.ErrorMessage, { "data-testid": "inventory-create-form-description-error" })
            ] })
          }
        )
      ] }),
      /* @__PURE__ */ jsx2(
        SwitchBox,
        {
          control: form.control,
          name: "requires_shipping",
          label: t("inventory.create.requiresShipping"),
          description: t("inventory.create.requiresShippingHint"),
          "data-testid": "inventory-create-form-requires-shipping"
        }
      )
    ] }),
    /* @__PURE__ */ jsx2(Divider, {}),
    /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-6", children: [
      /* @__PURE__ */ jsx2(Heading, { level: "h2", children: t("inventory.create.attributes") }),
      /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-1 gap-x-4 gap-y-4 lg:grid-cols-2 lg:gap-y-8", children: [
        /* @__PURE__ */ jsx2(
          Form.Field,
          {
            control: form.control,
            name: "width",
            render: ({ field }) => /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "inventory-create-form-width-item", children: [
              /* @__PURE__ */ jsx2(Form.Label, { optional: true, "data-testid": "inventory-create-form-width-label", children: t("products.fields.width.label") }),
              /* @__PURE__ */ jsx2(Form.Control, { "data-testid": "inventory-create-form-width-control", children: /* @__PURE__ */ jsx2(Input, { ...field, type: "number", min: 0, placeholder: "100", "data-testid": "inventory-create-form-width-input" }) }),
              /* @__PURE__ */ jsx2(Form.ErrorMessage, { "data-testid": "inventory-create-form-width-error" })
            ] })
          }
        ),
        /* @__PURE__ */ jsx2(
          Form.Field,
          {
            control: form.control,
            name: "length",
            render: ({ field }) => /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "inventory-create-form-length-item", children: [
              /* @__PURE__ */ jsx2(Form.Label, { optional: true, "data-testid": "inventory-create-form-length-label", children: t("products.fields.length.label") }),
              /* @__PURE__ */ jsx2(Form.Control, { "data-testid": "inventory-create-form-length-control", children: /* @__PURE__ */ jsx2(Input, { ...field, type: "number", min: 0, placeholder: "100", "data-testid": "inventory-create-form-length-input" }) }),
              /* @__PURE__ */ jsx2(Form.ErrorMessage, { "data-testid": "inventory-create-form-length-error" })
            ] })
          }
        ),
        /* @__PURE__ */ jsx2(
          Form.Field,
          {
            control: form.control,
            name: "height",
            render: ({ field }) => /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "inventory-create-form-height-item", children: [
              /* @__PURE__ */ jsx2(Form.Label, { optional: true, "data-testid": "inventory-create-form-height-label", children: t("products.fields.height.label") }),
              /* @__PURE__ */ jsx2(Form.Control, { "data-testid": "inventory-create-form-height-control", children: /* @__PURE__ */ jsx2(Input, { ...field, type: "number", min: 0, placeholder: "100", "data-testid": "inventory-create-form-height-input" }) }),
              /* @__PURE__ */ jsx2(Form.ErrorMessage, { "data-testid": "inventory-create-form-height-error" })
            ] })
          }
        ),
        /* @__PURE__ */ jsx2(
          Form.Field,
          {
            control: form.control,
            name: "weight",
            render: ({ field }) => /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "inventory-create-form-weight-item", children: [
              /* @__PURE__ */ jsx2(Form.Label, { optional: true, "data-testid": "inventory-create-form-weight-label", children: t("products.fields.weight.label") }),
              /* @__PURE__ */ jsx2(Form.Control, { "data-testid": "inventory-create-form-weight-control", children: /* @__PURE__ */ jsx2(Input, { ...field, type: "number", min: 0, placeholder: "100", "data-testid": "inventory-create-form-weight-input" }) }),
              /* @__PURE__ */ jsx2(Form.ErrorMessage, { "data-testid": "inventory-create-form-weight-error" })
            ] })
          }
        ),
        /* @__PURE__ */ jsx2(
          Form.Field,
          {
            control: form.control,
            name: "mid_code",
            render: ({ field }) => /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "inventory-create-form-mid-code-item", children: [
              /* @__PURE__ */ jsx2(Form.Label, { optional: true, "data-testid": "inventory-create-form-mid-code-label", children: t("products.fields.mid_code.label") }),
              /* @__PURE__ */ jsx2(Form.Control, { "data-testid": "inventory-create-form-mid-code-control", children: /* @__PURE__ */ jsx2(Input, { ...field, "data-testid": "inventory-create-form-mid-code-input" }) }),
              /* @__PURE__ */ jsx2(Form.ErrorMessage, { "data-testid": "inventory-create-form-mid-code-error" })
            ] })
          }
        ),
        /* @__PURE__ */ jsx2(
          Form.Field,
          {
            control: form.control,
            name: "hs_code",
            render: ({ field }) => /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "inventory-create-form-hs-code-item", children: [
              /* @__PURE__ */ jsx2(Form.Label, { optional: true, "data-testid": "inventory-create-form-hs-code-label", children: t("products.fields.hs_code.label") }),
              /* @__PURE__ */ jsx2(Form.Control, { "data-testid": "inventory-create-form-hs-code-control", children: /* @__PURE__ */ jsx2(Input, { ...field, "data-testid": "inventory-create-form-hs-code-input" }) }),
              /* @__PURE__ */ jsx2(Form.ErrorMessage, { "data-testid": "inventory-create-form-hs-code-error" })
            ] })
          }
        ),
        /* @__PURE__ */ jsx2(
          Form.Field,
          {
            control: form.control,
            name: "origin_country",
            render: ({ field }) => /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "inventory-create-form-origin-country-item", children: [
              /* @__PURE__ */ jsx2(Form.Label, { optional: true, "data-testid": "inventory-create-form-origin-country-label", children: t("products.fields.countryOrigin.label") }),
              /* @__PURE__ */ jsx2(Form.Control, { "data-testid": "inventory-create-form-origin-country-control", children: /* @__PURE__ */ jsx2(CountrySelect, { ...field, "data-testid": "inventory-create-form-origin-country-select" }) }),
              /* @__PURE__ */ jsx2(Form.ErrorMessage, { "data-testid": "inventory-create-form-origin-country-error" })
            ] })
          }
        ),
        /* @__PURE__ */ jsx2(
          Form.Field,
          {
            control: form.control,
            name: "material",
            render: ({ field }) => /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "inventory-create-form-material-item", children: [
              /* @__PURE__ */ jsx2(Form.Label, { optional: true, "data-testid": "inventory-create-form-material-label", children: t("products.fields.material.label") }),
              /* @__PURE__ */ jsx2(Form.Control, { "data-testid": "inventory-create-form-material-control", children: /* @__PURE__ */ jsx2(Input, { ...field, "data-testid": "inventory-create-form-material-input" }) }),
              /* @__PURE__ */ jsx2(Form.ErrorMessage, { "data-testid": "inventory-create-form-material-error" })
            ] })
          }
        )
      ] })
    ] })
  ] }) });
};
Root2._tabMeta = defineTabMeta({
  id: "details",
  labelKey: "inventory.create.details",
  validationFields: [
    "title",
    "sku",
    "description",
    "hs_code",
    "weight",
    "length",
    "height",
    "width",
    "origin_country",
    "mid_code",
    "material",
    "requires_shipping",
    "thumbnail"
  ]
});
var InventoryCreateDetailsTab = Root2;

// src/pages/inventory/inventory-create/components/inventory-create-form/inventory-create-form.tsx
import { jsx as jsx3 } from "react/jsx-runtime";
function InventoryCreateForm({
  locations,
  children,
  schema,
  defaultValues: extraDefaults
}) {
  const { t } = useTranslation3();
  const { handleSuccess } = useRouteModal();
  const form = useForm({
    defaultValues: {
      title: "",
      sku: "",
      hs_code: "",
      weight: "",
      length: "",
      height: "",
      width: "",
      origin_country: "",
      mid_code: "",
      material: "",
      description: "",
      requires_shipping: true,
      thumbnail: "",
      locations: Object.fromEntries(
        locations.map((location) => [location.id, ""])
      ),
      ...extraDefaults
    },
    resolver: zodResolver(schema ?? CreateInventoryItemSchema)
  });
  const { mutateAsync: createInventoryItem, isPending: isLoading } = useCreateInventoryItem();
  const handleSubmit = form.handleSubmit(async (data) => {
    const { locations: formLocations, weight, length, height, width, ...payload } = data;
    const cleanData = transformNullableFormData(payload, false);
    const cleanNumbers = transformNullableFormNumbers(
      { weight, length, height, width },
      false
    );
    const { inventory_item } = await createInventoryItem(
      {
        ...cleanData,
        ...cleanNumbers
      },
      {
        onError: (e) => {
          toast.error(e.message);
          return;
        }
      }
    );
    await sdk.admin.inventoryItem.batchUpdateLevels(inventory_item.id, {
      create: Object.entries(formLocations ?? {}).filter(([_, quantiy]) => !!quantiy).map(([location_id, stocked_quantity]) => ({
        location_id,
        stocked_quantity: transformNullableFormNumber(
          stocked_quantity,
          false
        )
      }))
    }).then(async () => {
      await queryClient.invalidateQueries({
        queryKey: inventoryItemsQueryKeys.lists()
      });
    }).catch((e) => {
      toast.error(e.message);
    }).finally(() => {
      handleSuccess();
      toast.success(t("inventory.create.successToast"));
    });
  });
  const defaultTabs = useMemo2(
    () => [
      /* @__PURE__ */ jsx3(InventoryCreateDetailsTab, {}, "details"),
      /* @__PURE__ */ jsx3(InventoryAvailabilityForm, { locations }, "availability")
    ],
    [locations]
  );
  const hasCustomChildren = Children.count(children) > 0;
  return /* @__PURE__ */ jsx3(
    TabbedForm,
    {
      form,
      onSubmit: handleSubmit,
      isLoading,
      children: hasCustomChildren ? children : defaultTabs
    }
  );
}

// src/pages/inventory/inventory-create/inventory-create.tsx
import { jsx as jsx4 } from "react/jsx-runtime";
var Root3 = ({ children }) => {
  const { isPending, stock_locations, isError, error } = useStockLocations({
    limit: 9999,
    fields: "id,name"
  });
  const ready = !isPending && !!stock_locations;
  if (isError) {
    throw error;
  }
  return /* @__PURE__ */ jsx4(RouteFocusModal, { children: ready && (Children2.count(children) > 0 ? children : /* @__PURE__ */ jsx4(InventoryCreateForm, { locations: stock_locations })) });
};
var InventoryCreatePage = Object.assign(Root3, {
  Form: InventoryCreateForm,
  DetailsTab: InventoryCreateDetailsTab,
  AvailabilityTab: InventoryAvailabilityForm,
  Tab: TabbedForm.Tab
});
var InventoryCreate = Root3;

export {
  CreateInventoryItemSchema,
  InventoryCreatePage,
  InventoryCreate
};
