import React from 'react';
import { Highlight, truncateAroundMatch } from './Highlight';
export function Hit({ hit, isSelected, onClick, onMouseEnter, openInNewTab, isChild, isLastChild, hierarchyType }) {
    const isFullHit = 'objectID' in hit;
    const suggestion = hit;
    const hitType = hierarchyType || suggestion.type;
    const breadcrumb = suggestion.parentTitle
        ? `${suggestion.category || ''} › ${suggestion.parentTitle}`.replace(/^› /, '')
        : suggestion.category || '';
    const title = getTitleForType(hit, hitType);
    let highlightedTitle = title;
    let highlightedContent = hit.content || suggestion.description || '';
    if (isFullHit) {
        const fullHit = hit;
        if (fullHit._highlightResult) {
            highlightedTitle = fullHit._highlightResult.title?.value || title;
            highlightedContent = fullHit._highlightResult.content?.value || hit.content || '';
        }
    }
    else {
        if (suggestion.highlight) {
            highlightedTitle = suggestion.highlight.title || title;
            highlightedContent = suggestion.highlight.content || hit.content || suggestion.description || '';
        }
    }
    const displayContent = highlightedContent ? truncateAroundMatch(highlightedContent, 120) : '';
    const url = hit.url || suggestion.route || '#';
    const classNames = ['seekora-docsearch-hit'];
    if (isSelected)
        classNames.push('seekora-docsearch-hit--selected');
    if (isChild)
        classNames.push('seekora-docsearch-hit--child');
    if (isLastChild)
        classNames.push('seekora-docsearch-hit--last-child');
    return (React.createElement("a", { href: url, className: classNames.join(' '), onClick: (e) => { e.preventDefault(); onClick(); }, onMouseEnter: onMouseEnter, role: "option", "aria-selected": isSelected, target: openInNewTab ? '_blank' : undefined, rel: openInNewTab ? 'noopener noreferrer' : undefined },
        isChild && (React.createElement("div", { className: "seekora-docsearch-hit-tree" },
            React.createElement(TreeConnector, { isLast: isLastChild }))),
        React.createElement("div", { className: "seekora-docsearch-hit-icon" },
            React.createElement(HitIcon, { type: getHitTypeFromLevel(hitType) })),
        React.createElement("div", { className: "seekora-docsearch-hit-content" },
            !isChild && breadcrumb && React.createElement("span", { className: "seekora-docsearch-hit-breadcrumb" }, breadcrumb),
            React.createElement("span", { className: "seekora-docsearch-hit-title" },
                React.createElement(Highlight, { value: title, highlightedValue: highlightedTitle })),
            displayContent && (React.createElement("span", { className: "seekora-docsearch-hit-description" },
                React.createElement(Highlight, { value: hit.content || '', highlightedValue: displayContent })))),
        React.createElement("div", { className: "seekora-docsearch-hit-action" }, openInNewTab ? (React.createElement("svg", { width: "16", height: "16", viewBox: "0 0 16 16", fill: "none", "aria-hidden": "true" },
            React.createElement("path", { d: "M6 3H3v10h10v-3M9 3h4v4M14 2L7 9", stroke: "currentColor", strokeWidth: "1.5", strokeLinecap: "round", strokeLinejoin: "round" }))) : (React.createElement("svg", { width: "16", height: "16", viewBox: "0 0 16 16", fill: "none", "aria-hidden": "true" },
            React.createElement("path", { d: "M6.75 3.25L11.5 8L6.75 12.75", stroke: "currentColor", strokeWidth: "1.5", strokeLinecap: "round", strokeLinejoin: "round" }))))));
}
function getTitleForType(hit, type) {
    const hierarchy = hit.hierarchy || {};
    if (!type)
        return hit.title || hierarchy.lvl1 || hierarchy.lvl0 || 'Untitled';
    const match = type.match(/^lvl(\d+)$/);
    if (match) {
        const level = parseInt(match[1], 10);
        const levelKey = `lvl${level}`;
        const levelTitle = hierarchy[levelKey];
        if (levelTitle)
            return levelTitle;
    }
    return hit.title || hierarchy.lvl1 || hierarchy.lvl0 || 'Untitled';
}
function getHitTypeFromLevel(type) {
    if (!type)
        return 'page';
    const match = type.match(/^lvl(\d+)$/);
    if (match) {
        const level = parseInt(match[1], 10);
        if (level === 1)
            return 'page';
        if (level <= 3)
            return 'section';
        return 'content';
    }
    return 'page';
}
function HitIcon({ type }) {
    switch (type) {
        case 'page':
            return (React.createElement("svg", { width: "20", height: "20", viewBox: "0 0 20 20", fill: "none", "aria-hidden": "true" },
                React.createElement("path", { d: "M4.5 3.5h11a1 1 0 011 1v11a1 1 0 01-1 1h-11a1 1 0 01-1-1v-11a1 1 0 011-1z", stroke: "currentColor", strokeWidth: "1.5", fill: "none" }),
                React.createElement("path", { d: "M7 7h6M7 10h6M7 13h4", stroke: "currentColor", strokeWidth: "1.5", strokeLinecap: "round" })));
        case 'section':
            return (React.createElement("svg", { width: "20", height: "20", viewBox: "0 0 20 20", fill: "none", "aria-hidden": "true" },
                React.createElement("path", { d: "M4 5.5h12M4 10h12M4 14.5h8", stroke: "currentColor", strokeWidth: "1.5", strokeLinecap: "round" })));
        case 'content':
            return (React.createElement("svg", { width: "20", height: "20", viewBox: "0 0 20 20", fill: "none", "aria-hidden": "true" },
                React.createElement("path", { d: "M4 6h12M4 10h8M4 14h10", stroke: "currentColor", strokeWidth: "1.5", strokeLinecap: "round" })));
    }
}
function TreeConnector({ isLast }) {
    return (React.createElement("svg", { width: "16", height: "20", viewBox: "0 0 16 20", fill: "none", "aria-hidden": "true", className: "seekora-docsearch-hit-tree-icon" }, isLast ? (React.createElement("path", { d: "M8 0V10H14", stroke: "currentColor", strokeWidth: "1.5", strokeLinecap: "round", strokeLinejoin: "round" })) : (React.createElement(React.Fragment, null,
        React.createElement("path", { d: "M8 0V20", stroke: "currentColor", strokeWidth: "1.5" }),
        React.createElement("path", { d: "M8 10H14", stroke: "currentColor", strokeWidth: "1.5", strokeLinecap: "round" })))));
}
