#!/usr/bin/env node
import { homedir } from "node:os";
import { dirname, join, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import {
  BoringPluginAssetManager
} from "@hachej/boring-workspace/server";
import {
  readWorkspacePluginPackagePiSnapshot,
  resolveDefaultWorkspacePluginPackagePaths
} from "@hachej/boring-workspace/app/server";
import {
  resolvePluginSourceScopePaths,
  resolveRegisteredPluginSourceDirs
} from "@hachej/boring-ui-plugin-cli";
function resolveBoringUiCliPackageRoot() {
  const here = dirname(fileURLToPath(import.meta.url));
  return resolve(here, "..", "..");
}
function getGlobalPiExtensionsRoot(options = {}) {
  return resolve(options.globalRoot ?? join(homedir(), ".pi", "agent", "extensions"));
}
function getGlobalPiAgentRoot(options = {}) {
  return resolve(options.globalAgentRoot ?? dirname(getGlobalPiExtensionsRoot(options)));
}
const CLI_DEFAULT_PLUGIN_PACKAGES = ["@hachej/boring-ask-user"];
function resolveCliDefaultPluginPackagePaths() {
  try {
    return resolveDefaultWorkspacePluginPackagePaths({
      anchorDir: resolveBoringUiCliPackageRoot(),
      defaultPluginPackages: CLI_DEFAULT_PLUGIN_PACKAGES
    });
  } catch (error) {
    console.error("[boring-ui] failed to resolve default plugin packages:", error instanceof Error ? error.message : String(error));
    return [];
  }
}
function resolveCliBoringPluginDirs(workspaceRoot, options = {}) {
  const resolvedWorkspaceRoot = resolve(workspaceRoot);
  const globalAgentRoot = getGlobalPiAgentRoot(options);
  const globalScope = resolvePluginSourceScopePaths("global", { globalRoot: globalAgentRoot });
  const localScope = resolvePluginSourceScopePaths("local", { workspaceRoot: resolvedWorkspaceRoot });
  const packageSources = [
    ...resolveRegisteredPluginSourceDirs(globalScope).map((dir) => ({ ...dir, scope: "global" })),
    ...resolveRegisteredPluginSourceDirs(localScope).map((dir) => ({ ...dir, scope: "local" }))
  ];
  const includeDefaultPackages = options.includeDefaultPackages ?? true;
  const roots = [
    ...includeDefaultPackages ? resolveCliDefaultPluginPackagePaths().map((rootDir) => ({ rootDir, kind: "internal" })) : [],
    { rootDir: getGlobalPiExtensionsRoot(options), kind: "external" },
    { rootDir: globalScope.npmDir, kind: "external" },
    { rootDir: globalScope.gitDir, kind: "external" },
    { rootDir: localScope.extensionsDir, kind: "external", workspaceId: resolvedWorkspaceRoot },
    { rootDir: localScope.npmDir, kind: "external", workspaceId: resolvedWorkspaceRoot },
    { rootDir: localScope.gitDir, kind: "external", workspaceId: resolvedWorkspaceRoot },
    ...packageSources.map((record) => ({
      rootDir: record.rootDir,
      kind: "external",
      registered: true,
      ...record.scope === "local" ? { workspaceId: resolvedWorkspaceRoot } : {}
    }))
  ];
  const seen = /* @__PURE__ */ new Set();
  return roots.filter((root) => {
    const key = resolve(typeof root === "string" ? root : root.rootDir);
    if (seen.has(key)) return false;
    seen.add(key);
    return true;
  });
}
function readCliPluginPiSnapshot(workspaceRoot, options = {}) {
  return readWorkspacePluginPackagePiSnapshot(resolveCliBoringPluginDirs(workspaceRoot, options));
}
function createCliPluginAssetManager(workspaceRoot, options = {}) {
  return new BoringPluginAssetManager({
    pluginDirs: resolveCliBoringPluginDirs(workspaceRoot, options),
    errorRoot: resolve(workspaceRoot, ".boring-agent", "plugin-errors"),
    frontTargetResolver: options.frontTargetResolver
  });
}
export {
  createCliPluginAssetManager,
  getGlobalPiExtensionsRoot,
  readCliPluginPiSnapshot,
  resolveBoringUiCliPackageRoot,
  resolveCliBoringPluginDirs,
  resolveCliDefaultPluginPackagePaths
};
