"use client";
'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

const jsxRuntime = require('react/jsx-runtime');
const React = require('react');
const blogHooks = require('../../hooks/blog-hooks.cjs');
const utils = require('../../../utils.cjs');
const highlightText = require('./highlight-text.cjs');
const searchModal = require('./search-modal.cjs');
const context = require('@btst/stack/context');

function _interopNamespaceCompat(e) {
	if (e && typeof e === 'object' && 'default' in e) return e;
	const n = Object.create(null);
	if (e) {
		for (const k in e) {
			n[k] = e[k];
		}
	}
	n.default = e;
	return n;
}

const React__namespace = /*#__PURE__*/_interopNamespaceCompat(React);

const renderBlogResult = (item, index, query) => {
  const q = (query || "").toLowerCase();
  const excerptMatches = item.processedExcerpt ? item.processedExcerpt.toLowerCase().includes(q) : false;
  const contentMatches = item.processedContent ? item.processedContent.toLowerCase().includes(q) : false;
  return /* @__PURE__ */ jsxRuntime.jsxs(
    "button",
    {
      "data-testid": "search-result",
      type: "button",
      className: "flex w-full cursor-pointer flex-col gap-2 rounded-sm border-border border-b px-4 py-3 text-left transition-colors hover:bg-accent",
      onClick: () => item.onClick?.(),
      children: [
        /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-start justify-between gap-2", children: [
          /* @__PURE__ */ jsxRuntime.jsx(
            highlightText.HighlightText,
            {
              text: item.title,
              searchQuery: query,
              className: "flex-1 font-medium text-sm leading-5"
            }
          ),
          item.publishedAt && /* @__PURE__ */ jsxRuntime.jsx("span", { className: "whitespace-nowrap text-muted-foreground text-xs", children: new Date(item.publishedAt).toLocaleDateString() })
        ] }),
        excerptMatches && /* @__PURE__ */ jsxRuntime.jsx(
          highlightText.HighlightText,
          {
            text: item.processedExcerpt || "",
            searchQuery: query,
            className: "text-muted-foreground text-xs leading-4",
            maxLength: 120
          }
        ),
        contentMatches && /* @__PURE__ */ jsxRuntime.jsx(
          highlightText.HighlightText,
          {
            text: item.processedContent,
            searchQuery: query,
            className: "text-muted-foreground text-xs leading-4",
            maxLength: 120
          }
        ),
        !excerptMatches && !contentMatches && /* @__PURE__ */ jsxRuntime.jsx(
          highlightText.HighlightText,
          {
            text: item.processedExcerpt || item.processedContent,
            searchQuery: query,
            className: "text-muted-foreground text-xs leading-4",
            maxLength: 120
          }
        )
      ]
    },
    item.id
  );
};
function SearchInput({
  className,
  triggerClassName,
  placeholder,
  buttonText,
  emptyMessage
}) {
  const { navigate } = context.usePluginOverrides("blog");
  const basePath = context.useBasePath();
  const [currentQuery, setCurrentQuery] = React__namespace.useState("");
  const { data: searchResults = [], isLoading } = blogHooks.usePostSearch({
    query: currentQuery,
    enabled: currentQuery.trim().length > 0,
    debounceMs: 300,
    published: true
  });
  const formattedResults = React__namespace.useMemo(() => {
    return searchResults.map((post) => ({
      id: post.id,
      title: post.title,
      slug: post.slug,
      publishedAt: post.publishedAt,
      authorName: "",
      processedContent: utils.stripMarkdown(utils.stripHtml(post.content || "")),
      processedExcerpt: utils.stripMarkdown(utils.stripHtml(post.excerpt || "")),
      onClick: () => navigate(`${basePath}/blog/${post.slug}`)
    }));
  }, [searchResults, navigate, basePath]);
  const handleSearch = React__namespace.useCallback(
    (query) => {
      setCurrentQuery(query);
      return [];
    },
    []
  );
  return /* @__PURE__ */ jsxRuntime.jsx(
    searchModal.SearchModal,
    {
      placeholder,
      buttonText,
      emptyMessage,
      searchFn: handleSearch,
      renderResult: renderBlogResult,
      results: formattedResults,
      isLoading,
      className,
      triggerClassName,
      keyboardShortcut: "\u2318K"
    }
  );
}

exports.SearchInput = SearchInput;
exports.default = SearchInput;
