import { Marker } from "/vendor/maplibre-gl/maplibre-gl.mjs";
import { SEARCH_ICON } from "/js/icons.js";
import { escapeHtml } from "/js/gpx.js";
const NOMINATIM_API = "https://nominatim.openstreetmap.org/search";
// "lat, lng" / "lat lng" decimal degrees — the same plain-text shape the
// topbar's own coordinate readout copies to the clipboard (map.js). Tried
// before hitting the geocoder, so pasting a coordinate pair jumps straight
// there with no network round-trip.
const COORD_PATTERN = /^\s*(-?\d+(?:\.\d+)?)\s*[, ]\s*(-?\d+(?:\.\d+)?)\s*$/;
// Sources the search box can look through, all ticked by default. "local"
// ones are answered by GET /api/search (every saved layer, shown or not,
// plus offline hiking trails — works with no uplink); "online" ones are
// queried straight from the browser and just come back empty when there's
// no connection. A new provider is one more entry here (plus, for a local
// one, a key in lib/maptool/search.rb's SOURCES).
const SOURCES = [
{ id: "layers", label: "My layers", local: true },
{ id: "hiking_trails", label: "Hiking trails", local: true },
{ id: "nominatim", label: "Online (Nominatim)", local: false },
];
// A place-name/coordinate search box, matching NPS's own Park Tiles viewer.
// Every result says where it was found; picking one from a layer that's
// currently switched off switches it on (see setLayerOpeners).
export class SearchControl {
constructor() {
this._results = [];
this._enabled = new Set(SOURCES.map((source) => source.id));
this._openers = {};
}
// { layers: (path) => ..., hiking_trails: (code) => ... } — set from
// map.js once the panels owning those layers exist.
setLayerOpeners(openers) {
this._openers = openers;
}
onAdd(map) {
this._map = map;
this._container = document.createElement("div");
this._container.className = "maplibregl-ctrl maplibregl-ctrl-group search-ctrl";
this._container.innerHTML = `