const BUG_INTAKE_URL = process.env.BUG_INTAKE_URL?.replace(/\/+$/, "") || "https://seawork-bug-intake.sg.seaverse.dev";
async function getJson(url) {
    const res = await fetch(url, { method: "GET", headers: { accept: "application/json" } });
    if (!res.ok)
        return { ok: false, status: res.status };
    const body = (await res.json());
    return { ok: true, status: res.status, body };
}
async function postJson(url, payload) {
    const res = await fetch(url, {
        method: "POST",
        headers: { "content-type": "application/json", accept: "application/json" },
        body: JSON.stringify(payload),
    });
    if (!res.ok) {
        let errBody;
        try {
            errBody = (await res.json());
        }
        catch {
            // ignore
        }
        return { ok: false, status: res.status, body: errBody };
    }
    const body = (await res.json());
    return { ok: true, status: res.status, body };
}
export async function handleListPublicIssues(msg, deps) {
    const params = new URLSearchParams();
    if (msg.sort)
        params.set("sort", msg.sort);
    if (msg.limit)
        params.set("limit", String(msg.limit));
    if (msg.offset !== undefined)
        params.set("offset", String(msg.offset));
    if (msg.email)
        params.set("email", msg.email);
    if (msg.clientId)
        params.set("clientId", msg.clientId);
    try {
        const r = await getJson(BUG_INTAKE_URL + "/issues?" + params.toString());
        if (!r.ok || !r.body?.ok) {
            return {
                type: "list_public_issues_response",
                requestId: msg.requestId,
                ok: false,
                error: "intake_error_" + r.status,
            };
        }
        return {
            type: "list_public_issues_response",
            requestId: msg.requestId,
            ok: true,
            issues: r.body.issues ?? [],
            total: r.body.total,
        };
    }
    catch (err) {
        deps.logger.error({ err }, "list public issues failed");
        return {
            type: "list_public_issues_response",
            requestId: msg.requestId,
            ok: false,
            error: err.message,
        };
    }
}
export async function handleGetPublicIssue(msg, deps) {
    const params = new URLSearchParams();
    if (msg.email)
        params.set("email", msg.email);
    if (msg.clientId)
        params.set("clientId", msg.clientId);
    const url = BUG_INTAKE_URL +
        "/issues/" +
        msg.issueNumber +
        (params.toString() ? "?" + params.toString() : "");
    try {
        const r = await getJson(url);
        if (!r.ok || !r.body?.ok) {
            return {
                type: "get_public_issue_response",
                requestId: msg.requestId,
                ok: false,
                error: "intake_error_" + r.status,
            };
        }
        return {
            type: "get_public_issue_response",
            requestId: msg.requestId,
            ok: true,
            issue: r.body.issue,
        };
    }
    catch (err) {
        deps.logger.error({ err }, "get public issue failed");
        return {
            type: "get_public_issue_response",
            requestId: msg.requestId,
            ok: false,
            error: err.message,
        };
    }
}
export async function handleListIssueComments(msg, deps) {
    try {
        const r = await getJson(BUG_INTAKE_URL + "/issues/" + msg.issueNumber + "/comments");
        if (!r.ok || !r.body?.ok) {
            return {
                type: "list_issue_comments_response",
                requestId: msg.requestId,
                ok: false,
                error: "intake_error_" + r.status,
            };
        }
        return {
            type: "list_issue_comments_response",
            requestId: msg.requestId,
            ok: true,
            comments: r.body.comments ?? [],
        };
    }
    catch (err) {
        deps.logger.error({ err }, "list comments failed");
        return {
            type: "list_issue_comments_response",
            requestId: msg.requestId,
            ok: false,
            error: err.message,
        };
    }
}
export async function handlePostIssueComment(msg, deps) {
    const email = msg.email?.trim();
    const clientId = msg.clientId?.trim();
    const displayName = msg.displayName?.trim();
    const body = msg.body?.trim();
    if (!email || !clientId || !displayName || !body) {
        return {
            type: "post_issue_comment_response",
            requestId: msg.requestId,
            ok: false,
            error: "missing_fields",
        };
    }
    try {
        const r = await postJson(BUG_INTAKE_URL + "/issues/" + msg.issueNumber + "/comments", { email, clientId, displayName, body });
        if (!r.ok || !r.body?.ok) {
            return {
                type: "post_issue_comment_response",
                requestId: msg.requestId,
                ok: false,
                error: r.body?.error ?? "intake_error_" + r.status,
            };
        }
        return {
            type: "post_issue_comment_response",
            requestId: msg.requestId,
            ok: true,
            comment: r.body.comment,
        };
    }
    catch (err) {
        deps.logger.error({ err }, "post comment failed");
        return {
            type: "post_issue_comment_response",
            requestId: msg.requestId,
            ok: false,
            error: err.message,
        };
    }
}
export async function handleToggleIssueReaction(msg, deps) {
    const email = msg.email?.trim();
    const clientId = msg.clientId?.trim();
    if (!email || !clientId) {
        return {
            type: "toggle_issue_reaction_response",
            requestId: msg.requestId,
            ok: false,
            error: "missing_identity",
        };
    }
    try {
        const r = await postJson(BUG_INTAKE_URL + "/issues/" + msg.issueNumber + "/reactions", {
            email,
            clientId,
            content: msg.content ?? "+1",
        });
        if (!r.ok || !r.body?.ok) {
            return {
                type: "toggle_issue_reaction_response",
                requestId: msg.requestId,
                ok: false,
                error: r.body?.error ?? "intake_error_" + r.status,
            };
        }
        return {
            type: "toggle_issue_reaction_response",
            requestId: msg.requestId,
            ok: true,
            total: r.body.total,
            viewerHasReacted: r.body.viewerHasReacted,
        };
    }
    catch (err) {
        deps.logger.error({ err }, "toggle reaction failed");
        return {
            type: "toggle_issue_reaction_response",
            requestId: msg.requestId,
            ok: false,
            error: err.message,
        };
    }
}
//# sourceMappingURL=forum-handler.js.map