import { w as publicSettings, $ as $fetch, a0 as addressToString, u as useLogger, d as defineEventHandler, c as createError, n as getValidatedQuery } from '../../nitro/nitro.mjs';
import { z } from 'zod';
import 'luxon';
import '@sequelize/core';
import 'html-to-text';
import 'crypto';
import 'node:fs';
import 'sharp';
import 'path';
import 'node:stream';
import 'jsdom';
import 'dompurify';
import 'mime-types';
import 'fs';
import 'fs/promises';
import 'linkify-html';
import '@peertube/http-signature';
import 'jsonld';
import 'lodash.intersection';
import 'email-templates';
import 'node:http';
import 'node:https';
import 'node:crypto';
import 'node:events';
import 'node:buffer';
import 'node:path';
import 'vue';
import 'vue-router';
import 'dotenv';
import 'url';
import 'bcryptjs';
import 'slugify';
import 'umzug';
import 'consola';
import '@sequelize/core/decorators-legacy';
import 'util';
import 'mitt';
import 'ical.js';
import 'node:url';
import 'fast-xml-parser';

const log = useLogger("geocode:nominatim");
function mapNominatimToPostalAddress(place) {
  if (!place.address) return null;
  const a = place.address;
  const street = [a.house_number, a.road || a.pedestrian || a.footway].filter(Boolean).join(" ") || void 0;
  const locality = a.city || a.town || a.village || a.suburb || void 0;
  const region = a.state || a.county || void 0;
  const country = a.country || a.country_code?.toUpperCase();
  return {
    type: "PostalAddress",
    streetAddress: street,
    addressLocality: locality,
    addressRegion: region,
    postalCode: a.postcode,
    addressCountry: country
  };
}
function getIcon(item) {
  if (["place", "amenity", "shop", "tourism", "leisure", "building"].includes(item.class)) {
    return "mdi-home";
  }
  const type = item.osm_type;
  const iconMap = {
    way: "mdi-road-variant",
    house: "mdi-home",
    node: "mdi-map-marker",
    relation: "mdi-city-variant"
  };
  return type in iconMap ? iconMap[type] : "mdi-home";
}
async function queryNominatim(q) {
  const endpoint = publicSettings.geocoding_provider || "https://nominatim.openstreetmap.org/search";
  log.info("Fetching details for", q);
  const countrycodes = publicSettings.geocoding_countrycodes || [];
  const params = {
    countrycodes: countrycodes.join(","),
    q,
    limit: 3,
    format: "json",
    addressdetails: 1,
    namedetails: 1
  };
  const addressList = await $fetch(endpoint, {
    params,
    headers: { "User-Agent": `gancio` }
  });
  return addressList?.map((v) => {
    const name = v?.namedetails?.alt_name ?? v?.namedetails?.name;
    const address = mapNominatimToPostalAddress(v);
    return {
      icon: getIcon(v),
      lat: v.lat,
      lon: v.lon,
      address,
      display_name: addressToString(address || ""),
      name
    };
  });
}

const schema = z.object({
  q: z.string()
});
const index = defineEventHandler(async (event) => {
  if (!publicSettings.allow_geolocation) {
    throw createError({ statusCode: 403, message: "Geocoding is not enabled" });
  }
  const { q } = await getValidatedQuery(event, schema.parse);
  return queryNominatim(q);
});

export { index as default };
