"use client";
import { jsx, jsxs } from "react/jsx-runtime";
import * as React from "react";
import { Sparkles } from "lucide-react";
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";
import { hasFeature } from "@open-mercato/shared/security/features";
const PORTAL_AI_INJECT_AGENT_ID = "customers.account_assistant";
const PORTAL_AI_INJECT_REQUIRED_FEATURE = "portal.account.manage";
function readUserId(user) {
  if (!user) return null;
  const id = user.id;
  return typeof id === "string" && id.length > 0 ? id : null;
}
function PortalAiAssistantTriggerWidget({ context }) {
  const t = useT();
  const [open, setOpen] = React.useState(false);
  const resolvedFeatures = Array.isArray(context?.resolvedFeatures) ? context?.resolvedFeatures : [];
  const featureAllowed = context?.isPortalAdmin === true || hasFeature(resolvedFeatures, PORTAL_AI_INJECT_REQUIRED_FEATURE);
  const pageContext = React.useMemo(() => ({
    view: "portal.profile",
    recordType: "customer",
    recordId: readUserId(context?.user ?? null),
    extra: {}
  }), [context?.user]);
  if (!featureAllowed) return null;
  return /* @__PURE__ */ jsxs("div", { className: "mt-6", "data-ai-portal-inject-wrapper": "", children: [
    /* @__PURE__ */ jsxs(
      Button,
      {
        type: "button",
        variant: "outline",
        size: "sm",
        onClick: () => setOpen(true),
        "data-ai-portal-inject-trigger": "",
        "aria-label": t(
          "customer_accounts.portal_ai_assistant.trigger.ariaLabel",
          "Open portal AI assistant"
        ),
        children: [
          /* @__PURE__ */ jsx(Sparkles, { className: "size-4", "aria-hidden": true }),
          /* @__PURE__ */ jsx("span", { children: t("customer_accounts.portal_ai_assistant.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"
        ),
        "data-ai-portal-inject-sheet": "",
        children: [
          /* @__PURE__ */ jsxs(DialogHeader, { children: [
            /* @__PURE__ */ jsx(DialogTitle, { children: t("customer_accounts.portal_ai_assistant.sheet.title", "Portal AI assistant") }),
            /* @__PURE__ */ jsx(DialogDescription, { children: t(
              "customer_accounts.portal_ai_assistant.sheet.description",
              "Read-only assistant for portal customers. Ask about your account and recent activity."
            ) })
          ] }),
          /* @__PURE__ */ jsx("div", { className: "min-h-0 flex-1", "data-ai-portal-inject-chat-container": "", children: /* @__PURE__ */ jsx(
            AiChat,
            {
              agent: PORTAL_AI_INJECT_AGENT_ID,
              pageContext,
              className: "h-full",
              placeholder: t(
                "customer_accounts.portal_ai_assistant.sheet.composerPlaceholder",
                "Ask about your account..."
              )
            }
          ) })
        ]
      }
    ) })
  ] });
}
export {
  PORTAL_AI_INJECT_AGENT_ID,
  PORTAL_AI_INJECT_REQUIRED_FEATURE,
  PortalAiAssistantTriggerWidget as default
};
//# sourceMappingURL=widget.client.js.map
