// Picking what a page needs out of a large API answer with JSONPath.
//
//   quillo run jsonpath.qwe      then open http://localhost:8080/cheap

use "jsonpath.qwer"

let STORE = {
    store: {
        books: [
            { title: "Sayings of the Century", author: "Nigel Rees", price: 8.95, tags: ["quotes"] },
            { title: "Sword of Honour", author: "Evelyn Waugh", price: 12.99, tags: ["war", "fiction"] },
            { title: "Moby Dick", author: "Herman Melville", price: 8.99, isbn: "0-553-21311-3", tags: ["sea", "fiction"] },
            { title: "The Lord of the Rings", author: "J. R. R. Tolkien", price: 22.99, isbn: "0-395-19395-8", tags: ["fantasy", "fiction"] },
        ],
        bicycle: { colour: "red", price: 399 },
    },
}

route GET /cheap = jsonpath(STORE, "$.store.books[?@.price < 10].title")?

route GET /query ? { q: str = "$..author" } = { q, found: jsonpath(STORE, q)? }

route GET /where = {
    with_isbn: jsonpath_located(STORE, "$.store.books[?@.isbn]")? | map((n) => n.pointer),
    first_over_10: jsonpath_first(STORE, "$.store.books[?@.price > 10].title")?,
    authors_h: jsonpath(STORE, "$.store.books[?match(@.author, 'H.*')].title")?,
    two_tags: jsonpath(STORE, "$.store.books[?length(@.tags) == 2].author")?,
    bad: jsonpath(STORE, "$.store[").error,
}
