/**
 * Query renderer factory for ::query{type="TODO"} / ::query{tag="project"}.
 *
 * Returns a BlockDecoration with:
 * - nodeWrapper for the styled outer container (md-query-block)
 * - decorationRanges covering the full node range (hidden when blurred)
 * - wrapperAttrs storing the raw query attrs for the widget factory
 */

import { findChildByTypeName } from "@enesis/editor"
import { parseAttrs } from "@enesis/lezer-comark"
import type { SyntaxNode } from "@lezer/common"

export function createQueryRenderer() {
  return (node: SyntaxNode, ctx: { doc: string }) => {
    const attrsNode = findChildByTypeName(node, "ComponentAttrs")
    if (!attrsNode) return null

    const rawAttrs = ctx.doc.slice(attrsNode.from, attrsNode.to)
    const parsed = parseAttrs(rawAttrs)
    return {
      type: "query",
      // Single range covering the full block for nodeWrapper positioning.
      // Hiding the source when blurred is handled by applyQueryDecorations.
      decorationRanges: [
        { type: "content", from: node.from, to: node.to },
      ],
      wrapperAttrs: { "data-query": rawAttrs },
      nodeWrapper: {
        className: "md-query-block",
        attrs: { "data-query-attrs": rawAttrs },
      },
    }
  }
}
