function requireString(value, field) {
    if (typeof value !== 'string' || value.trim().length === 0) {
        throw new Error(`System effect requires non-empty string field "${field}"`);
    }
    return value;
}
function requireNumber(value, field) {
    if (typeof value !== 'number' || !Number.isInteger(value) || value <= 0) {
        throw new Error(`System effect requires positive integer field "${field}"`);
    }
    return value;
}
function requireObject(value, field) {
    if (value == null || typeof value !== 'object' || Array.isArray(value)) {
        throw new Error(`System effect requires object field "${field}"`);
    }
    return value;
}
function validateAllowedKeys(value, field, allowedKeys) {
    for (const key of Object.keys(value)) {
        if (!allowedKeys.includes(key)) {
            throw new Error(`System effect does not allow unknown field "${field}.${key}"`);
        }
    }
}
function validateIssuePayload(value) {
    const issue = requireObject(value, 'issue');
    validateAllowedKeys(issue, 'issue', ['create', 'title', 'labels']);
    if (issue.create !== undefined && typeof issue.create !== 'boolean') {
        throw new Error('System effect requires boolean field "issue.create"');
    }
    if (issue.title !== undefined && typeof issue.title !== 'string') {
        throw new Error('System effect requires string field "issue.title"');
    }
    if (issue.labels !== undefined) {
        if (!Array.isArray(issue.labels) || issue.labels.some((label) => typeof label !== 'string')) {
            throw new Error('System effect requires string array field "issue.labels"');
        }
    }
}
function validateWorktreePayload(value) {
    const worktree = requireObject(value, 'worktree');
    validateAllowedKeys(worktree, 'worktree', ['enabled', 'auto_pr', 'draft_pr', 'managed_pr']);
    for (const key of ['enabled', 'auto_pr', 'draft_pr', 'managed_pr']) {
        if (worktree[key] !== undefined && typeof worktree[key] !== 'boolean') {
            throw new Error(`System effect requires boolean field "worktree.${key}"`);
        }
    }
    if ((worktree.auto_pr === true || worktree.draft_pr === true || worktree.managed_pr === true) && worktree.enabled !== true) {
        if (worktree.managed_pr === true) {
            throw new Error('System effect requires "worktree.enabled" when auto_pr, draft_pr, or managed_pr is true');
        }
        throw new Error('System effect requires "worktree.enabled" when auto_pr or draft_pr is true');
    }
    if (worktree.managed_pr === true && worktree.auto_pr !== true) {
        throw new Error('System effect requires "worktree.auto_pr" when "worktree.managed_pr" is true');
    }
}
function validateBaseBranchPayload(value) {
    if (typeof value === 'string') {
        requireString(value, 'base_branch');
        return;
    }
    const baseBranch = requireObject(value, 'base_branch');
    validateAllowedKeys(baseBranch, 'base_branch', ['name', 'create_if_missing']);
    requireString(baseBranch.name, 'base_branch.name');
    const createIfMissing = requireObject(baseBranch.create_if_missing, 'base_branch.create_if_missing');
    validateAllowedKeys(createIfMissing, 'base_branch.create_if_missing', ['from', 'push']);
    requireString(createIfMissing.from, 'base_branch.create_if_missing.from');
    if (createIfMissing.push !== undefined && typeof createIfMissing.push !== 'boolean') {
        throw new Error('System effect requires boolean field "base_branch.create_if_missing.push"');
    }
}
export function validateSystemEffectPayload(effect, payload) {
    if (effect.type === 'enqueue_task') {
        if (payload.mode !== undefined)
            requireString(payload.mode, 'mode');
        if (payload.workflow !== undefined)
            requireString(payload.workflow, 'workflow');
        if (payload.task !== undefined)
            requireString(payload.task, 'task');
        if (payload.pr !== undefined)
            requireNumber(payload.pr, 'pr');
        if (payload.base_branch !== undefined)
            validateBaseBranchPayload(payload.base_branch);
        if (payload.issue !== undefined)
            validateIssuePayload(payload.issue);
        if (payload.worktree !== undefined)
            validateWorktreePayload(payload.worktree);
        if (payload.mode === 'new' && payload.pr !== undefined) {
            throw new Error('System effect mode "new" does not allow field "pr"');
        }
        if (payload.mode === 'from_pr') {
            if (payload.pr === undefined) {
                throw new Error('System effect mode "from_pr" requires field "pr"');
            }
            if (payload.issue !== undefined) {
                throw new Error('System effect mode "from_pr" does not allow field "issue"');
            }
            if (payload.worktree !== undefined) {
                throw new Error('System effect mode "from_pr" does not allow field "worktree"');
            }
        }
    }
    if (effect.type === 'comment_pr') {
        requireNumber(payload.pr, 'pr');
        requireString(payload.body, 'body');
    }
    if (effect.type === 'sync_with_root'
        || effect.type === 'resolve_conflicts_with_ai'
        || effect.type === 'merge_pr'
        || effect.type === 'close_pr') {
        requireNumber(payload.pr, 'pr');
    }
}
//# sourceMappingURL=system-step-effect-runner.js.map