/**
 * Shared Zod schema primitives.
 *
 * Note: Uses zod v4 syntax for SDK compatibility.
 */
import { z } from 'zod/v4';
import { STATUS_VALUES } from './status.js';
import { CLAUDE_EFFORT_VALUES, CODEX_REASONING_EFFORT_VALUES, COPILOT_EFFORT_VALUES, RUNTIME_PREPARE_PRESETS } from './workflow-types.js';
export { McpServerConfigSchema, McpServersSchema } from './mcp-schemas.js';
/** Agent model schema (opus, sonnet, haiku) */
export const AgentModelSchema = z.enum(['opus', 'sonnet', 'haiku']).default('sonnet');
/** Agent configuration schema */
export const AgentConfigSchema = z.object({
    name: z.string().min(1),
    description: z.string().optional(),
    model: AgentModelSchema,
    systemPrompt: z.string().optional(),
    allowedTools: z.array(z.string()).optional(),
    maxTurns: z.number().int().positive().optional(),
});
/** Claude CLI configuration schema */
export const ClaudeConfigSchema = z.object({
    command: z.string().default('claude'),
    timeout: z.number().int().positive().default(300000),
});
/** TAKT global tool configuration schema */
export const TaktConfigSchema = z.object({
    defaultModel: AgentModelSchema,
    defaultWorkflow: z.string().default('default'),
    agentDirs: z.array(z.string()).default([]),
    workflowDirs: z.array(z.string()).default([]),
    sessionDir: z.string().optional(),
    claude: ClaudeConfigSchema.default({ command: 'claude', timeout: 300000 }),
});
/** Agent type schema */
export const AgentTypeSchema = z.enum(['coder', 'architect', 'supervisor', 'custom']);
/** Status schema */
export const StatusSchema = z.enum(STATUS_VALUES);
/** Permission mode schema for tool execution */
export const PermissionModeSchema = z.enum(['readonly', 'edit', 'full']);
/** Claude sandbox settings schema */
export const ClaudeSandboxSchema = z.object({
    allow_unsandboxed_commands: z.boolean().optional(),
    excluded_commands: z.array(z.string()).optional(),
}).optional();
/** Provider-specific step options schema */
export const StepProviderOptionsSchema = z.object({
    codex: z.object({
        network_access: z.boolean().optional(),
        reasoning_effort: z.enum(CODEX_REASONING_EFFORT_VALUES).optional(),
    }).optional(),
    opencode: z.object({
        network_access: z.boolean().optional(),
        variant: z.string().min(1).optional(),
    }).optional(),
    claude: z.object({
        allowed_tools: z.array(z.string()).optional(),
        effort: z.enum(CLAUDE_EFFORT_VALUES).optional(),
        sandbox: ClaudeSandboxSchema,
    }).optional(),
    claude_terminal: z.object({
        backend: z.enum(['tmux']).optional(),
        timeout_ms: z.number().int().positive().optional(),
        keep_session: z.boolean().optional(),
        transcript_poll_interval_ms: z.number().int().positive().optional(),
    }).strict().optional(),
    copilot: z.object({
        effort: z.enum(COPILOT_EFFORT_VALUES).optional(),
    }).optional(),
}).optional();
/** Provider key schema for profile maps */
export const ProviderProfileNameSchema = z.enum([
    'claude',
    'claude-sdk',
    'claude-terminal',
    'codex',
    'opencode',
    'cursor',
    'copilot',
    'kiro',
    'mock',
]);
export const ProviderTypeSchema = ProviderProfileNameSchema;
export const ProviderBlockSchema = z.object({
    type: ProviderTypeSchema,
    model: z.string().optional(),
    network_access: z.boolean().optional(),
    sandbox: ClaudeSandboxSchema,
}).strict().superRefine((provider, ctx) => {
    const hasNetworkAccess = provider.network_access !== undefined;
    const hasSandbox = provider.sandbox !== undefined;
    if (provider.type === 'claude-sdk') {
        if (hasNetworkAccess) {
            ctx.addIssue({
                code: 'custom',
                path: ['network_access'],
                message: "provider.type 'claude-sdk' does not support 'network_access'.",
            });
        }
        return;
    }
    if (provider.type === 'claude') {
        if (hasNetworkAccess) {
            ctx.addIssue({
                code: 'custom',
                path: ['network_access'],
                message: "provider.type 'claude' does not support 'network_access'.",
            });
        }
        return;
    }
    if (provider.type === 'claude-terminal') {
        if (hasNetworkAccess) {
            ctx.addIssue({
                code: 'custom',
                path: ['network_access'],
                message: "provider.type 'claude-terminal' does not support 'network_access'.",
            });
        }
        if (hasSandbox) {
            ctx.addIssue({
                code: 'custom',
                path: ['sandbox'],
                message: "provider.type 'claude-terminal' does not support 'sandbox'.",
            });
        }
        return;
    }
    if (provider.type === 'codex' || provider.type === 'opencode') {
        if (hasSandbox) {
            ctx.addIssue({
                code: 'custom',
                path: ['sandbox'],
                message: `provider.type '${provider.type}' does not support 'sandbox'.`,
            });
        }
        return;
    }
    if (hasNetworkAccess || hasSandbox) {
        ctx.addIssue({
            code: 'custom',
            message: `provider.type '${provider.type}' does not support provider-specific options in provider block.`,
        });
    }
});
export const ProviderReferenceSchema = z.union([ProviderTypeSchema, ProviderBlockSchema]);
export const RateLimitFallbackSchema = z.object({
    switch_chain: z.array(z.object({
        provider: ProviderTypeSchema,
        model: z.string().optional(),
    }).strict()).optional(),
}).strict();
/** Provider permission profile schema */
export const ProviderPermissionProfileSchema = z.object({
    default_permission_mode: PermissionModeSchema,
    step_permission_overrides: z.record(z.string(), PermissionModeSchema).optional(),
}).strict();
/** Provider permission profiles schema */
export const ProviderPermissionProfilesSchema = z.object({
    claude: ProviderPermissionProfileSchema.optional(),
    'claude-sdk': ProviderPermissionProfileSchema.optional(),
    'claude-terminal': ProviderPermissionProfileSchema.optional(),
    codex: ProviderPermissionProfileSchema.optional(),
    opencode: ProviderPermissionProfileSchema.optional(),
    cursor: ProviderPermissionProfileSchema.optional(),
    copilot: ProviderPermissionProfileSchema.optional(),
    kiro: ProviderPermissionProfileSchema.optional(),
    mock: ProviderPermissionProfileSchema.optional(),
}).strict().optional();
/** Runtime prepare preset identifiers */
export const RuntimePreparePresetSchema = z.enum(RUNTIME_PREPARE_PRESETS);
/** Runtime prepare entry: preset name or script path */
export const RuntimePrepareEntrySchema = z.union([
    RuntimePreparePresetSchema,
    z.string().min(1),
]);
/** Workflow-level runtime settings */
export const RuntimeConfigSchema = z.object({
    prepare: z.array(RuntimePrepareEntrySchema).optional(),
}).optional();
/** Workflow-level provider options schema */
export const WorkflowProviderOptionsSchema = z.object({
    provider: ProviderReferenceSchema.optional(),
    model: z.string().optional(),
    provider_options: StepProviderOptionsSchema,
    runtime: RuntimeConfigSchema,
}).optional();
/**
 * Output contract item schema (new structured format).
 */
