import {
  init_dist
} from "./chunk-77EZGPSR.js";
import {
  Err,
  Ok
} from "./chunk-RL4VMEXL.js";

// src/persona/generators/ci-workflow.ts
init_dist();
import YAML from "yaml";
function buildGitHubTriggers(triggers) {
  const on = {};
  for (const trigger of triggers) {
    switch (trigger.event) {
      case "on_pr": {
        const prConfig = {};
        if (trigger.conditions?.paths) prConfig.paths = trigger.conditions.paths;
        on.pull_request = prConfig;
        break;
      }
      case "on_commit": {
        const pushConfig = {};
        if (trigger.conditions?.branches) pushConfig.branches = trigger.conditions.branches;
        on.push = pushConfig;
        break;
      }
      case "scheduled":
        on.schedule = [{ cron: trigger.cron }];
        break;
    }
  }
  return on;
}
function generateCIWorkflow(persona, platform) {
  try {
    if (platform === "gitlab") return Err(new Error("GitLab CI generation is not yet supported"));
    const severity = persona.config.severity;
    const steps = [
      { uses: "actions/checkout@v4" },
      { uses: "actions/setup-node@v4", with: { "node-version": "20" } },
      { uses: "pnpm/action-setup@v4", with: { run_install: "frozen" } }
    ];
    const commandSteps = persona.steps.filter((s) => "command" in s);
    for (const step of commandSteps) {
      const severityFlag = severity ? ` --severity ${severity}` : "";
      steps.push({ run: `npx harness ${step.command}${severityFlag}` });
    }
    const workflow = {
      name: persona.name,
      on: buildGitHubTriggers(persona.triggers),
      jobs: {
        enforce: {
          "runs-on": "ubuntu-latest",
          steps
        }
      }
    };
    return Ok(YAML.stringify(workflow, { lineWidth: 0 }));
  } catch (error) {
    return Err(
      new Error(
        `Failed to generate CI workflow: ${error instanceof Error ? error.message : String(error)}`
      )
    );
  }
}

export {
  generateCIWorkflow
};
