package com.tt.honeyDue.storage

import kotlinx.browser.localStorage

/**
 * WASM implementation of TaskCacheManager using browser's localStorage.
 * Note: Web doesn't have widgets, so dirty flag methods use localStorage for consistency.
 */
actual class TaskCacheManager {
    actual fun saveTasks(tasksJson: String) {
        localStorage.setItem(KEY_TASKS, tasksJson)
    }

    actual fun getTasks(): String? {
        return localStorage.getItem(KEY_TASKS)
    }

    actual fun clearTasks() {
        localStorage.removeItem(KEY_TASKS)
    }

    /**
     * Check if tasks need refresh. Web doesn't have widgets, always returns false.
     */
    actual fun areTasksDirty(): Boolean {
        return localStorage.getItem(KEY_DIRTY_FLAG) == "true"
    }

    /**
     * Mark tasks as dirty. Web doesn't have widgets but supports the interface.
     */
    actual fun markTasksDirty() {
        localStorage.setItem(KEY_DIRTY_FLAG, "true")
    }

    /**
     * Clear the dirty flag.
     */
    actual fun clearDirtyFlag() {
        localStorage.setItem(KEY_DIRTY_FLAG, "false")
    }

    companion object {
        private const val KEY_TASKS = "cached_tasks"
        private const val KEY_DIRTY_FLAG = "tasks_dirty"

        private var instance: TaskCacheManager? = null

        fun getInstance(): TaskCacheManager {
            if (instance == null) {
                instance = TaskCacheManager()
            }
            return instance!!
        }
    }
}