export const OutputContractItemSchema = z.object({
    name: z.string().min(1),
    format: z.union([
        z.string().min(1),
        z.object({
            $param: z.string().min(1),
        }).strict(),
    ]),
    use_judge: z.boolean().optional().default(true),
    order: z.string().optional(),
});
/** Output contracts field schema for step-level definition. */
export const OutputContractsFieldSchema = z.object({
    report: z.array(OutputContractItemSchema).optional(),
}).optional();
const CommandQualityGateInputSchema = z.object({
    type: z.literal('command'),
    name: z.string().min(1).optional(),
    command: z.string({ error: 'command quality gate requires "command"' }).min(1),
    cwd: z.string().min(1).optional(),
    timeout_ms: z.number().int().positive().optional(),
}).strict();
function normalizeCommandQualityGate(gate) {
    return {
        type: gate.type,
        ...(gate.name !== undefined ? { name: gate.name } : {}),
        command: gate.command,
        ...(gate.cwd !== undefined ? { cwd: gate.cwd } : {}),
        ...(gate.timeout_ms !== undefined ? { timeoutMs: gate.timeout_ms } : {}),
    };
}
const QualityGateRawSchema = z.unknown().superRefine((gate, ctx) => {
    if (typeof gate === 'string') {
        return;
    }
    if (gate !== null && typeof gate === 'object' && gate.type === 'command') {
        const parsed = CommandQualityGateInputSchema.safeParse(gate);
        if (!parsed.success) {
            for (const issue of parsed.error.issues) {
                ctx.addIssue({
                    code: z.ZodIssueCode.custom,
                    path: issue.path,
                    message: issue.message,
                });
            }
        }
        return;
    }
    ctx.addIssue({
        code: z.ZodIssueCode.custom,
        message: 'quality gate must be a string or command gate object',
    });
}).transform((gate) => {
    if (typeof gate === 'string') {
        return gate;
    }
    return normalizeCommandQualityGate(CommandQualityGateInputSchema.parse(gate));
});
/** Quality gates schema - AI directives and command gates for step completion */
export const QualityGatesSchema = z.array(QualityGateRawSchema).optional();
/** Step-specific quality gates override schema */
export const StepQualityGatesOverrideSchema = z.object({
    quality_gates: QualityGatesSchema,
}).optional();
export function hasProviderOptionsLeaf(providerOptions) {
    return Object.values(providerOptions).some(hasDefinedProviderOptionLeaf);
}
function hasDefinedProviderOptionLeaf(value) {
    if (value === undefined) {
        return false;
    }
    if (Array.isArray(value)) {
        return true;
    }
    if (value === null || typeof value !== 'object') {
        return true;
    }
    return Object.values(value).some(hasDefinedProviderOptionLeaf);
}
export const PersonaProviderEntrySchema = z.object({
    type: ProviderTypeSchema.optional(),
    provider: ProviderTypeSchema.optional(),
    model: z.string().optional(),
    provider_options: StepProviderOptionsSchema,
}).strict().superRefine((entry, ctx) => {
    if (entry.type !== undefined && entry.provider !== undefined) {
        ctx.addIssue({
            code: 'custom',
            message: "persona_providers entry cannot include both 'type' and 'provider'",
        });
    }
    if (entry.type === undefined
        && entry.provider === undefined
        && entry.model === undefined
        && entry.provider_options === undefined) {
        ctx.addIssue({
            code: 'custom',
            message: "persona_providers entry must include at least one of 'provider', 'type', 'model', or 'provider_options'",
        });
    }
    if (entry.provider_options !== undefined
        && !hasProviderOptionsLeaf(entry.provider_options)) {
        ctx.addIssue({
            code: 'custom',
            path: ['provider_options'],
            message: "persona_providers entry 'provider_options' must include at least one provider-specific option",
        });
    }
});
export const PersonaProviderReferenceSchema = z.union([
    ProviderTypeSchema,
    PersonaProviderEntrySchema,
]);
export const TaktProviderEntrySchema = z.object({
    provider: ProviderTypeSchema.optional(),
    model: z.string().optional(),
}).strict().refine((entry) => entry.provider !== undefined || entry.model !== undefined, { message: "takt_providers.assistant must include either 'provider' or 'model'" });
export const TaktProvidersSchema = z.object({
    assistant: TaktProviderEntrySchema.optional(),
}).strict().refine((entry) => entry.assistant !== undefined, { message: "takt_providers must include 'assistant'" });
/** Custom agent configuration schema */
export const CustomAgentConfigSchema = z.object({
    name: z.string().min(1),
    prompt_file: z.string().optional(),
    prompt: z.string().optional(),
    allowed_tools: z.array(z.string()).optional(),
}).strict().refine((data) => data.prompt_file || data.prompt, { message: 'Agent must have prompt_file or prompt' });
export const LoggingConfigSchema = z.object({
    level: z.enum(['debug', 'info', 'warn', 'error']).optional(),
    trace: z.boolean().optional(),
    debug: z.boolean().optional(),
    provider_events: z.boolean().optional(),
    usage_events: z.boolean().optional(),
});
export const ObservabilityConfigSchema = z.object({
    enabled: z.boolean().optional(),
    monitor: z.boolean().optional(),
    session_log_exporter: z.boolean().optional(),
    usage_events_phase: z.boolean().optional(),
}).strict();
/** Analytics config schema */
export const AnalyticsConfigSchema = z.object({
    enabled: z.boolean().optional(),
    events_path: z.string().optional(),
    retention_days: z.number().int().positive().optional(),
});
/** Language setting schema */
export const LanguageSchema = z.enum(['en', 'ja']);
/** Pipeline execution config schema */
export const PipelineConfigSchema = z.object({
    default_branch_prefix: z.string().optional(),
    commit_message_template: z.string().optional(),
    pr_body_template: z.string().optional(),
}).strict();
//# sourceMappingURL=schema-base.js.map