"use client";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
import * as React from "react";
import { Button } from "@open-mercato/ui/primitives/button";
import { Input } from "@open-mercato/ui/primitives/input";
import { EmailInput } from "@open-mercato/ui/primitives/email-input";
import {
  Select,
  SelectContent,
  SelectItem,
  SelectTrigger,
  SelectValue
} from "@open-mercato/ui/primitives/select";
import { Spinner } from "@open-mercato/ui/primitives/spinner";
import { Card, CardContent, CardHeader, CardTitle } from "@open-mercato/ui/primitives/card";
import { useT } from "@open-mercato/shared/lib/i18n/context";
import { AddressFields } from "./AddressFields.js";
import { PackageEditor } from "./PackageEditor.js";
const LABEL_FORMATS = ["pdf", "zpl", "png"];
const ConfigureStep = (props) => {
  const {
    origin,
    destination,
    packages,
    labelFormat,
    senderContact,
    receiverContact,
    targetPoint,
    c2cSendingMethod,
    isFetchingRates,
    canProceed,
    senderContactErrors,
    receiverContactErrors,
    dropOffPointQuery,
    dropOffPoints,
    isFetchingDropOffPoints,
    dropOffPointsError,
    onOriginChange,
    onDestinationChange,
    onPackagesChange,
    onLabelFormatChange,
    onSenderContactChange,
    onReceiverContactChange,
    onTargetPointChange,
    onC2cSendingMethodChange,
    onSearchDropOffPoints,
    onBack,
    onNext
  } = props;
  const t = useT();
  const [searchInput, setSearchInput] = React.useState(dropOffPointQuery);
  return /* @__PURE__ */ jsxs("section", { className: "space-y-6", children: [
    /* @__PURE__ */ jsxs("div", { className: "grid gap-6 lg:grid-cols-2", children: [
      /* @__PURE__ */ jsxs(Card, { children: [
        /* @__PURE__ */ jsx(CardHeader, { children: /* @__PURE__ */ jsx(CardTitle, { className: "text-base", children: t("shipping_carriers.create.section.origin", "Origin address") }) }),
        /* @__PURE__ */ jsx(CardContent, { children: /* @__PURE__ */ jsx(AddressFields, { prefix: "origin", address: origin, onChange: onOriginChange, disabled: isFetchingRates }) })
      ] }),
      /* @__PURE__ */ jsxs(Card, { children: [
        /* @__PURE__ */ jsx(CardHeader, { children: /* @__PURE__ */ jsx(CardTitle, { className: "text-base", children: t("shipping_carriers.create.section.destination", "Destination address") }) }),
        /* @__PURE__ */ jsx(CardContent, { children: /* @__PURE__ */ jsx(AddressFields, { prefix: "dest", address: destination, onChange: onDestinationChange, disabled: isFetchingRates }) })
      ] })
    ] }),
    /* @__PURE__ */ jsxs(Card, { children: [
      /* @__PURE__ */ jsx(CardHeader, { children: /* @__PURE__ */ jsx(CardTitle, { className: "text-base", children: t("shipping_carriers.create.section.packages", "Packages") }) }),
      /* @__PURE__ */ jsx(CardContent, { children: /* @__PURE__ */ jsx(PackageEditor, { packages, onChange: onPackagesChange, disabled: isFetchingRates }) })
    ] }),
    /* @__PURE__ */ jsxs("div", { className: "grid gap-6 lg:grid-cols-2", children: [
      /* @__PURE__ */ jsxs(Card, { children: [
        /* @__PURE__ */ jsx(CardHeader, { children: /* @__PURE__ */ jsx(CardTitle, { className: "text-base", children: t("shipping_carriers.create.section.senderContact", "Sender contact") }) }),
        /* @__PURE__ */ jsx(CardContent, { children: /* @__PURE__ */ jsxs("div", { className: "grid gap-4 sm:grid-cols-2", children: [
          /* @__PURE__ */ jsxs("div", { className: "space-y-1", children: [
            /* @__PURE__ */ jsx("label", { className: "text-xs font-medium text-muted-foreground", children: t("shipping_carriers.create.field.phone", "Phone") }),
            /* @__PURE__ */ jsx(
              Input,
              {
                type: "tel",
                size: "sm",
                value: senderContact.phone,
                onChange: (e) => onSenderContactChange({ ...senderContact, phone: e.target.value }),
                disabled: isFetchingRates,
                placeholder: "e.g. 500000000",
                "aria-invalid": senderContactErrors.phone ? true : void 0
              }
            ),
            senderContactErrors.phone ? /* @__PURE__ */ jsx("p", { className: "text-xs text-destructive", children: senderContactErrors.phone }) : null
          ] }),
          /* @__PURE__ */ jsxs("div", { className: "space-y-1", children: [
            /* @__PURE__ */ jsx("label", { className: "text-xs font-medium text-muted-foreground", children: t("shipping_carriers.create.field.email", "Email") }),
            /* @__PURE__ */ jsx(
              EmailInput,
              {
                size: "sm",
                value: senderContact.email,
                onChange: (e) => onSenderContactChange({ ...senderContact, email: e.target.value }),
                disabled: isFetchingRates,
                placeholder: "e.g. sender@example.com",
                "aria-invalid": senderContactErrors.email ? true : void 0
              }
            ),
            senderContactErrors.email ? /* @__PURE__ */ jsx("p", { className: "text-xs text-destructive", children: senderContactErrors.email }) : null
          ] })
        ] }) })
      ] }),
      /* @__PURE__ */ jsxs(Card, { children: [
        /* @__PURE__ */ jsx(CardHeader, { children: /* @__PURE__ */ jsx(CardTitle, { className: "text-base", children: t("shipping_carriers.create.section.receiverContact", "Receiver contact") }) }),
        /* @__PURE__ */ jsx(CardContent, { children: /* @__PURE__ */ jsxs("div", { className: "grid gap-4 sm:grid-cols-2", children: [
          /* @__PURE__ */ jsxs("div", { className: "space-y-1", children: [
            /* @__PURE__ */ jsx("label", { className: "text-xs font-medium text-muted-foreground", children: t("shipping_carriers.create.field.phone", "Phone") }),
            /* @__PURE__ */ jsx(
              Input,
              {
                type: "tel",
                size: "sm",
                value: receiverContact.phone,
                onChange: (e) => onReceiverContactChange({ ...receiverContact, phone: e.target.value }),
                disabled: isFetchingRates,
                placeholder: "e.g. 500000000",
                "aria-invalid": receiverContactErrors.phone ? true : void 0
              }
            ),
            receiverContactErrors.phone ? /* @__PURE__ */ jsx("p", { className: "text-xs text-destructive", children: receiverContactErrors.phone }) : null
          ] }),
          /* @__PURE__ */ jsxs("div", { className: "space-y-1", children: [
            /* @__PURE__ */ jsx("label", { className: "text-xs font-medium text-muted-foreground", children: t("shipping_carriers.create.field.email", "Email") }),
            /* @__PURE__ */ jsx(
              EmailInput,
              {
                size: "sm",
                value: receiverContact.email,
                onChange: (e) => onReceiverContactChange({ ...receiverContact, email: e.target.value }),
                disabled: isFetchingRates,
                placeholder: "e.g. receiver@example.com",
                "aria-invalid": receiverContactErrors.email ? true : void 0
              }
            ),
            receiverContactErrors.email ? /* @__PURE__ */ jsx("p", { className: "text-xs text-destructive", children: receiverContactErrors.email }) : null
          ] })
        ] }) })
      ] })
    ] }),
    /* @__PURE__ */ jsxs(Card, { children: [
      /* @__PURE__ */ jsx(CardHeader, { children: /* @__PURE__ */ jsx(CardTitle, { className: "text-base", children: t("shipping_carriers.create.section.c2cSendingMethod", "C2C sending method") }) }),
      /* @__PURE__ */ jsx(CardContent, { children: /* @__PURE__ */ jsxs("div", { className: "space-y-1", children: [
        /* @__PURE__ */ jsx("label", { className: "text-xs font-medium text-muted-foreground", children: t("shipping_carriers.create.field.c2cSendingMethod", "Sending method (applies to courier_c2c service only)") }),
        /* @__PURE__ */ jsxs(
          Select,
          {
            value: c2cSendingMethod || void 0,
            onValueChange: (value) => onC2cSendingMethodChange(value ?? ""),
            disabled: isFetchingRates,
            children: [
              /* @__PURE__ */ jsx(SelectTrigger, { size: "sm", children: /* @__PURE__ */ jsx(SelectValue, { placeholder: t("shipping_carriers.create.c2cSendingMethod.default", "Dispatch order (default)") }) }),
              /* @__PURE__ */ jsxs(SelectContent, { children: [
                /* @__PURE__ */ jsx(SelectItem, { value: "parcel_locker", children: t("shipping_carriers.create.c2cSendingMethod.parcel_locker", "Parcel locker") }),
                /* @__PURE__ */ jsx(SelectItem, { value: "pop", children: t("shipping_carriers.create.c2cSendingMethod.pop", "POP (parcel pickup point)") }),
                /* @__PURE__ */ jsx(SelectItem, { value: "any_point", children: t("shipping_carriers.create.c2cSendingMethod.any_point", "Any point") })
              ] })
            ]
          }
        )
      ] }) })
    ] }),
    (c2cSendingMethod === "parcel_locker" || c2cSendingMethod === "pop") && /* @__PURE__ */ jsxs(Card, { children: [
      /* @__PURE__ */ jsx(CardHeader, { children: /* @__PURE__ */ jsx(CardTitle, { className: "text-base", children: t("shipping_carriers.create.section.dropOffPoint", "Drop-off / locker point") }) }),
      /* @__PURE__ */ jsxs(CardContent, { className: "space-y-4", children: [
        targetPoint && /* @__PURE__ */ jsxs("div", { className: "rounded border border-primary bg-primary/5 px-3 py-2 text-sm", children: [
          /* @__PURE__ */ jsx("span", { className: "font-medium text-xs text-muted-foreground mr-2", children: t("shipping_carriers.create.field.selectedPoint", "Selected:") }),
          /* @__PURE__ */ jsx("span", { className: "font-mono font-medium", children: targetPoint }),
          /* @__PURE__ */ jsx(
            Button,
            {
              type: "button",
              variant: "ghost",
              size: "sm",
              className: "ml-2 h-auto py-0 text-xs",
              onClick: () => onTargetPointChange(""),
              disabled: isFetchingRates,
              children: t("shipping_carriers.create.action.clearPoint", "Clear")
            }
          )
        ] }),
        /* @__PURE__ */ jsxs("div", { className: "flex gap-2", children: [
          /* @__PURE__ */ jsx(
            Input,
            {
              size: "sm",
              className: "flex-1",
              value: searchInput,
              onChange: (e) => setSearchInput(e.target.value),
              onKeyDown: (e) => {
                if (e.key === "Enter") {
                  e.preventDefault();
                  onSearchDropOffPoints(searchInput);
                }
              },
              disabled: isFetchingRates,
              placeholder: t("shipping_carriers.create.field.dropOffSearch", "Postal code (e.g. 30-624) or point name (e.g. KRA012)")
            }
          ),
          /* @__PURE__ */ jsx(
            Button,
            {
              type: "button",
              variant: "outline",
              size: "sm",
              onClick: () => onSearchDropOffPoints(searchInput),
              disabled: isFetchingRates || isFetchingDropOffPoints || searchInput.trim().length === 0,
              children: isFetchingDropOffPoints ? /* @__PURE__ */ jsx(Spinner, { className: "h-4 w-4" }) : t("shipping_carriers.create.action.searchPoints", "Search")
            }
          )
        ] }),
        dropOffPointsError && /* @__PURE__ */ jsx("p", { className: "text-xs text-destructive", children: dropOffPointsError }),
        dropOffPoints.length > 0 && /* @__PURE__ */ jsx("ul", { className: "max-h-60 overflow-y-auto divide-y rounded border text-sm", children: dropOffPoints.map((point) => /* @__PURE__ */ jsx("li", { children: /* @__PURE__ */ jsxs(
          "button",
          {
            type: "button",
            className: `w-full px-3 py-2 text-left hover:bg-accent ${targetPoint === point.id ? "bg-primary/5 font-medium" : ""}`,
            onClick: () => onTargetPointChange(point.id),
            disabled: isFetchingRates,
            children: [
              /* @__PURE__ */ jsx("span", { className: "font-mono mr-2", children: point.name }),
              /* @__PURE__ */ jsx("span", { className: "text-muted-foreground text-xs", children: [point.street, point.city, point.postalCode].filter(Boolean).join(", ") })
            ]
          }
        ) }, point.id)) }),
        !isFetchingDropOffPoints && dropOffPoints.length === 0 && dropOffPointQuery && !dropOffPointsError && /* @__PURE__ */ jsx("p", { className: "text-xs text-muted-foreground", children: t("shipping_carriers.create.dropOffPoints.noResults", "No drop-off points found for this search.") })
      ] })
    ] }),
    /* @__PURE__ */ jsxs(Card, { children: [
      /* @__PURE__ */ jsx(CardHeader, { children: /* @__PURE__ */ jsx(CardTitle, { className: "text-base", children: t("shipping_carriers.create.section.labelFormat", "Label format") }) }),
      /* @__PURE__ */ jsx(CardContent, { children: /* @__PURE__ */ jsx("div", { className: "flex gap-3", children: LABEL_FORMATS.map((fmt) => /* @__PURE__ */ jsx(
        Button,
        {
          type: "button",
          variant: labelFormat === fmt ? "default" : "outline",
          size: "sm",
          onClick: () => onLabelFormatChange(fmt),
          disabled: isFetchingRates,
          children: fmt.toUpperCase()
        },
        fmt
      )) }) })
    ] }),
    /* @__PURE__ */ jsxs("div", { className: "flex justify-between gap-3", children: [
      /* @__PURE__ */ jsx(Button, { type: "button", variant: "outline", onClick: onBack, disabled: isFetchingRates, children: t("shipping_carriers.create.back", "Back") }),
      /* @__PURE__ */ jsx(Button, { type: "button", onClick: onNext, disabled: !canProceed || isFetchingRates, children: isFetchingRates ? /* @__PURE__ */ jsxs(Fragment, { children: [
        /* @__PURE__ */ jsx(Spinner, { className: "mr-2 h-4 w-4" }),
        t("shipping_carriers.create.fetchingRates", "Fetching rates...")
      ] }) : t("shipping_carriers.create.getQuotes", "Get quotes") })
    ] })
  ] });
};
export {
  ConfigureStep
};
//# sourceMappingURL=ConfigureStep.js.map
