"use client";
import { jsx, jsxs } from "react/jsx-runtime";
import * as React from "react";
import { useT } from "@open-mercato/shared/lib/i18n/context";
import { Button } from "../../primitives/button.js";
import { Input } from "../../primitives/input.js";
import { Label } from "../../primitives/label.js";
import { getMessageUiComponentRegistry } from "@open-mercato/core/modules/messages/components/utils/typeUiRegistry";
import { getMessageObjectType } from "@open-mercato/core/modules/messages/lib/message-objects-registry";
function MessageObjectRecordPicker({
  search,
  onSearchChange,
  selectedId,
  onSelectedIdChange,
  items,
  isLoading,
  error,
  onRetry,
  entityModule,
  entityType
}) {
  const t = useT();
  const messageUiRegistry = getMessageUiComponentRegistry();
  const itemComponents = React.useMemo(() => {
    return items.map((item) => {
      const resolvedModule = item.entityModule || entityModule;
      const resolvedEntityType = item.entityType || entityType;
      const componentKey = resolvedModule && resolvedEntityType ? `${resolvedModule}:${resolvedEntityType}` : null;
      const PreviewComponent = (componentKey ? messageUiRegistry.objectPreviewComponents[componentKey] : null) ?? messageUiRegistry.objectPreviewComponents["messages:default"] ?? null;
      const objectType = resolvedModule && resolvedEntityType ? getMessageObjectType(resolvedModule, resolvedEntityType) : null;
      return {
        item,
        PreviewComponent,
        objectType,
        resolvedModule,
        resolvedEntityType
      };
    });
  }, [items, entityModule, entityType, messageUiRegistry]);
  return /* @__PURE__ */ jsxs("div", { className: "space-y-2", children: [
    /* @__PURE__ */ jsx(Label, { htmlFor: "messages-object-record-search", children: t("messages.composer.objectPicker.recordSearchLabel", "Search records") }),
    /* @__PURE__ */ jsx(
      Input,
      {
        id: "messages-object-record-search",
        value: search,
        onChange: (event) => onSearchChange(event.target.value),
        placeholder: t("messages.composer.objectPicker.recordSearchPlaceholder", "Type to find a record")
      }
    ),
    isLoading ? /* @__PURE__ */ jsx("p", { className: "text-xs text-muted-foreground", children: t("messages.composer.objectPicker.loadingRecords", "Loading records...") }) : null,
    error ? /* @__PURE__ */ jsxs("div", { className: "space-y-2 rounded border border-destructive/40 bg-destructive/5 p-2 text-xs text-destructive", children: [
      /* @__PURE__ */ jsx("p", { children: error }),
      /* @__PURE__ */ jsx(Button, { type: "button", size: "sm", variant: "outline", onClick: onRetry, children: t("common.retry", "Retry") })
    ] }) : null,
    /* @__PURE__ */ jsxs("div", { className: "space-y-2", children: [
      /* @__PURE__ */ jsx(Label, { children: t("messages.composer.objectPicker.recordLabel", "Record") }),
      !selectedId && /* @__PURE__ */ jsx("p", { className: "text-sm text-muted-foreground", children: t("messages.composer.objectPicker.recordPlaceholder", "Select record") }),
      /* @__PURE__ */ jsx("div", { className: "space-y-2 max-h-64 overflow-y-auto", children: itemComponents.map(({ item, PreviewComponent, objectType, resolvedModule, resolvedEntityType }) => /* @__PURE__ */ jsx(
        "div",
        {
          className: `cursor-pointer rounded-md border p-2 transition-colors ${selectedId === item.id ? "border-primary bg-primary/5" : "border-border hover:bg-muted/50"}`,
          onClick: () => onSelectedIdChange(item.id),
          onKeyDown: (e) => {
            if (e.key === "Enter" || e.key === " ") {
              e.preventDefault();
              onSelectedIdChange(item.id);
            }
          },
          role: "button",
          tabIndex: 0,
          children: PreviewComponent ? /* @__PURE__ */ jsx(
            PreviewComponent,
            {
              entityId: item.id,
              entityModule: item.entityModule || entityModule || "",
              entityType: item.entityType || entityType || "",
              snapshot: item.snapshot,
              previewData: {
                title: item.label,
                subtitle: item.subtitle || void 0
              },
              icon: objectType?.icon
            }
          ) : /* @__PURE__ */ jsxs("div", { className: "text-sm", children: [
            /* @__PURE__ */ jsx("p", { className: "font-medium", children: item.label }),
            item.subtitle && /* @__PURE__ */ jsx("p", { className: "text-muted-foreground text-xs", children: item.subtitle })
          ] })
        },
        item.id
      )) })
    ] }),
    !isLoading && !error && items.length === 0 ? /* @__PURE__ */ jsx("p", { className: "text-xs text-muted-foreground", children: t("messages.composer.objectPicker.noRecords", "No records found for this object type.") }) : null
  ] });
}
export {
  MessageObjectRecordPicker
};
//# sourceMappingURL=MessageObjectRecordPicker.js.map
