# 073: Update Local Data
# Modify stored data

# Load existing data
todos = load "todos" locally or []

# Add new todo
add_todo(text):
  new_todo = {
    id: generate_id(),
    text: text,
    done: false,
    created_at: now()
  }

  todos.append(new_todo)
  store todos locally as "todos"
  show "Todo added"

# Mark as done
complete_todo(id):
  todos = todos.map((todo) => {
    if todo.id == id:
      { ...todo, done: true }
    else:
      todo
  })

  store todos locally as "todos"
  show "Todo completed"

# Delete todo
delete_todo(id):
  todos = todos.filter((todo) => todo.id != id)
  store todos locally as "todos"
  show "Todo deleted"

# Usage
button "Add Todo" -> add_todo("New task")
button "Complete #1" -> complete_todo(1)
button "Delete #2" -> delete_todo(2)

generate_id():
  return Math.random() * 1000000
