import { extractMediaFromBirdRaw, extractMediaFromXurlRaw } from "./media.js";
const asRecord = (value) => value && typeof value === "object" ? value : null;
const asArray = (value) => (Array.isArray(value) ? value : null);
const asString = (value) => (typeof value === "string" ? value : null);
const asNumber = (value) => (typeof value === "number" ? value : null);
function resolveXurlTopLevelError(root) {
    if (!root)
        return null;
    const status = asNumber(root.status);
    const title = asString(root.title)?.trim();
    const detail = asString(root.detail)?.trim();
    if (!status && !title && !detail)
        return null;
    const label = detail || title || "request failed";
    const statusText = status ? ` (${status})` : "";
    const base = `xurl API error: ${label}${statusText}`;
    if (status === 401 || /unauthorized/i.test(label)) {
        return `${base}. xurl is installed but is not authorized for this request; run "xurl auth status" and configure OAuth credentials, or install "bird" for fallback.`;
    }
    return base;
}
function resolveXurlArticleText(article) {
    if (!article)
        return null;
    const title = asString(article.title)?.trim() ?? "";
    const body = asString(article.text)?.trim() ??
        asString(article.body)?.trim() ??
        asString(article.preview_text)?.trim() ??
        asString(article.excerpt)?.trim() ??
        "";
    if (title && body && !body.includes(title)) {
        return `${title}\n\n${body}`;
    }
    if (body)
        return body;
    if (title)
        return title;
    const articleResults = asRecord(article.article_results);
    const articleResult = asRecord(articleResults?.result);
    if (!articleResult)
        return null;
    return resolveXurlArticleText(articleResult);
}
function resolveXurlTweetText(data) {
    const dataText = asString(data.text)?.trim() ?? "";
    const noteTweet = asRecord(data.note_tweet);
    const noteTweetText = asString(noteTweet?.text)?.trim() ?? "";
    const articleText = resolveXurlArticleText(asRecord(data.article)) ?? "";
    const candidates = [dataText, noteTweetText, articleText].filter((value) => value.length > 0);
    if (candidates.length === 0)
        return null;
    return candidates.sort((left, right) => right.length - left.length)[0] ?? null;
}
export function parseXurlTweetPayload(raw) {
    const root = asRecord(raw);
    const topLevelError = resolveXurlTopLevelError(root);
    if (topLevelError)
        throw new Error(topLevelError);
    const errors = asArray(root?.errors);
    if (errors && errors.length > 0) {
        const first = asRecord(errors[0]);
        const message = asString(first?.message);
        if (message)
            throw new Error(`xurl API error: ${message}`);
    }
    const data = asRecord(root?.data);
    if (!data) {
        throw new Error("xurl read returned invalid payload");
    }
    const text = resolveXurlTweetText(data);
    if (!text) {
        throw new Error("xurl read returned invalid payload");
    }
    const includes = asRecord(root?.includes);
    const users = asArray(includes?.users) ?? [];
    const authorId = asString(data.author_id);
    const authorRecord = users.map((entry) => asRecord(entry)).find((entry) => asString(entry?.id) === authorId) ?? null;
    return {
        id: asString(data.id) ?? undefined,
        text,
        author: authorRecord && (asString(authorRecord.username) || asString(authorRecord.name))
            ? {
                username: asString(authorRecord.username) ?? undefined,
                name: asString(authorRecord.name) ?? undefined,
            }
            : undefined,
        createdAt: asString(data.created_at) ?? undefined,
        media: extractMediaFromXurlRaw(raw),
        client: "xurl",
    };
}
export function parseBirdTweetPayload(raw) {
    const parsed = raw;
    const tweet = Array.isArray(parsed) ? parsed[0] : parsed;
    if (!tweet || typeof tweet.text !== "string") {
        throw new Error("bird read returned invalid payload");
    }
    const { _raw, ...rest } = tweet;
    const media = extractMediaFromBirdRaw(_raw);
    return { ...rest, media, client: "bird" };
}
//# sourceMappingURL=parse.js.map