#!/usr/bin/env node
"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 __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 import_fs = require("fs");
var import_path = require("path");
var import_os = require("os");
const KIROGRAPH_DIR = (0, import_path.join)((0, import_os.homedir)(), ".kirograph");
const MODELS_DIR = (0, import_path.join)(KIROGRAPH_DIR, "models");
const MODEL_ID = "nomic-ai/nomic-embed-text-v1.5";
async function downloadModel() {
  (0, import_fs.mkdirSync)(KIROGRAPH_DIR, { recursive: true });
  (0, import_fs.mkdirSync)(MODELS_DIR, { recursive: true });
  const modelCachePath = (0, import_path.join)(MODELS_DIR, MODEL_ID.replace("/", "--"));
  if ((0, import_fs.existsSync)(modelCachePath)) {
    console.log("KiroGraph: Embedding model already downloaded.");
    return;
  }
  console.log("KiroGraph: Downloading embedding model (~130MB)...");
  console.log("This is a one-time download for semantic code search.\n");
  try {
    const { pipeline, env } = await import("@huggingface/transformers");
    env.cacheDir = MODELS_DIR;
    await pipeline("feature-extraction", MODEL_ID, {
      progress_callback: (progress) => {
        if (progress.status === "progress" && progress.file && progress.progress !== void 0) {
          const fileName = progress.file.split("/").pop();
          const percent = Math.round(progress.progress);
          process.stdout.write(`\rDownloading ${fileName}... ${percent}%   `);
        } else if (progress.status === "done") {
          process.stdout.write("\n");
        }
      }
    });
    console.log("\nKiroGraph: Embedding model ready!");
  } catch (error) {
    console.log("\nKiroGraph: Could not download embedding model.");
    console.log("Semantic search will download it on first use.");
    if (process.env.KIROGRAPH_DEBUG) {
      console.error(error);
    }
  }
}
downloadModel().catch(() => process.exit(0));
