/** Wishlist identifiers use a dedicated namespace: `W` + digits (e.g. `W1`, `W42`). */
export const WISHLIST_ID_RE = /^W\d+$/;
const REQUIRED_STRING_FIELDS = [
    "title",
    "problemStatement",
    "expectedOutcome",
    "impact",
    "constraints",
    "successSignals",
    "requestor",
    "evidenceRef"
];
function nonEmptyString(v, label) {
    if (typeof v !== "string" || v.trim().length === 0) {
        return null;
    }
    return v.trim();
}
/**
 * Validates intake fields for creating or replacing content on an open wishlist item.
 * Wishlist items never carry a Task Engine `phase`; reject if present.
 */
/**
 * Validates required wishlist intake strings without requiring a `W###` id (Phase 24 task-backed intake).
 */
export function validateWishlistContentFields(args) {
    const errors = [];
    if ("phase" in args && args.phase !== undefined) {
        errors.push("Wishlist intake must not include 'phase'; only canonical phased tasks use phase.");
    }
    for (const key of REQUIRED_STRING_FIELDS) {
        const s = nonEmptyString(args[key], key);
        if (s === null) {
            errors.push(`Wishlist '${key}' is required and must be a non-empty string.`);
        }
    }
    if (errors.length > 0) {
        return { ok: false, errors };
    }
    return { ok: true };
}
export function validateWishlistIntakePayload(args) {
    const errors = [];
    if ("phase" in args && args.phase !== undefined) {
        errors.push("Wishlist items must not include 'phase'; only canonical tasks are phased.");
    }
    const id = typeof args.id === "string" ? args.id.trim() : "";
    if (!id) {
        errors.push("Wishlist 'id' is required.");
    }
    else if (!WISHLIST_ID_RE.test(id)) {
        errors.push(`Wishlist 'id' must match ${WISHLIST_ID_RE.source} (e.g. W1).`);
    }
    for (const key of REQUIRED_STRING_FIELDS) {
        const s = nonEmptyString(args[key], key);
        if (s === null) {
            errors.push(`Wishlist '${key}' is required and must be a non-empty string.`);
        }
    }
    if (errors.length > 0) {
        return { ok: false, errors };
    }
    return { ok: true };
}
export function buildWishlistItemFromIntake(args, timestamp) {
    const id = args.id.trim();
    const item = {
        id,
        status: "open",
        title: args.title.trim(),
        problemStatement: args.problemStatement.trim(),
        expectedOutcome: args.expectedOutcome.trim(),
        impact: args.impact.trim(),
        constraints: args.constraints.trim(),
        successSignals: args.successSignals.trim(),
        requestor: args.requestor.trim(),
        evidenceRef: args.evidenceRef.trim(),
        createdAt: timestamp,
        updatedAt: timestamp
    };
    return item;
}
export function validateWishlistUpdatePayload(updates) {
    if ("phase" in updates && updates.phase !== undefined) {
        return { ok: false, errors: ["Wishlist updates cannot set 'phase'."] };
    }
    const errors = [];
    const allowed = new Set([
        "title",
        "problemStatement",
        "expectedOutcome",
        "impact",
        "constraints",
        "successSignals",
        "requestor",
        "evidenceRef"
    ]);
    for (const key of Object.keys(updates)) {
        if (!allowed.has(key)) {
            errors.push(`Cannot update unknown or immutable wishlist field '${key}'.`);
        }
    }
    for (const key of REQUIRED_STRING_FIELDS) {
        if (key in updates) {
            const s = nonEmptyString(updates[key], key);
            if (s === null) {
                errors.push(`Wishlist '${key}' must be a non-empty string when provided.`);
            }
        }
    }
    if (errors.length > 0) {
        return { ok: false, errors };
    }
    return { ok: true };
}
