• Calculate the effective status for a manga entry, considering auto-pause settings. If auto-pause is enabled and inactivity threshold is exceeded, returns PAUSED status.

    Parameters

    • kenmei: KenmeiMangaData

      Kenmei manga data with status and activity timestamps.

    • syncConfig: SyncConfig

      Sync configuration with auto-pause settings.

    Returns MediaListStatus

    The effective AniList media status (may be auto-paused).

    Does not throw; logs warnings for invalid dates or thresholds.

    export function getEffectiveStatus(
    kenmei: KenmeiMangaData,
    syncConfig: SyncConfig,
    ): MediaListStatus {
    // Check if manga should be auto-paused due to inactivity
    const lastActivity = kenmei.lastReadAt || kenmei.updatedAt;
    if (
    syncConfig.autoPauseInactive &&
    kenmei.status.toLowerCase() !== "completed" &&
    kenmei.status.toLowerCase() !== "dropped" &&
    lastActivity
    ) {
    // Calculate how many days since the last activity
    const lastUpdated = new Date(lastActivity);

    // Validate the parsed date
    if (Number.isNaN(lastUpdated.getTime())) {
    console.warn(
    `[Auto-Pause Warning] Title: "${kenmei.title}" | Invalid date format: ${lastActivity}`,
    );
    return STATUS_MAPPING[kenmei.status as KenmeiStatus];
    }

    const daysSinceUpdate = Math.floor(
    (Date.now() - lastUpdated.getTime()) / (1000 * 60 * 60 * 24),
    );

    // Additional validation
    if (daysSinceUpdate < 0) {
    console.warn(
    `[Auto-Pause Warning] Title: "${kenmei.title}" | Negative days calculated (future date): ${daysSinceUpdate}`,
    );
    return STATUS_MAPPING[kenmei.status as KenmeiStatus];
    }

    // Check if using a custom threshold (not in predefined list)
    const isCustomThreshold = ![1, 7, 14, 30, 60, 90, 180, 365].includes(
    syncConfig.autoPauseThreshold,
    );
    const threshold = isCustomThreshold
    ? (syncConfig.customAutoPauseThreshold ?? 30)
    : syncConfig.autoPauseThreshold;

    // Validate threshold value
    const validThreshold =
    typeof threshold === "number" && threshold > 0 ? threshold : 30;
    if (validThreshold !== threshold) {
    console.warn(
    `[Auto-Pause Warning] Title: "${kenmei.title}" | Invalid threshold value: ${threshold}, using fallback: ${validThreshold}`,
    );
    }

    if (daysSinceUpdate >= validThreshold) {
    return "PAUSED";
    }
    }

    // Otherwise use the normal status mapping
    // Use type assertion for safety
    const status = kenmei.status as KenmeiStatus;
    return STATUS_MAPPING[status];
    }