/*
 * Copyright 2026, Jaime Terrats.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import { existsSync } from 'node:fs';
import { join } from 'node:path';
import { PHASE_REGISTRY, allPhaseIds, buildPhasePlan, derivePhaseGates, phasePlaybooks, resolvePhaseSequenceFromRoles, } from '../phases/registry.js';
// ─── Backwards-compatible exports (derived from registry) ────────────────────
export const PHASE_SEQUENCE = allPhaseIds();
export const ALWAYS_INCLUDED = PHASE_REGISTRY.filter((p) => p.alwaysIncluded).map((p) => p.id);
export const PROFILE_PHASE_MAP = Object.fromEntries(PHASE_REGISTRY.filter((p) => p.triggeredByProfiles.length > 0).flatMap((p) => p.triggeredByProfiles.map((trigger) => [trigger, p.id])));
export const PHASE_TO_PROFILE = Object.fromEntries(PHASE_REGISTRY.map((p) => [p.id, p.profile]));
export const PHASE_GATES = derivePhaseGates(allPhaseIds());
export const PHASE_PLAYBOOKS = Object.fromEntries(PHASE_REGISTRY.map((p) => [p.id, p.playbooks]));
// ─── Phase sequence resolution ───────────────────────────────────────────────
export function resolvePhaseSequence(profiles, taskRoles, phaseOverrides, declaredPhases) {
    // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
    if ((phaseOverrides && phaseOverrides.length > 0) || (declaredPhases && declaredPhases.length > 0)) {
        const plan = buildPhasePlan({ taskRoles, activeProfiles: profiles, overrides: phaseOverrides, declaredPhases });
        return plan.included.map((e) => e.phase);
    }
    return resolvePhaseSequenceFromRoles(taskRoles ?? undefined, profiles);
}
// ─── Implementor resolution ──────────────────────────────────────────────────
const ORCHESTRATION_PROFILES = new Set(['pm', 'ba', 'ta', 'sa', 'qa', 'devops']);
export function resolveImplementorProfile(activeProfiles) {
    const found = activeProfiles.find((p) => !ORCHESTRATION_PROFILES.has(p));
    return found ?? 'developer';
}
// ─── Phase instructions / playbooks ──────────────────────────────────────────
export const DEFAULT_INSTRUCTIONS_DIR = '.setup-agents/playbooks';
export function resolvePhaseInstructions(cwd, phase, instructionsDir) {
    const dir = join(cwd, instructionsDir ?? DEFAULT_INSTRUCTIONS_DIR);
    const filenames = phasePlaybooks(phase);
    const playbooks = filenames.map((filename) => {
        const path = join(dir, filename);
        return { filename, path, exists: existsSync(path) };
    });
    return { phase, instructionsDir: dir, playbooks };
}
export function resolveAllPhaseInstructions(cwd, instructionsDir) {
    return PHASE_SEQUENCE.map((phase) => resolvePhaseInstructions(cwd, phase, instructionsDir));
}
// Re-export for convenience
export { phaseToProfile, phaseDeliversTo, phaseValidatesWith, getPhaseDefinition } from '../phases/registry.js';
//# sourceMappingURL=workflow-instructions.js.map