import {
  createFile
} from "./chunk-HEVS5YLD.js";
import {
  runCLI
} from "./chunk-2GXVIN57.js";
import {
  WORKSPACE_CONFIG_FILE_NAME
} from "./chunk-V6OJGLBA.js";

// src/workspace-init.ts
import * as readline from "readline";
import * as path from "path";
import YAML from "yaml";
var LOG_PREFIX = "[workspace:init]";
var WORKSPACE_FILENAME = WORKSPACE_CONFIG_FILE_NAME;
var DEFAULT_WORKSPACE_ID = "default";
var DEFAULT_WORKSPACE_NAME = "My Project";
var DEFAULT_PROJECT_NAME = "my-project";
var DEFAULT_LANE_TITLE = "Default";
var DEFAULT_NAMESPACE = DEFAULT_WORKSPACE_ID;
var CANONICAL_BOOTSTRAP_COMMAND = "npx lumenflow";
var LEGACY_WORKSPACE_INIT_ENTRYPOINT = "workspace-init";
var LEGACY_ENTRYPOINT_MARKER = "legacy";
var LEGACY_ENTRYPOINT_MESSAGE_PREFIX = `${LOG_PREFIX} ${LEGACY_ENTRYPOINT_MARKER} entrypoint`;
var DEFAULT_DENY_OVERLAYS = ["~/.ssh", "~/.aws", "~/.gnupg", ".env"];
var DEFAULT_DENY_OVERLAYS_PROMPT = DEFAULT_DENY_OVERLAYS.join(", ");
var SANDBOX_NETWORK_PROFILE = {
  OFF: "off",
  FULL: "full"
};
var DEFAULT_SANDBOX_NETWORK_PROFILE = SANDBOX_NETWORK_PROFILE.OFF;
var CLOUD_CONNECT_RESPONSE = {
  YES: "yes",
  NO: "no",
  SHORT_YES: "y",
  BOOLEAN_TRUE: "true"
};
var CLOUD_CONNECT_TRUTHY = /* @__PURE__ */ new Set([
  CLOUD_CONNECT_RESPONSE.YES,
  CLOUD_CONNECT_RESPONSE.SHORT_YES,
  CLOUD_CONNECT_RESPONSE.BOOLEAN_TRUE
]);
var SD_PACK_ID = "software-delivery";
var SD_PACK_VERSION = "0.1.0";
var EMPTY_SOFTWARE_DELIVERY_CONFIG = Object.freeze({});
function createEmptySoftwareDeliveryConfig() {
  return { ...EMPTY_SOFTWARE_DELIVERY_CONFIG };
}
var DEFAULT_SOFTWARE_DELIVERY_PACK_PIN = Object.freeze({
  id: SD_PACK_ID,
  version: SD_PACK_VERSION,
  integrity: "dev",
  source: "local"
});
function createDefaultPackPins() {
  return [{ ...DEFAULT_SOFTWARE_DELIVERY_PACK_PIN }];
}
var WORKSPACE_QUESTIONS = [
  {
    name: "projectName",
    prompt: "Project name",
    defaultValue: DEFAULT_PROJECT_NAME
  },
  {
    name: "lanes",
    prompt: "Work lanes (comma-separated, e.g., Backend, Frontend, DevOps)",
    defaultValue: DEFAULT_LANE_TITLE
  },
  {
    name: "sandboxProfile",
    prompt: "Sandbox network profile (off | full)",
    defaultValue: DEFAULT_SANDBOX_NETWORK_PROFILE
  },
  {
    name: "deniedPaths",
    prompt: "Denied paths (comma-separated, e.g., ~/.ssh, ~/.aws, .env)",
    defaultValue: DEFAULT_DENY_OVERLAYS_PROMPT
  },
  {
    name: "cloudConnect",
    prompt: "Enable cloud agent support? (yes | no)",
    defaultValue: CLOUD_CONNECT_RESPONSE.NO
  }
];
function buildLegacyWorkspaceInitGuidance(entrypoint = LEGACY_WORKSPACE_INIT_ENTRYPOINT) {
  return `${LEGACY_ENTRYPOINT_MESSAGE_PREFIX} "${entrypoint}" is retired. Use "${CANONICAL_BOOTSTRAP_COMMAND}" for bootstrap-all onboarding.`;
}
function parseAnswers(answers) {
  const lanes = answers.lanes.split(",").map((l) => l.trim()).filter(Boolean);
  const deniedPaths = answers.deniedPaths ? answers.deniedPaths.split(",").map((p) => p.trim()).filter(Boolean) : [];
  const cloudConnect = CLOUD_CONNECT_TRUTHY.has(answers.cloudConnect.toLowerCase());
  const sandboxProfile = answers.sandboxProfile === SANDBOX_NETWORK_PROFILE.FULL ? SANDBOX_NETWORK_PROFILE.FULL : SANDBOX_NETWORK_PROFILE.OFF;
  return {
    projectName: answers.projectName,
    lanes,
    sandboxProfile,
    deniedPaths,
    cloudConnect
  };
}
function toKebabCase(title) {
  return title.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-|-$/g, "");
}
function getDefaultWorkspaceConfig() {
  return {
    id: DEFAULT_WORKSPACE_ID,
    name: DEFAULT_WORKSPACE_NAME,
    packs: createDefaultPackPins(),
    lanes: [
      {
        id: DEFAULT_WORKSPACE_ID,
        title: DEFAULT_LANE_TITLE,
        allowed_scopes: []
      }
    ],
    policies: {},
    security: {
      allowed_scopes: [],
      network_default: DEFAULT_SANDBOX_NETWORK_PROFILE,
      deny_overlays: [...DEFAULT_DENY_OVERLAYS]
    },
    software_delivery: createEmptySoftwareDeliveryConfig(),
    memory_namespace: DEFAULT_NAMESPACE,
    event_namespace: DEFAULT_NAMESPACE
  };
}
function buildWorkspaceConfig(input) {
  const lanes = input.lanes.map((title) => ({
    id: toKebabCase(title),
    title,
    allowed_scopes: []
  }));
  return {
    id: toKebabCase(input.projectName),
    name: input.projectName,
    packs: createDefaultPackPins(),
    lanes,
    policies: {},
    security: {
      allowed_scopes: [],
      network_default: input.sandboxProfile,
      deny_overlays: input.deniedPaths
    },
    software_delivery: createEmptySoftwareDeliveryConfig(),
    memory_namespace: toKebabCase(input.projectName),
    event_namespace: toKebabCase(input.projectName)
  };
}
function generateWorkspaceYaml(config) {
  const lines = [];
  lines.push("# LumenFlow Workspace Configuration");
  lines.push("# Generated by workspace:init");
  lines.push("");
  lines.push("# Workspace ID - unique identifier for this workspace");
  lines.push(`id: ${YAML.stringify(config.id).trim()}`);
  lines.push("");
  lines.push("# Human-readable project name");
  lines.push(`name: ${YAML.stringify(config.name).trim()}`);
  lines.push("");
  lines.push("# Domain packs - plugins that add tools, policies, and task types");
  lines.push("# Add packs with: pnpm pack:install --id <pack-id>");
  if (config.packs.length === 0) {
    lines.push("packs: []");
  } else {
    lines.push("packs:");
    for (const pack of config.packs) {
      lines.push(`  - id: ${YAML.stringify(pack.id).trim()}`);
      lines.push(`    version: ${YAML.stringify(pack.version).trim()}`);
      lines.push(`    integrity: ${YAML.stringify(pack.integrity).trim()}`);
      lines.push(`    source: ${YAML.stringify(pack.source).trim()}`);
    }
  }
  lines.push("");
  lines.push("# Work lanes - partitions for parallel work streams");
  lines.push("# Each lane has an ID, title, and optional scope restrictions");
  lines.push("lanes:");
  for (const lane of config.lanes) {
    lines.push(`  - id: ${YAML.stringify(lane.id).trim()}`);
    lines.push(`    title: ${YAML.stringify(lane.title).trim()}`);
    lines.push(`    allowed_scopes: ${YAML.stringify(lane.allowed_scopes).trim()}`);
  }
  lines.push("");
  lines.push("# Security configuration");
  lines.push("security:");
  lines.push("  # Workspace-level scope restrictions");
  lines.push(`  allowed_scopes: ${YAML.stringify(config.security.allowed_scopes).trim()}`);
  lines.push(
    `  # Network access default for sandbox (${SANDBOX_NETWORK_PROFILE.OFF} = no network, ${SANDBOX_NETWORK_PROFILE.FULL} = unrestricted)`
  );
  lines.push(`  network_default: ${config.security.network_default}`);
  lines.push("  # Paths denied to agent sandbox via deny overlays");
  lines.push("  deny_overlays:");
  if (config.security.deny_overlays.length === 0) {
    lines.pop();
    lines.push("  deny_overlays: []");
  } else {
    for (const overlay of config.security.deny_overlays) {
      lines.push(`    - ${YAML.stringify(overlay).trim()}`);
    }
  }
  lines.push("");
  lines.push("# Software delivery config extensions (required workspace v2 block)");
  lines.push(`software_delivery: ${YAML.stringify(config.software_delivery).trim()}`);
  lines.push("");
  lines.push("# Memory namespace for session tracking and context recovery");
  lines.push(`memory_namespace: ${YAML.stringify(config.memory_namespace).trim()}`);
  lines.push("");
  lines.push("# Event namespace for kernel event storage");
  lines.push(`event_namespace: ${YAML.stringify(config.event_namespace).trim()}`);
  lines.push("");
  return lines.join("\n");
}
async function writeWorkspaceFile(targetDir, config, force = false) {
  const result = {
    created: [],
    skipped: [],
    merged: [],
    warnings: []
  };
  const yamlContent = generateWorkspaceYaml(config);
  const filePath = path.join(targetDir, WORKSPACE_FILENAME);
  await createFile(filePath, yamlContent, force, result, targetDir);
  return result;
}
async function runNonInteractive(targetDir, force = false) {
  const config = getDefaultWorkspaceConfig();
  return writeWorkspaceFile(targetDir, config, force);
}
function askQuestion(rl, question) {
  return new Promise((resolve) => {
    const prompt = question.defaultValue ? `${question.prompt} [${question.defaultValue}]: ` : `${question.prompt}: `;
    rl.question(prompt, (answer) => {
      resolve(answer.trim() || question.defaultValue);
    });
  });
}
async function runInteractive(targetDir, force = false) {
  const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
  });
  try {
    console.log(`${LOG_PREFIX} Workspace configuration wizard`);
    console.log(`${LOG_PREFIX} Answer the following questions (press Enter for defaults):
`);
    const answers = {};
    for (const question of WORKSPACE_QUESTIONS) {
      answers[question.name] = await askQuestion(rl, question);
    }
    const input = parseAnswers(answers);
    const config = buildWorkspaceConfig(input);
    const result = await writeWorkspaceFile(targetDir, config, force);
    console.log(`
${LOG_PREFIX} Workspace configuration complete!`);
    if (result.created.length > 0) {
      console.log(`${LOG_PREFIX} Created: ${result.created.join(", ")}`);
    }
    if (result.skipped.length > 0) {
      console.log(`${LOG_PREFIX} Skipped (already exists): ${result.skipped.join(", ")}`);
    }
    return result;
  } finally {
    rl.close();
  }
}
async function main() {
  const invokedEntryPoint = path.basename(
    process.argv[1] ?? LEGACY_WORKSPACE_INIT_ENTRYPOINT,
    ".js"
  );
  console.warn(buildLegacyWorkspaceInitGuidance(invokedEntryPoint));
}
if (import.meta.main) {
  void runCLI(main);
}

export {
  LOG_PREFIX,
  WORKSPACE_FILENAME,
  DEFAULT_WORKSPACE_ID,
  DEFAULT_WORKSPACE_NAME,
  DEFAULT_PROJECT_NAME,
  DEFAULT_LANE_TITLE,
  DEFAULT_NAMESPACE,
  CANONICAL_BOOTSTRAP_COMMAND,
  DEFAULT_DENY_OVERLAYS,
  DEFAULT_DENY_OVERLAYS_PROMPT,
  SANDBOX_NETWORK_PROFILE,
  DEFAULT_SANDBOX_NETWORK_PROFILE,
  CLOUD_CONNECT_RESPONSE,
  DEFAULT_SOFTWARE_DELIVERY_PACK_PIN,
  WORKSPACE_QUESTIONS,
  buildLegacyWorkspaceInitGuidance,
  parseAnswers,
  getDefaultWorkspaceConfig,
  buildWorkspaceConfig,
  generateWorkspaceYaml,
  writeWorkspaceFile,
  runNonInteractive,
  runInteractive,
  main
};
