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

// src/persona/loader.ts
init_dist();
import * as fs from "fs";
import * as path from "path";
import YAML from "yaml";

// src/persona/schema.ts
import { z } from "zod";
var TriggerContextSchema = z.enum(["always", "on_pr", "on_commit", "on_review", "scheduled", "manual", "on_plan_approved"]).default("always");
var CommandStepSchema = z.object({
  command: z.string(),
  when: TriggerContextSchema
});
var SkillStepSchema = z.object({
  skill: z.string(),
  when: TriggerContextSchema,
  output: z.enum(["inline", "artifact", "auto"]).default("auto")
});
var StepSchema = z.union([CommandStepSchema, SkillStepSchema]);
var PersonaTriggerSchema = z.discriminatedUnion("event", [
  z.object({
    event: z.literal("on_pr"),
    conditions: z.object({
      paths: z.array(z.string()).optional(),
      min_files: z.number().optional()
    }).optional()
  }),
  z.object({
    event: z.literal("on_commit"),
    conditions: z.object({ branches: z.array(z.string()).optional() }).optional()
  }),
  z.object({
    event: z.literal("scheduled"),
    cron: z.string()
  }),
  z.object({
    event: z.literal("manual")
  })
]);
var PersonaConfigSchema = z.object({
  severity: z.enum(["error", "warning"]).default("error"),
  autoFix: z.boolean().default(false),
  timeout: z.number().default(3e5)
});
var PersonaOutputsSchema = z.object({
  "agents-md": z.boolean().default(true),
  "ci-workflow": z.boolean().default(true),
  "runtime-config": z.boolean().default(true)
});
var PersonaSchemaV1 = z.object({
  version: z.literal(1),
  name: z.string(),
  description: z.string(),
  role: z.string(),
  skills: z.array(z.string()),
  commands: z.array(z.string()),
  triggers: z.array(PersonaTriggerSchema),
  config: PersonaConfigSchema.default({}),
  outputs: PersonaOutputsSchema.default({})
});
var PersonaSchemaV2 = z.object({
  version: z.literal(2),
  name: z.string(),
  description: z.string(),
  role: z.string(),
  skills: z.array(z.string()),
  steps: z.array(StepSchema),
  triggers: z.array(PersonaTriggerSchema),
  config: PersonaConfigSchema.default({}),
  outputs: PersonaOutputsSchema.default({})
});
var PersonaSchema = z.union([PersonaSchemaV1, PersonaSchemaV2]);

// src/persona/loader.ts
function normalizePersona(raw) {
  if (raw.version === 1 && Array.isArray(raw.commands)) {
    const { commands, ...rest } = raw;
    return {
      ...rest,
      steps: commands.map((cmd) => ({
        command: cmd,
        when: "always"
      }))
    };
  }
  return raw;
}
function loadPersona(filePath) {
  try {
    if (!fs.existsSync(filePath)) {
      return Err(new Error(`Persona file not found: ${filePath}`));
    }
    const raw = fs.readFileSync(filePath, "utf-8");
    const parsed = YAML.parse(raw);
    const result = PersonaSchema.safeParse(parsed);
    if (!result.success) {
      return Err(new Error(`Invalid persona ${filePath}: ${result.error.message}`));
    }
    return Ok(normalizePersona(result.data));
  } catch (error) {
    return Err(
      new Error(`Failed to load persona: ${error instanceof Error ? error.message : String(error)}`)
    );
  }
}
function listPersonas(dir) {
  try {
    if (!fs.existsSync(dir)) return Ok([]);
    const entries = fs.readdirSync(dir).filter((f) => f.endsWith(".yaml") || f.endsWith(".yml"));
    const personas = [];
    for (const entry of entries) {
      const filePath = path.join(dir, entry);
      const result = loadPersona(filePath);
      if (result.ok) {
        personas.push({ name: result.value.name, description: result.value.description, filePath });
      }
    }
    return Ok(personas);
  } catch (error) {
    return Err(
      new Error(
        `Failed to list personas: ${error instanceof Error ? error.message : String(error)}`
      )
    );
  }
}

export {
  loadPersona,
  listPersonas
};
