import SwiftUI

struct Book: Identifiable, Hashable {
   let id = UUID()
   var title: String
   var author: String
   var cover: String
   var year: Int
   var selected: Bool
   
   var displayYear: String {
      get {
         return String(year)
      }
   }
}
@Observable class ApplicationData {
   var showPictures: Bool = true
   var showYear: Bool = true

   @ObservationIgnored var userData: [Book] {
      didSet {
         filterValues(search: "")
      }
   }
   var filteredItems: [Book] = []

   func filterValues(search: String) {
      if search.isEmpty {
         filteredItems = []
      } else {
         let list = userData.filter( { item in
            return item.title.localizedStandardContains(search)
         })
         filteredItems = list.sorted(by: { $0.title < $1.title })
      }
   }
   static let shared: ApplicationData = ApplicationData()
   private init() {
      userData = [
         Book(title: "Apple Intelligence for Masterminds", author: "J.D Gauchat", cover: "book1", year: 2026, selected: false),
         Book(title: "Steve Jobs", author: "Isaacson", cover: "book2", year: 2011, selected: false),
         Book(title: "Swift for Masterminds", author: "J.D Gauchat", cover: "book3", year: 2026, selected: false),
         Book(title: "The C Programming Language", author: "Kernighan", cover: "book4", year: 1988, selected: false),
         Book(title: "Being Digital", author: "Negroponte", cover: "book5", year: 1996, selected: false),
         Book(title: "Only the Paranoid Survive", author: "Grove", cover: "book6", year: 1999, selected: false),
         Book(title: "Accidental Empires", author: "Cringely", cover: "book7", year: 1996, selected: false),
         Book(title: "Bobby Fischer Teaches Chess", author: "Fischer", cover: "book8", year: 1982, selected: false),
         Book(title: "New Guide to Science", author: "Asimov", cover: "book9", year: 1993, selected: false),
         Book(title: "Christine", author: "King", cover: "book10", year: 1983, selected: false),
         Book(title: "IT", author: "King", cover: "book11", year: 1987, selected: false),
         Book(title: "Ending Aging", author: "Grey", cover: "book12", year: 2007, selected: false)
      ]
      filterValues(search: "")
   }
}