import { inferTaskPhaseKey } from "./phase-resolution.js";
function isRecord(value) {
    return typeof value === "object" && value !== null && !Array.isArray(value);
}
function normalizeStringList(values) {
    if (typeof values === "string") {
        const trimmed = values.trim();
        return trimmed.length > 0 ? [trimmed] : [];
    }
    if (!Array.isArray(values)) {
        return [];
    }
    const out = [];
    for (const entry of values) {
        if (typeof entry !== "string") {
            continue;
        }
        const trimmed = entry.trim();
        if (trimmed.length > 0) {
            out.push(trimmed);
        }
    }
    return Array.from(new Set(out));
}
export function featureDetailsForTask(slugs, enrich) {
    if (!slugs?.length) {
        return null;
    }
    const out = [];
    for (const slug of slugs) {
        const row = enrich.get(slug);
        if (row) {
            out.push({
                slug: row.slug,
                name: row.name,
                componentId: row.componentId,
                componentDisplayName: row.componentDisplayName
            });
        }
    }
    return out.length > 0 ? out : null;
}
export function readTaskSeverity(task) {
    if (!isRecord(task.metadata)) {
        return null;
    }
    const severity = task.metadata.severity;
    return typeof severity === "string" && severity.trim().length > 0 ? severity.trim() : null;
}
export function readTaskComponents(task, featureDetails) {
    if (isRecord(task.metadata)) {
        const explicit = normalizeStringList(task.metadata.components);
        if (explicit.length > 0) {
            return explicit;
        }
    }
    if (!featureDetails?.length) {
        return [];
    }
    return Array.from(new Set(featureDetails
        .map((detail) => detail.componentDisplayName || detail.componentId)
        .filter((value) => typeof value === "string" && value.trim().length > 0)));
}
export function projectTaskReadEntity(task, enrich) {
    const featureDetails = featureDetailsForTask(task.features, enrich);
    return {
        ...task,
        severity: readTaskSeverity(task),
        components: readTaskComponents(task, featureDetails)
    };
}
export function projectDashboardTaskRow(task, enrich, options) {
    const featureDetails = featureDetailsForTask(task.features, enrich);
    const row = {
        id: task.id,
        title: task.title,
        phase: task.phase ?? null,
        phaseKey: inferTaskPhaseKey(task),
        severity: readTaskSeverity(task),
        components: readTaskComponents(task, featureDetails),
        features: task.features?.length ? task.features : null,
        featureDetails
    };
    if (options?.includePriority !== false) {
        row.priority = task.priority ?? null;
    }
    return row;
}
