"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseCreateDialogInput = parseCreateDialogInput;
exports.makeCreateDialogFailure = makeCreateDialogFailure;
exports.normalizeCreateDialogErrorCode = normalizeCreateDialogErrorCode;
const priming_1 = require("../priming");
const task_package_1 = require("../utils/task-package");
function parseCreateDialogInput(parsed) {
    const requestId = typeof parsed['requestId'] === 'string' ? parsed['requestId'].trim() : '';
    if (!requestId) {
        return {
            requestId: 'unknown',
            status: 400,
            errorCode: 'CREATE_FAILED',
            error: 'requestId is required',
        };
    }
    const agentId = typeof parsed['agentId'] === 'string' ? parsed['agentId'].trim() : '';
    if (!agentId) {
        return {
            requestId,
            status: 400,
            errorCode: 'TEAM_MEMBER_INVALID',
            error: 'agentId is required',
        };
    }
    const taskDocPath = typeof parsed['taskDocPath'] === 'string' ? parsed['taskDocPath'].trim() : '';
    if (!taskDocPath) {
        return {
            requestId,
            status: 400,
            errorCode: 'TASKDOC_INVALID',
            error: 'taskDocPath is required',
        };
    }
    if (!(0, task_package_1.isTaskPackagePath)(taskDocPath)) {
        return {
            requestId,
            status: 400,
            errorCode: 'TASKDOC_INVALID',
            error: `taskDocPath must be a Taskdoc directory ending in '.tsk' (got: '${taskDocPath}')`,
        };
    }
    const rawPriming = parsed['priming'];
    const priming = (() => {
        if (rawPriming === undefined) {
            return { ok: true, value: undefined };
        }
        const normalized = (0, priming_1.normalizePrimingConfig)(rawPriming);
        if (!normalized.ok) {
            return { ok: false, error: normalized.error };
        }
        return { ok: true, value: normalized.priming };
    })();
    if (!priming.ok) {
        return {
            requestId,
            status: 400,
            errorCode: 'CREATE_FAILED',
            error: `Invalid priming config: ${priming.error}`,
        };
    }
    return {
        requestId,
        agentId,
        taskDocPath,
        priming: priming.value,
    };
}
function makeCreateDialogFailure(requestId, errorCode, error) {
    return {
        kind: 'failure',
        requestId,
        errorCode,
        error,
    };
}
function normalizeCreateDialogErrorCode(raw) {
    switch (raw) {
        case 'TEAM_NOT_READY':
        case 'TEAM_MEMBER_INVALID':
        case 'TASKDOC_INVALID':
        case 'AUTH_REQUIRED':
        case 'CREATE_FAILED':
            return raw;
        default:
            return 'CREATE_FAILED';
    }
}
