"use client";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
import * as React from "react";
import { Button } from "@open-mercato/ui/primitives/button";
import {
  Select,
  SelectContent,
  SelectItem,
  SelectTrigger,
  SelectValue
} from "@open-mercato/ui/primitives/select";
import { apiCall, withScopedApiRequestHeaders } from "@open-mercato/ui/backend/utils/apiCall";
import { buildOptimisticLockHeader } from "@open-mercato/ui/backend/utils/optimisticLock";
import { useGuardedMutation } from "@open-mercato/ui/backend/injection/useGuardedMutation";
import { flash } from "@open-mercato/ui/backend/FlashMessages";
import { useT } from "@open-mercato/shared/lib/i18n/context";
import { Loader2 } from "lucide-react";
import {
  Dialog,
  DialogContent,
  DialogHeader,
  DialogTitle,
  DialogFooter,
  DialogDescription
} from "@open-mercato/ui/primitives/dialog";
import { Input } from "@open-mercato/ui/primitives/input";
import { Textarea } from "@open-mercato/ui/primitives/textarea";
import { Label } from "@open-mercato/ui/primitives/label";
import { useActionDescriptionResolver } from "./ActionCard.js";
function ShipmentPayloadEditor({
  payload,
  updateField
}) {
  const t = useT();
  const trackingNumbers = (payload.trackingNumbers || []).join(", ");
  return /* @__PURE__ */ jsxs("div", { className: "space-y-3", children: [
    /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-3", children: [
      /* @__PURE__ */ jsxs("div", { children: [
        /* @__PURE__ */ jsx(Label, { children: t("inbox_ops.edit_dialog.carrier", "Carrier") }),
        /* @__PURE__ */ jsx(Input, { value: payload.carrierName || "", onChange: (event) => updateField("carrierName", event.target.value) })
      ] }),
      /* @__PURE__ */ jsxs("div", { children: [
        /* @__PURE__ */ jsx(Label, { children: t("inbox_ops.edit_dialog.status", "Status") }),
        /* @__PURE__ */ jsx(Input, { value: payload.statusLabel || "", onChange: (event) => updateField("statusLabel", event.target.value) })
      ] })
    ] }),
    /* @__PURE__ */ jsxs("div", { children: [
      /* @__PURE__ */ jsx(Label, { children: t("inbox_ops.edit_dialog.tracking_numbers", "Tracking Numbers") }),
      /* @__PURE__ */ jsx(
        Input,
        {
          value: trackingNumbers,
          onChange: (event) => updateField("trackingNumbers", event.target.value.split(",").map((s) => s.trim()).filter(Boolean)),
          placeholder: t("inbox_ops.placeholder.comma_separated", "Comma-separated")
        }
      )
    ] }),
    /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-3", children: [
      /* @__PURE__ */ jsxs("div", { children: [
        /* @__PURE__ */ jsx(Label, { children: t("inbox_ops.edit_dialog.shipped_at", "Shipped At") }),
        /* @__PURE__ */ jsx(Input, { value: payload.shippedAt || "", onChange: (event) => updateField("shippedAt", event.target.value), placeholder: "YYYY-MM-DD" })
      ] }),
      /* @__PURE__ */ jsxs("div", { children: [
        /* @__PURE__ */ jsx(Label, { children: t("inbox_ops.edit_dialog.estimated_delivery", "Estimated Delivery") }),
        /* @__PURE__ */ jsx(Input, { value: payload.estimatedDelivery || "", onChange: (event) => updateField("estimatedDelivery", event.target.value), placeholder: "YYYY-MM-DD" })
      ] })
    ] }),
    /* @__PURE__ */ jsxs("div", { children: [
      /* @__PURE__ */ jsx(Label, { children: t("inbox_ops.edit_dialog.notes", "Notes") }),
      /* @__PURE__ */ jsx(Textarea, { value: payload.notes || "", onChange: (event) => updateField("notes", event.target.value), rows: 2 })
    ] })
  ] });
}
function ContactPayloadEditor({
  payload,
  updateField
}) {
  const t = useT();
  const type = payload.type || "person";
  const isPerson = type === "person";
  const existingName = payload.name || "";
  const nameParts = existingName.trim().split(/\s+/).filter((p) => p.length > 0);
  const [firstName, setFirstName] = React.useState(() => isPerson ? nameParts[0] || "" : "");
  const [lastName, setLastName] = React.useState(() => isPerson ? nameParts.slice(1).join(" ") || "" : "");
  const updatePersonName = React.useCallback((first, last) => {
    const combined = `${first} ${last}`.trim();
    updateField("name", combined);
  }, [updateField]);
  const lastNameMissing = isPerson && !lastName.trim();
  return /* @__PURE__ */ jsxs("div", { className: "space-y-3", children: [
    /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-3", children: [
      isPerson ? /* @__PURE__ */ jsxs(Fragment, { children: [
        /* @__PURE__ */ jsxs("div", { children: [
          /* @__PURE__ */ jsx(Label, { children: t("inbox_ops.contact.first_name", "First Name") }),
          /* @__PURE__ */ jsx(
            Input,
            {
              value: firstName,
              onChange: (event) => {
                setFirstName(event.target.value);
                updatePersonName(event.target.value, lastName);
              }
            }
          )
        ] }),
        /* @__PURE__ */ jsxs("div", { children: [
          /* @__PURE__ */ jsx(Label, { children: t("inbox_ops.contact.last_name", "Last Name") }),
          /* @__PURE__ */ jsx(
            Input,
            {
              value: lastName,
              onChange: (event) => {
                setLastName(event.target.value);
                updatePersonName(firstName, event.target.value);
              },
              className: lastNameMissing ? "border-red-500" : ""
            }
          ),
          lastNameMissing && /* @__PURE__ */ jsx("p", { className: "text-xs text-red-600 mt-1", children: t("inbox_ops.contact.last_name_required", "Last name is required") })
        ] })
      ] }) : /* @__PURE__ */ jsxs("div", { children: [
        /* @__PURE__ */ jsx(Label, { children: t("inbox_ops.edit_dialog.name", "Name") }),
        /* @__PURE__ */ jsx(Input, { value: existingName, onChange: (event) => updateField("name", event.target.value) })
      ] }),
      /* @__PURE__ */ jsxs("div", { children: [
        /* @__PURE__ */ jsx(Label, { children: t("inbox_ops.edit_dialog.type", "Type") }),
        /* @__PURE__ */ jsxs(
          Select,
          {
            value: type,
            onValueChange: (value) => updateField("type", value),
            children: [
              /* @__PURE__ */ jsx(SelectTrigger, { children: /* @__PURE__ */ jsx(SelectValue, {}) }),
              /* @__PURE__ */ jsxs(SelectContent, { children: [
                /* @__PURE__ */ jsx(SelectItem, { value: "person", children: t("inbox_ops.contact_type.person", "Person") }),
                /* @__PURE__ */ jsx(SelectItem, { value: "company", children: t("inbox_ops.contact_type.company", "Company") })
              ] })
            ]
          }
        )
      ] })
    ] }),
    /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-3", children: [
      /* @__PURE__ */ jsxs("div", { children: [
        /* @__PURE__ */ jsx(Label, { children: t("inbox_ops.edit_dialog.email", "Email") }),
        /* @__PURE__ */ jsx(Input, { value: payload.email || "", onChange: (event) => updateField("email", event.target.value) })
      ] }),
      /* @__PURE__ */ jsxs("div", { children: [
        /* @__PURE__ */ jsx(Label, { children: t("inbox_ops.edit_dialog.phone", "Phone") }),
        /* @__PURE__ */ jsx(Input, { value: payload.phone || "", onChange: (event) => updateField("phone", event.target.value) })
      ] })
    ] }),
    /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-3", children: [
      /* @__PURE__ */ jsxs("div", { children: [
        /* @__PURE__ */ jsx(Label, { children: t("inbox_ops.edit_dialog.company_name", "Company Name") }),
        /* @__PURE__ */ jsx(Input, { value: payload.companyName || "", onChange: (event) => updateField("companyName", event.target.value) })
      ] }),
      /* @__PURE__ */ jsxs("div", { children: [
        /* @__PURE__ */ jsx(Label, { children: t("inbox_ops.edit_dialog.role", "Role") }),
        /* @__PURE__ */ jsx(Input, { value: payload.role || "", onChange: (event) => updateField("role", event.target.value) })
      ] })
    ] })
  ] });
}
function LinkContactPayloadEditor({
  payload,
  updateField
}) {
  const t = useT();
  return /* @__PURE__ */ jsxs("div", { className: "space-y-3", children: [
    /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-3", children: [
      /* @__PURE__ */ jsxs("div", { children: [
        /* @__PURE__ */ jsx(Label, { children: t("inbox_ops.edit_dialog.email", "Email") }),
        /* @__PURE__ */ jsx(Input, { value: payload.emailAddress || "", onChange: (event) => updateField("emailAddress", event.target.value) })
      ] }),
      /* @__PURE__ */ jsxs("div", { children: [
        /* @__PURE__ */ jsx(Label, { children: t("inbox_ops.edit_dialog.contact_id", "Contact ID") }),
        /* @__PURE__ */ jsx(Input, { value: payload.contactId || "", onChange: (event) => updateField("contactId", event.target.value) })
      ] })
    ] }),
    /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-3", children: [
      /* @__PURE__ */ jsxs("div", { children: [
        /* @__PURE__ */ jsx(Label, { children: t("inbox_ops.edit_dialog.contact_type", "Contact Type") }),
        /* @__PURE__ */ jsxs(
          Select,
          {
            value: payload.contactType || "person",
            onValueChange: (value) => updateField("contactType", value),
            children: [
              /* @__PURE__ */ jsx(SelectTrigger, { children: /* @__PURE__ */ jsx(SelectValue, {}) }),
              /* @__PURE__ */ jsxs(SelectContent, { children: [
                /* @__PURE__ */ jsx(SelectItem, { value: "person", children: t("inbox_ops.contact_type.person", "Person") }),
                /* @__PURE__ */ jsx(SelectItem, { value: "company", children: t("inbox_ops.contact_type.company", "Company") })
              ] })
            ]
          }
        )
      ] }),
      /* @__PURE__ */ jsxs("div", { children: [
        /* @__PURE__ */ jsx(Label, { children: t("inbox_ops.edit_dialog.contact_name", "Contact Name") }),
        /* @__PURE__ */ jsx(Input, { value: payload.contactName || "", onChange: (event) => updateField("contactName", event.target.value) })
      ] })
    ] })
  ] });
}
function DraftReplyPayloadEditor({
  payload,
  updateField
}) {
  const t = useT();
  return /* @__PURE__ */ jsxs("div", { className: "space-y-3", children: [
    /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-3", children: [
      /* @__PURE__ */ jsxs("div", { children: [
        /* @__PURE__ */ jsx(Label, { children: t("inbox_ops.edit_dialog.to", "To") }),
        /* @__PURE__ */ jsx(Input, { value: payload.to || "", onChange: (event) => updateField("to", event.target.value) })
      ] }),
      /* @__PURE__ */ jsxs("div", { children: [
        /* @__PURE__ */ jsx(Label, { children: t("inbox_ops.edit_dialog.to_name", "To Name") }),
        /* @__PURE__ */ jsx(Input, { value: payload.toName || "", onChange: (event) => updateField("toName", event.target.value) })
      ] })
    ] }),
    /* @__PURE__ */ jsxs("div", { children: [
      /* @__PURE__ */ jsx(Label, { children: t("inbox_ops.edit_dialog.subject", "Subject") }),
      /* @__PURE__ */ jsx(Input, { value: payload.subject || "", onChange: (event) => updateField("subject", event.target.value) })
    ] }),
    /* @__PURE__ */ jsxs("div", { children: [
      /* @__PURE__ */ jsx(Label, { children: t("inbox_ops.edit_dialog.body", "Body") }),
      /* @__PURE__ */ jsx(Textarea, { value: payload.body || "", onChange: (event) => updateField("body", event.target.value), rows: 6 })
    ] })
  ] });
}
function UpdateOrderPayloadEditor({
  payload,
  updateField
}) {
  const t = useT();
  const quantityChanges = payload.quantityChanges || [];
  const deliveryDateChange = payload.deliveryDateChange || {};
  const noteAdditions = Array.isArray(payload.noteAdditions) ? payload.noteAdditions.join("\n") : "";
  return /* @__PURE__ */ jsxs("div", { className: "space-y-3", children: [
    /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-3", children: [
      /* @__PURE__ */ jsxs("div", { children: [
        /* @__PURE__ */ jsx(Label, { children: t("inbox_ops.edit_dialog.order_id", "Order ID") }),
        /* @__PURE__ */ jsx(Input, { value: payload.orderId || "", onChange: (event) => updateField("orderId", event.target.value) })
      ] }),
      /* @__PURE__ */ jsxs("div", { children: [
        /* @__PURE__ */ jsx(Label, { children: t("inbox_ops.edit_dialog.order_number", "Order Number") }),
        /* @__PURE__ */ jsx(Input, { value: payload.orderNumber || "", onChange: (event) => updateField("orderNumber", event.target.value) })
      ] })
    ] }),
    quantityChanges.length > 0 && /* @__PURE__ */ jsxs("div", { children: [
      /* @__PURE__ */ jsx(Label, { className: "mb-1 block", children: t("inbox_ops.edit_dialog.quantity_changes", "Quantity Changes") }),
      /* @__PURE__ */ jsx("div", { className: "space-y-2", children: quantityChanges.map((change, index) => /* @__PURE__ */ jsx("div", { className: "border rounded p-2 space-y-2", children: /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-3 gap-2", children: [
        /* @__PURE__ */ jsxs("div", { children: [
          /* @__PURE__ */ jsx(Label, { className: "text-xs", children: t("inbox_ops.edit_dialog.line_item", "Line Item") }),
          /* @__PURE__ */ jsx(
            Input,
            {
              className: "h-8 text-sm",
              value: change.lineItemName || "",
              onChange: (event) => {
                const arr = [...quantityChanges];
                arr[index] = { ...arr[index], lineItemName: event.target.value };
                updateField("quantityChanges", arr);
              }
            }
          )
        ] }),
        /* @__PURE__ */ jsxs("div", { children: [
          /* @__PURE__ */ jsx(Label, { className: "text-xs", children: t("inbox_ops.edit_dialog.old_qty", "Old Qty") }),
          /* @__PURE__ */ jsx(
            Input,
            {
              className: "h-8 text-sm",
              value: change.oldQuantity || "",
              onChange: (event) => {
                const arr = [...quantityChanges];
                arr[index] = { ...arr[index], oldQuantity: event.target.value };
                updateField("quantityChanges", arr);
              }
            }
          )
        ] }),
        /* @__PURE__ */ jsxs("div", { children: [
          /* @__PURE__ */ jsx(Label, { className: "text-xs", children: t("inbox_ops.edit_dialog.new_qty", "New Qty") }),
          /* @__PURE__ */ jsx(
            Input,
            {
              className: "h-8 text-sm",
              value: change.newQuantity || "",
              onChange: (event) => {
                const arr = [...quantityChanges];
                arr[index] = { ...arr[index], newQuantity: event.target.value };
                updateField("quantityChanges", arr);
              }
            }
          )
        ] })
      ] }) }, index)) })
    ] }),
    Boolean(deliveryDateChange.oldDate || deliveryDateChange.newDate) && /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-3", children: [
      /* @__PURE__ */ jsxs("div", { children: [
        /* @__PURE__ */ jsx(Label, { children: t("inbox_ops.edit_dialog.old_delivery_date", "Old Delivery Date") }),
        /* @__PURE__ */ jsx(
          Input,
          {
            value: deliveryDateChange.oldDate || "",
            onChange: (event) => updateField("deliveryDateChange", { ...deliveryDateChange, oldDate: event.target.value }),
            placeholder: "YYYY-MM-DD"
          }
        )
      ] }),
      /* @__PURE__ */ jsxs("div", { children: [
        /* @__PURE__ */ jsx(Label, { children: t("inbox_ops.edit_dialog.new_delivery_date", "New Delivery Date") }),
        /* @__PURE__ */ jsx(
          Input,
          {
            value: deliveryDateChange.newDate || "",
            onChange: (event) => updateField("deliveryDateChange", { ...deliveryDateChange, newDate: event.target.value }),
            placeholder: "YYYY-MM-DD"
          }
        )
      ] })
    ] }),
    /* @__PURE__ */ jsxs("div", { children: [
      /* @__PURE__ */ jsx(Label, { children: t("inbox_ops.edit_dialog.note_additions", "Notes") }),
      /* @__PURE__ */ jsx(
        Textarea,
        {
          value: noteAdditions,
          onChange: (event) => {
            const lines = event.target.value.split("\n").filter(Boolean);
            updateField("noteAdditions", lines.length > 0 ? lines : void 0);
          },
          rows: 2,
          placeholder: t("inbox_ops.placeholder.one_note_per_line", "One note per line")
        }
      )
    ] })
  ] });
}
function LogActivityPayloadEditor({
  payload,
  updateField
}) {
  const t = useT();
  return /* @__PURE__ */ jsxs("div", { className: "space-y-3", children: [
    /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-3", children: [
      /* @__PURE__ */ jsxs("div", { children: [
        /* @__PURE__ */ jsx(Label, { children: t("inbox_ops.edit_dialog.contact_name", "Contact Name") }),
        /* @__PURE__ */ jsx(Input, { value: payload.contactName || "", onChange: (event) => updateField("contactName", event.target.value) })
      ] }),
      /* @__PURE__ */ jsxs("div", { children: [
        /* @__PURE__ */ jsx(Label, { children: t("inbox_ops.edit_dialog.contact_type", "Contact Type") }),
        /* @__PURE__ */ jsxs(
          Select,
          {
            value: payload.contactType || "person",
            onValueChange: (value) => updateField("contactType", value),
            children: [
              /* @__PURE__ */ jsx(SelectTrigger, { children: /* @__PURE__ */ jsx(SelectValue, {}) }),
              /* @__PURE__ */ jsxs(SelectContent, { children: [
                /* @__PURE__ */ jsx(SelectItem, { value: "person", children: t("inbox_ops.contact_type.person", "Person") }),
                /* @__PURE__ */ jsx(SelectItem, { value: "company", children: t("inbox_ops.contact_type.company", "Company") })
              ] })
            ]
          }
        )
      ] })
    ] }),
    /* @__PURE__ */ jsxs("div", { children: [
      /* @__PURE__ */ jsx(Label, { children: t("inbox_ops.edit_dialog.activity_type", "Activity Type") }),
      /* @__PURE__ */ jsxs(
        Select,
        {
          value: payload.activityType || "email",
          onValueChange: (value) => updateField("activityType", value),
          children: [
            /* @__PURE__ */ jsx(SelectTrigger, { children: /* @__PURE__ */ jsx(SelectValue, {}) }),
            /* @__PURE__ */ jsxs(SelectContent, { children: [
              /* @__PURE__ */ jsx(SelectItem, { value: "email", children: t("inbox_ops.activity_type.email", "Email") }),
              /* @__PURE__ */ jsx(SelectItem, { value: "call", children: t("inbox_ops.activity_type.call", "Call") }),
              /* @__PURE__ */ jsx(SelectItem, { value: "meeting", children: t("inbox_ops.activity_type.meeting", "Meeting") }),
              /* @__PURE__ */ jsx(SelectItem, { value: "note", children: t("inbox_ops.activity_type.note", "Note") })
            ] })
          ]
        }
      )
    ] }),
    /* @__PURE__ */ jsxs("div", { children: [
      /* @__PURE__ */ jsx(Label, { children: t("inbox_ops.edit_dialog.subject", "Subject") }),
      /* @__PURE__ */ jsx(Input, { value: payload.subject || "", onChange: (event) => updateField("subject", event.target.value) })
    ] }),
    /* @__PURE__ */ jsxs("div", { children: [
      /* @__PURE__ */ jsx(Label, { children: t("inbox_ops.edit_dialog.body", "Body") }),
      /* @__PURE__ */ jsx(Textarea, { value: payload.body || "", onChange: (event) => updateField("body", event.target.value), rows: 6 })
    ] })
  ] });
}
function EditActionDialog({
  action,
  actionTypeLabels,
  onClose,
  onSaved
}) {
  const t = useT();
  const { runMutation } = useGuardedMutation({
    contextId: "inbox-ops-edit-action"
  });
  const [payload, setPayload] = React.useState(
    () => structuredClone(action.payload)
  );
  const [isSaving, setIsSaving] = React.useState(false);
  const [jsonMode, setJsonMode] = React.useState(false);
  const [jsonText, setJsonText] = React.useState(() => JSON.stringify(action.payload, null, 2));
  const [jsonError, setJsonError] = React.useState(null);
  const handleSave = React.useCallback(async () => {
    let finalPayload = payload;
    if (jsonMode) {
      try {
        finalPayload = JSON.parse(jsonText);
        setJsonError(null);
      } catch {
        setJsonError(t("inbox_ops.edit_dialog.invalid_json", "Invalid JSON"));
        return;
      }
    }
    setIsSaving(true);
    const result = await runMutation({
      // TODO(#2373-D): thread updatedAt — ActionDetail type lives outside this file's edit scope
      operation: () => withScopedApiRequestHeaders(
        buildOptimisticLockHeader(void 0),
        () => apiCall(
          `/api/inbox_ops/proposals/${action.proposalId}/actions/${action.id}`,
          { method: "PATCH", body: JSON.stringify({ payload: finalPayload }) }
        )
      ),
      context: {}
    });
    if (result?.ok && result.result?.ok) {
      flash(t("inbox_ops.edit_dialog.saved", "Action updated successfully"), "success");
      onSaved();
      onClose();
    } else {
      flash(result?.result?.error || t("inbox_ops.flash.save_failed", "Failed to save"), "error");
    }
    setIsSaving(false);
  }, [action, payload, jsonMode, jsonText, t, onSaved, onClose, runMutation]);
  React.useEffect(() => {
    function handleKeyDown(event) {
      if ((event.metaKey || event.ctrlKey) && event.key === "Enter") {
        event.preventDefault();
        handleSave();
      }
    }
    window.addEventListener("keydown", handleKeyDown);
    return () => window.removeEventListener("keydown", handleKeyDown);
  }, [handleSave]);
  const updateField = (key, value) => {
    setPayload((prev) => ({ ...prev, [key]: value }));
  };
  const resolveActionDescription = useActionDescriptionResolver();
  const label = actionTypeLabels[action.actionType] || action.actionType;
  const hasTypedEditor = [
    "create_order",
    "create_quote",
    "update_order",
    "update_shipment",
    "create_contact",
    "link_contact",
    "log_activity",
    "draft_reply"
  ].includes(action.actionType);
  return /* @__PURE__ */ jsx(Dialog, { open: true, onOpenChange: (open) => {
    if (!open) onClose();
  }, children: /* @__PURE__ */ jsxs(DialogContent, { className: "sm:max-w-2xl sm:max-h-[85vh]", children: [
    /* @__PURE__ */ jsxs(DialogHeader, { children: [
      /* @__PURE__ */ jsxs(DialogTitle, { children: [
        t("inbox_ops.edit_dialog.title", "Edit Action"),
        ": ",
        label
      ] }),
      /* @__PURE__ */ jsx(DialogDescription, { children: resolveActionDescription(action.description, action.payload) })
    ] }),
    /* @__PURE__ */ jsxs("div", { className: "space-y-4 overflow-y-auto max-h-[60vh] py-2", children: [
      hasTypedEditor && !jsonMode && /* @__PURE__ */ jsxs(Fragment, { children: [
        action.actionType === "update_order" && /* @__PURE__ */ jsx(UpdateOrderPayloadEditor, { payload, updateField }),
        action.actionType === "update_shipment" && /* @__PURE__ */ jsx(ShipmentPayloadEditor, { payload, updateField }),
        action.actionType === "create_contact" && /* @__PURE__ */ jsx(ContactPayloadEditor, { payload, updateField }),
        action.actionType === "link_contact" && /* @__PURE__ */ jsx(LinkContactPayloadEditor, { payload, updateField }),
        action.actionType === "log_activity" && /* @__PURE__ */ jsx(LogActivityPayloadEditor, { payload, updateField }),
        action.actionType === "draft_reply" && /* @__PURE__ */ jsx(DraftReplyPayloadEditor, { payload, updateField })
      ] }),
      (!hasTypedEditor || jsonMode) && /* @__PURE__ */ jsxs("div", { className: "space-y-2", children: [
        /* @__PURE__ */ jsx(Label, { children: t("inbox_ops.edit_dialog.payload_json", "Payload (JSON)") }),
        /* @__PURE__ */ jsx(
          Textarea,
          {
            className: "font-mono text-xs min-h-[200px]",
            value: jsonText,
            onChange: (event) => {
              setJsonText(event.target.value);
              setJsonError(null);
            }
          }
        ),
        jsonError && /* @__PURE__ */ jsx("p", { className: "text-xs text-red-600", children: t("inbox_ops.edit_dialog.invalid_json", "Invalid JSON") })
      ] }),
      hasTypedEditor && /* @__PURE__ */ jsx(
        Button,
        {
          type: "button",
          variant: "ghost",
          size: "sm",
          onClick: () => {
            if (!jsonMode) {
              setJsonText(JSON.stringify(payload, null, 2));
            } else {
              try {
                setPayload(JSON.parse(jsonText));
                setJsonError(null);
              } catch {
                setJsonError(t("inbox_ops.edit_dialog.invalid_json", "Invalid JSON"));
                return;
              }
            }
            setJsonMode(!jsonMode);
          },
          children: jsonMode ? t("inbox_ops.edit_dialog.form_view", "Form view") : t("inbox_ops.edit_dialog.json_view", "JSON view")
        }
      )
    ] }),
    /* @__PURE__ */ jsxs(DialogFooter, { children: [
      /* @__PURE__ */ jsx(Button, { type: "button", variant: "outline", onClick: onClose, disabled: isSaving, children: t("inbox_ops.edit_dialog.cancel", "Cancel") }),
      /* @__PURE__ */ jsxs(Button, { type: "button", onClick: handleSave, disabled: isSaving, children: [
        isSaving && /* @__PURE__ */ jsx(Loader2, { className: "h-4 w-4 animate-spin mr-1" }),
        t("inbox_ops.edit_dialog.save", "Save Changes")
      ] })
    ] })
  ] }) });
}
export {
  EditActionDialog
};
//# sourceMappingURL=EditActionDialog.js.map
