"use client";
import { jsx, jsxs } from "react/jsx-runtime";
import * as React from "react";
import { useRouter, useSearchParams } from "next/navigation";
import { Page, PageBody } from "@open-mercato/ui/backend/Page";
import { DataTable } from "@open-mercato/ui/backend/DataTable";
import { Tag } from "@open-mercato/ui/primitives/tag";
import { Button } from "@open-mercato/ui/primitives/button";
import { Alert, AlertDescription } from "@open-mercato/ui/primitives/alert";
import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogTitle
} from "@open-mercato/ui/primitives/dialog";
import { Input } from "@open-mercato/ui/primitives/input";
import { Label } from "@open-mercato/ui/primitives/label";
import { Textarea } from "@open-mercato/ui/primitives/textarea";
import { KbdShortcut } from "@open-mercato/ui/primitives/kbd";
import { InjectionSpot } from "@open-mercato/ui/backend/injection/InjectionSpot";
import { apiCall } from "@open-mercato/ui/backend/utils/apiCall";
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";
const PROFILE_CHANNELS_MUTATION_CONTEXT_ID = "communication-channels-profile";
const IMPORT_HISTORY_MUTATION_CONTEXT_ID = "communication-channels-import-history";
const DISCONNECT_MUTATION_CONTEXT_ID = "communication-channels-disconnect";
function ProfileCommunicationChannelsPage() {
  const t = useT();
  const router = useRouter();
  const searchParams = useSearchParams();
  const flashType = searchParams?.get("flash");
  const flashCode = searchParams?.get("code");
  const flashProvider = searchParams?.get("provider");
  const [rows, setRows] = React.useState([]);
  const [isLoading, setIsLoading] = React.useState(true);
  const [errorMessage, setErrorMessage] = React.useState(null);
  const [reloadKey, setReloadKey] = React.useState(0);
  const [importChannel, setImportChannel] = React.useState(null);
  const [disconnectChannel, setDisconnectChannel] = React.useState(null);
  const { runMutation, retryLastMutation } = useGuardedMutation({
    contextId: PROFILE_CHANNELS_MUTATION_CONTEXT_ID,
    blockedMessage: t("ui.forms.flash.saveBlocked", "Save blocked by validation")
  });
  React.useEffect(() => {
    if (flashType === "connected") {
      flash(
        flashProvider ? t("communication_channels.profile.flash.connectedWithProvider", "Channel connected ({provider}).", {
          provider: flashProvider
        }) : t("communication_channels.profile.flash.connected", "Channel connected."),
        "success"
      );
    } else if (flashType === "error") {
      flash(
        flashCode === "oauth_client_not_configured" ? t(
          "communication_channels.profile.connect.notConfigured",
          "This provider is not configured yet. Ask an administrator to add the OAuth Client ID and Secret under Integrations before connecting a mailbox."
        ) : flashCode === "mailbox_already_connected" ? t(
          "communication_channels.profile.connect.mailboxAlreadyConnected",
          "This mailbox is already connected through another provider. Disconnect it first to reconnect it with a different one."
        ) : flashCode ? t("communication_channels.profile.flash.errorWithCode", "Failed to connect channel \u2014 {code}.", {
          code: flashCode
        }) : t("communication_channels.profile.flash.error", "Failed to connect channel."),
        "error"
      );
    }
  }, [flashType, flashCode, flashProvider, t]);
  React.useEffect(() => {
    let cancelled = false;
    async function load() {
      setIsLoading(true);
      setErrorMessage(null);
      const response = await apiCall(
        "/api/communication_channels/me/channels"
      ).catch((err) => ({
        ok: false,
        result: { error: err instanceof Error ? err.message : "Failed to load channels" }
      }));
      if (cancelled) return;
      if (!response.ok) {
        const body = response.result;
        setErrorMessage(
          body?.error ?? t("communication_channels.errors.loadList", "Failed to load channels")
        );
        setRows([]);
      } else {
        const data = response.result ?? {};
        setRows(Array.isArray(data.items) ? data.items : []);
      }
      setIsLoading(false);
    }
    void load();
    return () => {
      cancelled = true;
    };
  }, [reloadKey, t]);
  const reauthRows = rows.filter((r) => r.status === "requires_reauth");
  const onSetPrimary = React.useCallback(
    async (channelId) => {
      let response;
      try {
        response = await runMutation({
          operation: () => apiCall(
            `/api/communication_channels/channels/${encodeURIComponent(channelId)}/set-primary`,
            { method: "POST" }
          ),
          context: {
            formId: PROFILE_CHANNELS_MUTATION_CONTEXT_ID,
            resourceKind: "communication_channels.channel",
            resourceId: channelId,
            retryLastMutation
          },
          mutationPayload: { isPrimary: true }
        });
      } catch (err) {
        flash(err instanceof Error ? err.message : t("communication_channels.profile.actions.setPrimaryFailed", "Failed to set as primary"), "error");
        return;
      }
      if (!response.ok) {
        const body = response.result;
        flash(
          body?.error ?? t("communication_channels.profile.actions.setPrimaryFailed", "Failed to set as primary"),
          "error"
        );
        return;
      }
      flash(
        t("communication_channels.profile.actions.setPrimarySuccess", "Marked as primary."),
        "success"
      );
      setReloadKey((k) => k + 1);
    },
    [retryLastMutation, runMutation, t]
  );
  const onRegisterPush = React.useCallback(
    async (channelId) => {
      let response;
      try {
        response = await runMutation({
          operation: () => apiCall(
            `/api/communication_channels/channels/${encodeURIComponent(channelId)}/push/register`,
            { method: "POST" }
          ),
          context: {
            formId: PROFILE_CHANNELS_MUTATION_CONTEXT_ID,
            resourceKind: "communication_channels.channel",
            resourceId: channelId,
            retryLastMutation
          },
          mutationPayload: { action: "push-register" }
        });
      } catch (err) {
        flash(err instanceof Error ? err.message : t("communication_channels.push.button.reregister", "Re-register push"), "error");
        return;
      }
      if (!response.ok) {
        const body = response.result;
        flash(body?.error ?? t("communication_channels.push.flash.registerFailed", "Failed to register push"), "error");
        return;
      }
      const result = response.result ?? {};
      if (result.pushStatus === "active") {
        flash(t("communication_channels.push.status.active", "Push active"), "success");
      } else {
        flash(
          t(
            "communication_channels.push.status.failed",
            "Push registration returned a non-active status \u2014 falling back to polling."
          ),
          "error"
        );
      }
      setReloadKey((k) => k + 1);
    },
    [retryLastMutation, runMutation, t]
  );
  const onPollNow = React.useCallback(
    async (channelId) => {
      let response;
      try {
        response = await runMutation({
          operation: () => apiCall(
            `/api/communication_channels/channels/${encodeURIComponent(channelId)}/poll-now`,
            { method: "POST" }
          ),
          context: {
            formId: PROFILE_CHANNELS_MUTATION_CONTEXT_ID,
            resourceKind: "communication_channels.channel",
            resourceId: channelId,
            retryLastMutation
          },
          mutationPayload: { action: "poll-now" }
        });
      } catch (err) {
        flash(err instanceof Error ? err.message : t("communication_channels.profile.actions.pollNowFailed", "Failed to trigger poll"), "error");
        return;
      }
      if (!response.ok) {
        const body = response.result;
        flash(
          body?.error ?? t("communication_channels.profile.actions.pollNowFailed", "Failed to trigger poll"),
          "error"
        );
        return;
      }
      flash(
        t(
          "communication_channels.profile.actions.pollNowSuccess",
          "Poll triggered \u2014 new messages will appear on linked Person timelines in a few seconds."
        ),
        "success"
      );
      setTimeout(() => setReloadKey((k) => k + 1), 1500);
    },
    [retryLastMutation, runMutation, t]
  );
  const columns = React.useMemo(
    () => [
      {
        header: t("communication_channels.columns.displayName", "Channel"),
        accessorKey: "displayName"
      },
      {
        header: t("communication_channels.columns.provider", "Provider"),
        accessorKey: "providerKey",
        cell: ({ row }) => /* @__PURE__ */ jsx(Tag, { variant: "info", children: t(
          `communication_channels.channel.providers.${row.original.providerKey}`,
          row.original.providerKey
        ) })
      },
      {
        header: t("communication_channels.columns.identifier", "Email / username"),
        accessorKey: "externalIdentifier",
        cell: ({ row }) => row.original.externalIdentifier ?? "\u2014",
        meta: { truncate: true, maxWidth: 240 }
      },
      {
        header: t("communication_channels.profile.columns.primary", "Primary"),
        accessorKey: "isPrimary",
        cell: ({ row }) => row.original.isPrimary ? /* @__PURE__ */ jsx(Tag, { variant: "success", dot: true, children: t("communication_channels.profile.primary", "Primary") }) : /* @__PURE__ */ jsx(
          Button,
          {
            type: "button",
            variant: "outline",
            size: "sm",
            onClick: () => void onSetPrimary(row.original.id),
            "aria-label": t("communication_channels.profile.actions.setPrimary", "Set as primary"),
            children: t("communication_channels.profile.actions.setPrimary", "Set as primary")
          }
        )
      },
      {
        header: t("communication_channels.columns.status", "Status"),
        accessorKey: "status",
        cell: ({ row }) => statusTag(row.original.status, t)
      },
      {
        id: "pushStatus",
        header: t("communication_channels.push.status.active", "Push"),
        cell: ({ row }) => {
          const supportsPush = row.original.providerKey === "gmail";
          if (!supportsPush) {
            return /* @__PURE__ */ jsx("span", { className: "text-xs text-muted-foreground", children: t("communication_channels.push.status.inactive", "Polling only") });
          }
          const ps = row.original.pushStatus;
          if (ps === "active") {
            return /* @__PURE__ */ jsx(Tag, { variant: "success", dot: true, children: t("communication_channels.push.status.active", "Push active") });
          }
          if (ps === "failed") {
            const errorMsg = row.original.lastPushError?.message ?? null;
            return /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-2", children: [
              /* @__PURE__ */ jsx(Tag, { variant: "error", dot: true, children: t("communication_channels.push.status.failed", "Push failed \u2014 using polling") }),
              /* @__PURE__ */ jsx(
                Button,
                {
                  type: "button",
                  variant: "outline",
                  size: "sm",
                  onClick: () => void onRegisterPush(row.original.id),
                  "aria-label": t("communication_channels.push.button.reregister", "Re-register push"),
                  title: errorMsg ?? void 0,
                  children: t("communication_channels.push.button.reregister", "Re-register push")
                }
              )
            ] });
          }
          return /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-2", children: [
            /* @__PURE__ */ jsx("span", { className: "text-xs text-muted-foreground", children: t("communication_channels.push.status.inactive", "Polling only") }),
            /* @__PURE__ */ jsx(
              Button,
              {
                type: "button",
                variant: "outline",
                size: "sm",
                onClick: () => void onRegisterPush(row.original.id),
                "aria-label": t("communication_channels.push.button.reregister", "Re-register push"),
                children: t("communication_channels.push.button.reregister", "Re-register push")
              }
            )
          ] });
        }
      },
      {
        header: t("communication_channels.profile.columns.lastPolled", "Last synced"),
        accessorKey: "lastPolledAt",
        cell: ({ row }) => row.original.lastPolledAt ? new Date(row.original.lastPolledAt).toLocaleString() : "\u2014"
      },
      {
        id: "importHistory",
        header: t("communication_channels.profile.columns.importHistory", "History"),
        cell: ({ row }) => {
          const eligible = row.original.isActive && row.original.status === "connected" && row.original.channelType === "email";
          const label = t("communication_channels.profile.actions.importHistory", "Import history");
          return /* @__PURE__ */ jsx(
            Button,
            {
              type: "button",
              variant: "outline",
              size: "sm",
              onClick: () => setImportChannel(row.original),
              disabled: !eligible,
              "aria-label": label,
              children: label
            }
          );
        }
      },
      {
        id: "pollNow",
        header: t("communication_channels.profile.columns.pollNow", "Sync"),
        cell: ({ row }) => {
          const pollable = row.original.isActive && (row.original.status === "connected" || row.original.status === "error");
          const label = row.original.status === "error" ? t("communication_channels.profile.actions.retryPoll", "Retry") : t("communication_channels.profile.actions.pollNow", "Poll now");
          return /* @__PURE__ */ jsx(
            Button,
            {
              type: "button",
              variant: row.original.status === "error" ? "default" : "outline",
              size: "sm",
              onClick: () => void onPollNow(row.original.id),
              disabled: !pollable,
              "aria-label": label,
              children: label
            }
          );
        }
      },
      {
        id: "disconnect",
        header: t("communication_channels.profile.columns.disconnect", "Connection"),
        cell: ({ row }) => {
          const label = t("communication_channels.profile.actions.disconnect", "Disconnect");
          return /* @__PURE__ */ jsx(
            Button,
            {
              type: "button",
              variant: "outline",
              size: "sm",
              onClick: () => setDisconnectChannel(row.original),
              "aria-label": label,
              children: label
            }
          );
        }
      }
    ],
    [onSetPrimary, onPollNow, onRegisterPush, t]
  );
  return /* @__PURE__ */ jsx(Page, { children: /* @__PURE__ */ jsxs(PageBody, { children: [
    /* @__PURE__ */ jsxs("header", { className: "mb-4 flex items-baseline justify-between", children: [
      /* @__PURE__ */ jsxs("div", { children: [
        /* @__PURE__ */ jsx("h2", { className: "text-2xl font-semibold", children: t("communication_channels.profile.title", "My communication channels") }),
        /* @__PURE__ */ jsx("p", { className: "text-sm text-muted-foreground", children: t(
          "communication_channels.profile.subtitle",
          "Connect your personal mailbox so outbound messages come from your address and inbound emails land in your unified inbox."
        ) })
      ] }),
      /* @__PURE__ */ jsx(
        InjectionSpot,
        {
          spotId: "profile:communication-channels:connect",
          context: { reload: () => setReloadKey((k) => k + 1) },
          data: {}
        }
      )
    ] }),
    reauthRows.length > 0 ? /* @__PURE__ */ jsx(Alert, { variant: "warning", className: "mb-4", children: /* @__PURE__ */ jsx(AlertDescription, { children: t(
      "communication_channels.profile.alerts.requiresReauth",
      '{count} channel(s) need reconnection. Click "Reconnect" on the affected channel below.',
      { count: reauthRows.length }
    ) }) }) : null,
    /* @__PURE__ */ jsx(
      DataTable,
      {
        title: t("communication_channels.profile.tableTitle", "Your channels"),
        extensionTableId: "communication_channels.profile.channels",
        columns,
        data: rows,
        isLoading,
        error: errorMessage,
        emptyState: t(
          "communication_channels.profile.empty",
          'You have no connected channels yet. Use the "Connect channel" entry above to add Gmail or IMAP.'
        )
      }
    ),
    /* @__PURE__ */ jsx(
      ImportHistoryDialog,
      {
        channel: importChannel,
        onClose: () => setImportChannel(null),
        onQueued: () => {
          setImportChannel(null);
          router.refresh();
        }
      }
    ),
    /* @__PURE__ */ jsx(
      DisconnectChannelDialog,
      {
        channel: disconnectChannel,
        onClose: () => setDisconnectChannel(null),
        onDisconnected: () => {
          setDisconnectChannel(null);
          setReloadKey((k) => k + 1);
        }
      }
    )
  ] }) });
}
function ImportHistoryDialog({ channel, onClose, onQueued }) {
  const t = useT();
  const [sinceDays, setSinceDays] = React.useState("30");
  const [contactEmails, setContactEmails] = React.useState("");
  const [maxMessages, setMaxMessages] = React.useState("500");
  const [fieldErrors, setFieldErrors] = React.useState({});
  const [submitting, setSubmitting] = React.useState(false);
  const { runMutation, retryLastMutation } = useGuardedMutation({
    contextId: IMPORT_HISTORY_MUTATION_CONTEXT_ID,
    blockedMessage: t("ui.forms.flash.saveBlocked", "Save blocked by validation")
  });
  React.useEffect(() => {
    if (channel) {
      setSinceDays("30");
      setContactEmails("");
      setMaxMessages("500");
      setFieldErrors({});
      setSubmitting(false);
    }
  }, [channel?.id]);
  const handleSubmit = React.useCallback(async () => {
    if (!channel || submitting) return;
    const sinceNum = Number.parseInt(sinceDays, 10);
    const maxNum = Number.parseInt(maxMessages, 10);
    const errors = {};
    if (!Number.isFinite(sinceNum) || sinceNum < 1 || sinceNum > 365) {
      errors.sinceDays = t(
        "communication_channels.profile.importHistory.errors.sinceDays",
        "Choose a number between 1 and 365 days."
      );
    }
    if (!Number.isFinite(maxNum) || maxNum < 1 || maxNum > 5e3) {
      errors.maxMessages = t(
        "communication_channels.profile.importHistory.errors.maxMessages",
        "Choose a number between 1 and 5000 messages."
      );
    }
    const parsedEmails = contactEmails.split(/[\s,;]+/).map((s) => s.trim()).filter(Boolean);
    if (parsedEmails.length > 0 && parsedEmails.some((s) => !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(s))) {
      errors.contactEmails = t(
        "communication_channels.profile.importHistory.errors.contactEmails",
        "One or more entries is not a valid email address."
      );
    }
    if (Object.keys(errors).length > 0) {
      setFieldErrors(errors);
      return;
    }
    setFieldErrors({});
    setSubmitting(true);
    const mutationPayload = {
      sinceDays: sinceNum,
      maxMessages: maxNum,
      ...parsedEmails.length > 0 ? { contactEmails: parsedEmails } : {}
    };
    let response;
    try {
      response = await runMutation({
        operation: () => apiCall(
          `/api/communication_channels/channels/${encodeURIComponent(channel.id)}/import-history`,
          {
            method: "POST",
            headers: { "content-type": "application/json" },
            body: JSON.stringify(mutationPayload)
          }
        ),
        context: {
          formId: IMPORT_HISTORY_MUTATION_CONTEXT_ID,
          resourceKind: "communication_channels.channel",
          resourceId: channel.id,
          retryLastMutation
        },
        mutationPayload
      });
    } catch (err) {
      setSubmitting(false);
      flash(
        err instanceof Error ? err.message : t("communication_channels.profile.importHistory.flash.error", "Failed to queue history import."),
        "error"
      );
      return;
    }
    setSubmitting(false);
    if (!response.ok) {
      const body = response.result;
      if (body?.fieldErrors && Object.keys(body.fieldErrors).length > 0) {
        setFieldErrors(body.fieldErrors);
        return;
      }
      flash(
        body?.error ?? t(
          "communication_channels.profile.importHistory.flash.error",
          "Failed to queue history import."
        ),
        "error"
      );
      return;
    }
    flash(
      t(
        "communication_channels.profile.importHistory.flash.success",
        "History import queued \u2014 track progress in the top bar."
      ),
      "success"
    );
    onQueued();
  }, [channel, sinceDays, maxMessages, contactEmails, submitting, t, onQueued, retryLastMutation, runMutation]);
  const handleKeyDown = React.useCallback(
    (event) => {
      if (event.key === "Enter" && (event.metaKey || event.ctrlKey)) {
        event.preventDefault();
        void handleSubmit();
      }
    },
    [handleSubmit]
  );
  return /* @__PURE__ */ jsx(Dialog, { open: channel !== null, onOpenChange: (open) => {
    if (!open) onClose();
  }, children: /* @__PURE__ */ jsxs(DialogContent, { onKeyDown: handleKeyDown, children: [
    /* @__PURE__ */ jsxs(DialogHeader, { children: [
      /* @__PURE__ */ jsx(DialogTitle, { children: t("communication_channels.profile.importHistory.title", "Import channel history") }),
      /* @__PURE__ */ jsx(DialogDescription, { children: t(
        "communication_channels.profile.importHistory.description",
        "Pull older messages this channel never observed at connect-time. Filters narrow the search server-side."
      ) })
    ] }),
    /* @__PURE__ */ jsxs("div", { className: "space-y-4", children: [
      /* @__PURE__ */ jsxs("div", { className: "space-y-1", children: [
        /* @__PURE__ */ jsx(Label, { htmlFor: "import-history-since", children: t("communication_channels.profile.importHistory.fields.sinceDays", "Look back (days)") }),
        /* @__PURE__ */ jsx(
          Input,
          {
            id: "import-history-since",
            type: "number",
            min: 1,
            max: 365,
            value: sinceDays,
            onChange: (e) => setSinceDays(e.target.value),
            "aria-invalid": Boolean(fieldErrors.sinceDays)
          }
        ),
        fieldErrors.sinceDays ? /* @__PURE__ */ jsx("p", { className: "text-xs text-status-error-text", children: fieldErrors.sinceDays }) : null
      ] }),
      /* @__PURE__ */ jsxs("div", { className: "space-y-1", children: [
        /* @__PURE__ */ jsx(Label, { htmlFor: "import-history-emails", children: t(
          "communication_channels.profile.importHistory.fields.contactEmails",
          "Filter by sender (optional)"
        ) }),
        /* @__PURE__ */ jsx(
          Textarea,
          {
            id: "import-history-emails",
            rows: 3,
            value: contactEmails,
            onChange: (e) => setContactEmails(e.target.value),
            placeholder: t(
              "communication_channels.profile.importHistory.fields.contactEmailsPlaceholder",
              "alice@example.com, bob@example.com"
            ),
            "aria-invalid": Boolean(fieldErrors.contactEmails)
          }
        ),
        fieldErrors.contactEmails ? /* @__PURE__ */ jsx("p", { className: "text-xs text-status-error-text", children: fieldErrors.contactEmails }) : /* @__PURE__ */ jsx("p", { className: "text-xs text-muted-foreground", children: t(
          "communication_channels.profile.importHistory.fields.contactEmailsHint",
          "Leave empty to scan all senders in the window."
        ) })
      ] }),
      /* @__PURE__ */ jsxs("div", { className: "space-y-1", children: [
        /* @__PURE__ */ jsx(Label, { htmlFor: "import-history-max", children: t("communication_channels.profile.importHistory.fields.maxMessages", "Maximum messages") }),
        /* @__PURE__ */ jsx(
          Input,
          {
            id: "import-history-max",
            type: "number",
            min: 1,
            max: 5e3,
            value: maxMessages,
            onChange: (e) => setMaxMessages(e.target.value),
            "aria-invalid": Boolean(fieldErrors.maxMessages)
          }
        ),
        fieldErrors.maxMessages ? /* @__PURE__ */ jsx("p", { className: "text-xs text-status-error-text", children: fieldErrors.maxMessages }) : null
      ] }),
      fieldErrors.channelId ? /* @__PURE__ */ jsx(Alert, { variant: "warning", children: /* @__PURE__ */ jsx(AlertDescription, { children: fieldErrors.channelId }) }) : null
    ] }),
    /* @__PURE__ */ jsxs(DialogFooter, { children: [
      /* @__PURE__ */ jsx("span", { className: "mr-auto text-xs text-muted-foreground", children: /* @__PURE__ */ jsx(KbdShortcut, { keys: ["\u2318", "Enter"] }) }),
      /* @__PURE__ */ jsx(Button, { type: "button", variant: "outline", onClick: onClose, disabled: submitting, children: t("communication_channels.profile.importHistory.cancel", "Cancel") }),
      /* @__PURE__ */ jsx(Button, { type: "button", onClick: () => void handleSubmit(), disabled: submitting, children: submitting ? t("communication_channels.profile.importHistory.submitting", "Queueing\u2026") : t("communication_channels.profile.importHistory.submit", "Start import") })
    ] })
  ] }) });
}
function DisconnectChannelDialog({
  channel,
  onClose,
  onDisconnected
}) {
  const t = useT();
  const [submitting, setSubmitting] = React.useState(false);
  const { runMutation, retryLastMutation } = useGuardedMutation({
    contextId: DISCONNECT_MUTATION_CONTEXT_ID,
    blockedMessage: t("ui.forms.flash.saveBlocked", "Save blocked by validation")
  });
  React.useEffect(() => {
    if (channel) setSubmitting(false);
  }, [channel?.id]);
  const handleConfirm = React.useCallback(async () => {
    if (!channel || submitting) return;
    setSubmitting(true);
    let response;
    try {
      response = await runMutation({
        // optimistic-lock-exempt: self-service connect/disconnect of the
        // signed-in operator's OWN communication channel (an integration link),
        // not a shared multi-editor record. Disconnect is a terminal action
        // keyed by channel id; there is no concurrent-edit lost-update window to
        // guard, and the row carries no client-surfaced `updatedAt` round-trip.
        operation: () => apiCall(
          `/api/communication_channels/channels/${encodeURIComponent(channel.id)}`,
          { method: "DELETE" }
        ),
        context: {
          formId: DISCONNECT_MUTATION_CONTEXT_ID,
          resourceKind: "communication_channels.channel",
          resourceId: channel.id,
          retryLastMutation
        },
        mutationPayload: { action: "disconnect" }
      });
    } catch (err) {
      setSubmitting(false);
      flash(
        err instanceof Error ? err.message : t("communication_channels.profile.actions.disconnectFailed", "Failed to disconnect channel"),
        "error"
      );
      return;
    }
    setSubmitting(false);
    if (!response.ok) {
      const body = response.result;
      flash(
        body?.error ?? t("communication_channels.profile.actions.disconnectFailed", "Failed to disconnect channel"),
        "error"
      );
      return;
    }
    flash(
      t(
        "communication_channels.profile.actions.disconnectSuccess",
        "Channel disconnected. You can reconnect it anytime."
      ),
      "success"
    );
    onDisconnected();
  }, [channel, submitting, runMutation, retryLastMutation, t, onDisconnected]);
  const handleKeyDown = React.useCallback(
    (event) => {
      if (event.key === "Enter" && (event.metaKey || event.ctrlKey)) {
        event.preventDefault();
        void handleConfirm();
      }
    },
    [handleConfirm]
  );
  return /* @__PURE__ */ jsx(Dialog, { open: channel !== null, onOpenChange: (open) => {
    if (!open) onClose();
  }, children: /* @__PURE__ */ jsxs(DialogContent, { onKeyDown: handleKeyDown, children: [
    /* @__PURE__ */ jsxs(DialogHeader, { children: [
      /* @__PURE__ */ jsx(DialogTitle, { children: t("communication_channels.profile.disconnect.title", "Disconnect channel") }),
      /* @__PURE__ */ jsx(DialogDescription, { children: t(
        "communication_channels.profile.disconnect.description",
        "This removes the connection and stops syncing. Emails already imported stay on your timelines. You can reconnect anytime."
      ) })
    ] }),
    channel ? /* @__PURE__ */ jsx("p", { className: "text-sm font-medium", children: channel.externalIdentifier ?? channel.displayName }) : null,
    /* @__PURE__ */ jsxs(DialogFooter, { children: [
      /* @__PURE__ */ jsx("span", { className: "mr-auto text-xs text-muted-foreground", children: /* @__PURE__ */ jsx(KbdShortcut, { keys: ["\u2318", "Enter"] }) }),
      /* @__PURE__ */ jsx(Button, { type: "button", variant: "outline", onClick: onClose, disabled: submitting, children: t("communication_channels.profile.disconnect.cancel", "Cancel") }),
      /* @__PURE__ */ jsx(Button, { type: "button", variant: "destructive", onClick: () => void handleConfirm(), disabled: submitting, children: submitting ? t("communication_channels.profile.disconnect.submitting", "Disconnecting\u2026") : t("communication_channels.profile.disconnect.confirm", "Disconnect") })
    ] })
  ] }) });
}
function statusTag(status, t) {
  switch (status) {
    case "connected":
      return /* @__PURE__ */ jsx(Tag, { variant: "success", dot: true, children: t("communication_channels.status.connected", "Connected") });
    case "requires_reauth":
      return /* @__PURE__ */ jsx(Tag, { variant: "warning", dot: true, children: t("communication_channels.status.requiresReauth", "Needs reconnection") });
    case "error":
      return /* @__PURE__ */ jsx(Tag, { variant: "error", dot: true, children: t("communication_channels.status.error", "Error") });
    case "disconnected":
      return /* @__PURE__ */ jsx(Tag, { variant: "neutral", children: t("communication_channels.status.disconnected", "Disconnected") });
    default:
      return /* @__PURE__ */ jsx(Tag, { variant: "neutral", children: status });
  }
}
export {
  ProfileCommunicationChannelsPage as default
};
//# sourceMappingURL=page.js.map
