package main

import (
	"log"
	"time"

	"github.com/kami/maven/internal/config"
	"github.com/kami/maven/internal/websearch"
)

// searchWiring — the metasearch source, assembled. nil ⇒ off, which is the
// default: no `search` block, no query ever leaves the LAN.
//
// Thinner than kiwixWiring because there is nothing to rewrite. SearXNG ranks
// with real engines, so the question goes out as he asked it, and that is the
// reason this source sits ahead of the ZIMs rather than behind them.
type searchWiring struct {
	client *websearch.Client
	max    int
	runes  int
}

// wireSearch builds the search client from the `search` block, or returns nil
// when there is none. config.Normalise has already dropped a block with no URL
// and filled the two size defaults, so this does no validation of its own.
func wireSearch(cfg *config.Config) *searchWiring {
	if cfg.Search == nil {
		return nil
	}
	sc := cfg.Search
	log.Printf("voice: web search at %s (language %q, engines %q)", sc.URL, sc.Language, sc.Engines)
	return &searchWiring{
		client: websearch.New(sc.URL, websearch.Options{
			Language: sc.Language,
			Engines:  sc.Engines,
			Timeout:  time.Duration(sc.Timeout),
		}),
		max:   sc.MaxResults,
		runes: sc.SnippetRunes,
	}
}
