// @wuys/map · integrations/mount.js
//
// One-liner mount for hosts that already preload `window.WUYS_WORLD_TOPO` and
// `window.WUYS_FLAGS`. Returns a fully-wired map.
//
//   import { mountMap } from "@wuys/map/integrations/mount.js";
//   const map = await mountMap("#geo-map", { theme: "antique" });
//
// Use this when a host page wants to drop the new map into an existing layout
// and keep its own controls bar (overlays default off).

import { createMap, attachOverlays, themes } from "../index.js";

export async function mountMap(target, opts = {}) {
  const el = typeof target === "string" ? document.querySelector(target) : target;
  if (!el) throw new Error("[wuys/map] mountMap: target not found: " + target);

  // Accept theme as a string id OR an object. createMap wants the object.
  let themeObj = opts.theme;
  if (typeof themeObj === "string") themeObj = themes[themeObj] || themes.vector;
  else if (!themeObj) themeObj = themes.vector;

  // If the host owns the URL hash already, default hashSync to false so we
  // don't fight over `location.hash`.
  const map = await createMap({
    target: el,
    view: opts.view ?? "flat",
    theme: themeObj,
    renderer: opts.renderer ?? "svg",
    flagsVisible:  opts.flagsVisible  !== false,
    labelsVisible: opts.labelsVisible === true,
    layers: opts.layers,
    getCountryName: opts.getCountryName,
    data: opts.data,
    position: opts.position,
    hashSync: opts.hashSync === true,
  });

  if (opts.overlays !== false) {
    attachOverlays({
      map, target: el,
      ui: opts.ui ?? { view: true, themes: true, flags: true, search: true, help: true, perf: false, rendererBadge: true },
    });
  }

  return map;
}
