"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var qdrant_dashboard_exports = {};
__export(qdrant_dashboard_exports, {
  ensureQdrantUI: () => ensureQdrantUI,
  openQdrantDashboard: () => openQdrantDashboard
});
module.exports = __toCommonJS(qdrant_dashboard_exports);
var path = __toESM(require("path"));
var fs = __toESM(require("fs"));
var import_child_process = require("child_process");
var import_ui = require("../ui");
var import_qdrant_index = require("../../vectors/qdrant-index");
var import_archive = require("./archive");
const SERVER_STATE_FILE = "qdrant-server.json";
const UI_RELEASES_API = "https://api.github.com/repos/qdrant/qdrant-web-ui/releases/latest";
async function downloadQdrantUI(cacheDir) {
  const release = await (0, import_archive.fetchJson)(UI_RELEASES_API);
  const asset = release.assets.find((a) => a.name === "dist-qdrant.zip");
  if (!asset) throw new Error("dist-qdrant.zip not found in latest release");
  const tmpZip = path.join(cacheDir, "..", "_qdrant-ui.zip");
  fs.mkdirSync(path.dirname(tmpZip), { recursive: true });
  await (0, import_archive.downloadFile)(asset.browser_download_url, tmpZip);
  const tmpDir = path.join(cacheDir, "..", "_qdrant-ui-extract");
  fs.mkdirSync(tmpDir, { recursive: true });
  await (0, import_archive.extractZip)(tmpZip, tmpDir);
  const distDir = path.join(tmpDir, "dist");
  if (!fs.existsSync(distDir)) throw new Error("dist/ not found in zip");
  fs.rmSync(cacheDir, { recursive: true, force: true });
  fs.renameSync(distDir, cacheDir);
  try {
    fs.unlinkSync(tmpZip);
  } catch {
  }
  try {
    fs.rmSync(tmpDir, { recursive: true, force: true });
  } catch {
  }
}
function openBrowser(url) {
  const cmd = process.platform === "darwin" ? "open" : process.platform === "win32" ? "start" : "xdg-open";
  try {
    (0, import_child_process.spawn)(cmd, [url], { stdio: "ignore", detached: true, shell: process.platform === "win32" }).unref();
  } catch {
  }
}
async function ensureQdrantUI(projectRoot) {
  const cacheDir = path.join(projectRoot, ".kirograph", import_qdrant_index.DASHBOARD_SUBDIR);
  const indexHtml = path.join(cacheDir, "index.html");
  if (fs.existsSync(indexHtml)) return true;
  process.stdout.write(`  Downloading Qdrant Web UI (first time only)\u2026
`);
  try {
    await downloadQdrantUI(cacheDir);
    process.stdout.write(`  Qdrant Web UI ready.
`);
    return true;
  } catch (err) {
    process.stdout.write(`  Qdrant Web UI download failed: ${String(err)}
`);
    return false;
  }
}
async function openQdrantDashboard(projectRoot) {
  const kirographDir = path.join(projectRoot, ".kirograph");
  let port = null;
  try {
    const state = JSON.parse(fs.readFileSync(path.join(kirographDir, SERVER_STATE_FILE), "utf8"));
    port = state.port;
  } catch {
  }
  if (!port) {
    process.stdout.write(`  Qdrant server is not running \u2014 use kg qdrant start.
`);
    return;
  }
  const url = `http://127.0.0.1:${port}/dashboard`;
  console.log(`
  ${import_ui.violet}${import_ui.bold}Qdrant Dashboard${import_ui.reset}  ${import_ui.dim}(built-in)${import_ui.reset}`);
  console.log(`  ${import_ui.green}\u2713${import_ui.reset} Opening ${import_ui.violet}${url}${import_ui.reset}`);
  console.log();
  openBrowser(url);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  ensureQdrantUI,
  openQdrantDashboard
});
//# sourceMappingURL=qdrant-dashboard.js.map
