"use client";
import { jsx, jsxs } from "react/jsx-runtime";
import { Button } from "@open-mercato/ui/primitives/button";
import { Input } from "@open-mercato/ui/primitives/input";
import { useT } from "@open-mercato/shared/lib/i18n/context";
const PACKAGE_FIELDS = ["weightKg", "lengthCm", "widthCm", "heightCm"];
const DEFAULT_PACKAGE = { weightKg: 1, lengthCm: 20, widthCm: 15, heightCm: 10 };
const PackageEditor = (props) => {
  const { packages, onChange, disabled } = props;
  const t = useT();
  const fieldLabel = (field) => {
    const labels = {
      weightKg: t("shipping_carriers.create.package.weightKg", "Weight (kg)"),
      lengthCm: t("shipping_carriers.create.package.lengthCm", "Length (cm)"),
      widthCm: t("shipping_carriers.create.package.widthCm", "Width (cm)"),
      heightCm: t("shipping_carriers.create.package.heightCm", "Height (cm)")
    };
    return labels[field];
  };
  const updatePackage = (index, field, raw) => {
    const value = parseFloat(raw);
    onChange(packages.map(
      (pkg, idx) => idx === index ? { ...pkg, [field]: Number.isNaN(value) ? 0 : value } : pkg
    ));
  };
  const addPackage = () => onChange([...packages, DEFAULT_PACKAGE]);
  const removePackage = (index) => onChange(packages.filter((_, idx) => idx !== index));
  return /* @__PURE__ */ jsxs("div", { className: "space-y-3", children: [
    packages.map((pkg, index) => /* @__PURE__ */ jsxs("div", { className: "rounded-lg border bg-muted/30 p-3", children: [
      /* @__PURE__ */ jsxs("div", { className: "mb-2 flex items-center justify-between", children: [
        /* @__PURE__ */ jsxs("span", { className: "text-xs font-medium text-muted-foreground", children: [
          t("shipping_carriers.create.package.label", "Package"),
          " ",
          index + 1
        ] }),
        packages.length > 1 ? /* @__PURE__ */ jsx(
          Button,
          {
            type: "button",
            variant: "ghost",
            size: "sm",
            className: "h-auto px-2 py-0.5 text-xs text-red-600 hover:text-red-700",
            onClick: () => removePackage(index),
            disabled,
            children: t("shipping_carriers.create.package.remove", "Remove")
          }
        ) : null
      ] }),
      /* @__PURE__ */ jsx("div", { className: "grid gap-3 sm:grid-cols-4", children: PACKAGE_FIELDS.map((field) => /* @__PURE__ */ jsxs("div", { children: [
        /* @__PURE__ */ jsx("label", { className: "mb-1 block text-xs font-medium text-muted-foreground", children: fieldLabel(field) }),
        /* @__PURE__ */ jsx(
          Input,
          {
            type: "number",
            min: "0.01",
            step: "0.01",
            value: pkg[field],
            onChange: (event) => updatePackage(index, field, event.target.value),
            disabled
          }
        )
      ] }, field)) })
    ] }, index)),
    /* @__PURE__ */ jsx(Button, { type: "button", variant: "outline", size: "sm", onClick: addPackage, disabled, children: t("shipping_carriers.create.package.add", "+ Add package") })
  ] });
};
export {
  PackageEditor
};
//# sourceMappingURL=PackageEditor.js.map
