"use client";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
import * as React from "react";
import { AiIcon } from "@open-mercato/ui/ai/AiIcon";
import { AiChat } from "@open-mercato/ui/ai/AiChat";
import { Button } from "@open-mercato/ui/primitives/button";
import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogHeader,
  DialogTitle
} from "@open-mercato/ui/primitives/dialog";
import { useT } from "@open-mercato/shared/lib/i18n/context";
import { cn } from "@open-mercato/shared/lib/utils";
const CUSTOMERS_AI_DEAL_DETAIL_AGENT_ID = "customers.account_assistant";
function readString(value) {
  return typeof value === "string" && value.length > 0 ? value : null;
}
function buildPageContext(context, data) {
  const dealRecord = data?.deal ?? context?.data?.deal;
  const dealId = readString(context?.dealId) ?? readString(context?.recordId) ?? readString(dealRecord?.id) ?? null;
  if (!dealId) return null;
  const stage = readString(context?.stage) ?? readString(dealRecord?.status) ?? readString(dealRecord?.pipelineStage) ?? null;
  const pipelineStageId = readString(context?.pipelineStageId) ?? readString(dealRecord?.pipelineStageId) ?? null;
  return {
    view: "customers.deal.detail",
    recordType: "deal",
    recordId: dealId,
    extra: { stage, pipelineStageId }
  };
}
function computeCustomersAiDealDetailPageContext(context, data) {
  return buildPageContext(context, data);
}
function AiDealDetailTriggerWidget({ context, data }) {
  const t = useT();
  const [open, setOpen] = React.useState(false);
  const pageContext = React.useMemo(() => buildPageContext(context, data), [context, data]);
  const handleClick = React.useCallback(() => {
    setOpen(true);
  }, []);
  if (!pageContext) return null;
  return /* @__PURE__ */ jsxs(Fragment, { children: [
    /* @__PURE__ */ jsxs(
      Button,
      {
        type: "button",
        variant: "outline",
        size: "sm",
        onClick: handleClick,
        "data-ai-customers-deal-trigger": "",
        "data-ai-customers-deal-id": pageContext.recordId,
        "aria-label": t(
          "customers.ai_assistant.dealDetail.trigger.ariaLabel",
          "Open AI assistant for this deal"
        ),
        children: [
          /* @__PURE__ */ jsx(AiIcon, { className: "size-4" }),
          /* @__PURE__ */ jsx("span", { children: t("customers.ai_assistant.dealDetail.trigger.label", "Ask AI") })
        ]
      }
    ),
    /* @__PURE__ */ jsx(Dialog, { open, onOpenChange: setOpen, children: /* @__PURE__ */ jsxs(
      DialogContent,
      {
        className: cn(
          "sm:max-w-xl sm:top-0 sm:bottom-0 sm:right-0 sm:left-auto sm:translate-x-0 sm:translate-y-0",
          "sm:h-screen sm:max-h-screen sm:rounded-none sm:rounded-l-2xl",
          "flex flex-col gap-3 p-4 z-[70]"
        ),
        "data-ai-customers-deal-sheet": "",
        children: [
          /* @__PURE__ */ jsxs(DialogHeader, { children: [
            /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between gap-3", children: [
              /* @__PURE__ */ jsx(DialogTitle, { children: t(
                "customers.ai_assistant.dealDetail.sheet.title",
                "Customers AI assistant \u2014 deal"
              ) }),
              pageContext.extra.stage ? /* @__PURE__ */ jsx(
                "span",
                {
                  className: "inline-flex items-center rounded-full border border-border bg-secondary px-2 py-0.5 text-xs text-secondary-foreground",
                  "data-ai-customers-deal-stage-pill": "",
                  "data-ai-customers-deal-stage": pageContext.extra.stage,
                  children: pageContext.extra.stage
                }
              ) : null
            ] }),
            /* @__PURE__ */ jsx(DialogDescription, { children: t(
              "customers.ai_assistant.dealDetail.sheet.description",
              "Ask about this deal. With the per-tenant mutation-policy override enabled, the assistant can also propose a stage change that you confirm before anything is saved."
            ) })
          ] }),
          /* @__PURE__ */ jsx("div", { className: "min-h-0 flex-1", "data-ai-customers-deal-chat-container": "", children: /* @__PURE__ */ jsx(
            AiChat,
            {
              agent: CUSTOMERS_AI_DEAL_DETAIL_AGENT_ID,
              pageContext,
              className: "h-full",
              placeholder: t(
                "customers.ai_assistant.dealDetail.sheet.composerPlaceholder",
                "Ask about this deal, the stage, pipeline..."
              )
            }
          ) })
        ]
      }
    ) })
  ] });
}
export {
  CUSTOMERS_AI_DEAL_DETAIL_AGENT_ID,
  computeCustomersAiDealDetailPageContext,
  AiDealDetailTriggerWidget as default
};
//# sourceMappingURL=widget.client.js.map
