import { cmsApi } from './api.js';
/**
 * Frozen tuple of every action the server accepts. Used both as a type-safe
 * iteration source (bubble menu pills, slash-command registration) and by
 * the runtime `isCoauthorAction` guard so callers don't drift from the
 * server's allowlist.
 */
export const COAUTHOR_ACTIONS = ['rewrite', 'expand', 'compress', 'proofread'];
const ACTION_LABELS = {
    rewrite: 'Rewrite',
    expand: 'Expand',
    compress: 'Compress',
    proofread: 'Proofread',
};
const ACTION_DESCRIPTIONS = {
    rewrite: 'Rewrite the selection in a new tone or style',
    expand: 'Expand the selection with more detail',
    compress: 'Shorten the selection while preserving meaning',
    proofread: 'Correct grammar, spelling, and punctuation',
};
/**
 * Hard client-side ceiling that matches `AI_CONTEXT_MAX_CHARS` in cms-core.
 * Catching this here avoids a round-trip just to get a 400 back when a user
 * pastes a giant block into the bubble menu.
 */
export const COAUTHOR_TEXT_MAX_CHARS = 50_000;
export function coauthorActionLabel(action) {
    return ACTION_LABELS[action];
}
export function coauthorActionDescription(action) {
    return ACTION_DESCRIPTIONS[action];
}
export function isCoauthorAction(value) {
    return typeof value === 'string' && COAUTHOR_ACTIONS.includes(value);
}
/**
 * Run a text-transformation micro-action against `/ai/coauthor`.
 *
 * Returns a discriminated outcome so callers can switch on `outcome.ok`
 * without a try/catch — the underlying `cmsApi` already turns every
 * non-2xx and every network failure into a structured error envelope.
 *
 * Pre-flight validation is intentionally cheap (whitespace + length) so the
 * server stays the single source of truth for richer auth/rate-limit logic.
 */
export async function coauthorText(action, text, opts = {}) {
    if (!isCoauthorAction(action)) {
        return { ok: false, error: `Unknown action "${String(action)}"`, status: 0 };
    }
    if (typeof text !== 'string' || text.trim().length === 0) {
        return { ok: false, error: 'No text provided', status: 0 };
    }
    if (text.length > COAUTHOR_TEXT_MAX_CHARS) {
        return {
            ok: false,
            error: `Text exceeds ${COAUTHOR_TEXT_MAX_CHARS.toLocaleString()} character limit`,
            status: 0,
        };
    }
    const body = { action, text };
    if (opts.style !== undefined)
        body.style = opts.style;
    if (opts.tone !== undefined)
        body.tone = opts.tone;
    if (opts.instructions !== undefined)
        body.instructions = opts.instructions;
    if (opts.targetLength !== undefined)
        body.targetLength = opts.targetLength;
    const res = await cmsApi('/ai/coauthor', {
        method: 'POST',
        body: JSON.stringify(body),
    });
    if (res.error || !res.data) {
        return {
            ok: false,
            error: res.error ?? `Request failed (${res.status})`,
            status: res.status,
        };
    }
    if (typeof res.data.text !== 'string') {
        return {
            ok: false,
            error: 'Malformed response from /ai/coauthor (missing text)',
            status: res.status,
        };
    }
    return {
        ok: true,
        result: {
            action: res.data.action ?? action,
            text: res.data.text,
            changes: action === 'proofread' && Array.isArray(res.data.changes)
                ? res.data.changes
                : undefined,
        },
    };
}
//# sourceMappingURL=coauthor-client.js.map