{
  "$schema": "./.schemas/page.schema.json",
  "title": "Library",
  "icon": "books.vertical",
  "sidebar": false,
  "params": {
    "id": {
      "pattern": "[A-Za-z0-9-]{1,64}",
      "example": "6f1c2a3b-4d5e-4f60-8a7b-9c0d1e2f3a4b"
    }
  },
  "title_from": {
    "ref": "lib",
    "value": {
      "first": "name",
      "of": "libraries.libraries",
      "where": {
        "path": "id",
        "equals": "{param.id}"
      }
    }
  },
  "sources": {
    "lib": {
      "url": "http://audiobookshelf.local:13378",
      "secret": "audiobookshelf",
      "auth": {
        "type": "bearer"
      },
      "every": "15m",
      "requests": {
        "libraries": "/api/libraries",
        "library": "/api/libraries/{param.id}",
        "stats": "/api/libraries/{param.id}/stats",
        "recent": "/api/libraries/{param.id}/items?sort=addedAt&desc=1&limit=15&minified=1",
        "episodes": {
          "path": "/api/libraries/{libraryId}/recent-episodes?limit=15",
          "each_row": {
            "from": "library.folders",
            "key": "libraryId",
            "max": 1,
            "pick": [
              "episodes"
            ]
          }
        }
      },
      "derive": [
        {
          "name": "kind",
          "value": {
            "first": "mediaType",
            "of": "libraries.libraries",
            "where": {
              "path": "id",
              "equals": "{param.id}"
            }
          }
        },
        {
          "name": "hours",
          "value": {
            "divide": [
              "stats.totalDuration",
              3600
            ],
            "round": 0
          }
        },
        {
          "name": "eps_sent",
          "value": {
            "if": {
              "path": "episodes[0].answer.episodes",
              "exists": true
            },
            "then": {
              "template": "yes"
            },
            "else": {
              "template": "no"
            }
          }
        },
        {
          "name": "eps",
          "value": {
            "each": "episodes",
            "where": {
              "path": "key",
              "equals": "{param.id}"
            },
            "flatten": "answer.episodes",
            "limit": 15,
            "fields": {
              "id": "id",
              "item": "libraryItemId",
              "title": "title",
              "by": "podcast.metadata.title",
              "at": "addedAt",
              "cover": "podcast.coverPath"
            }
          }
        }
      ]
    }
  },
  "cards": [
    {
      "id": "books",
      "type": "facts",
      "label": "In this library",
      "items": [
        {
          "key": "Books",
          "value": "—"
        },
        {
          "key": "Length",
          "value": "—"
        },
        {
          "key": "Authors",
          "value": "—"
        },
        {
          "key": "Size",
          "value": "—"
        }
      ],
      "source": {
        "ref": "lib",
        "when": {
          "path": "kind",
          "not": "podcast"
        },
        "map": {
          "items[0].value": "stats.totalItems",
          "items[1].value": {
            "template": "{hours} h"
          },
          "items[2].value": "stats.totalAuthors",
          "items[3].value": {
            "template": "{stats.totalSize|bytes}"
          }
        }
      }
    },
    {
      "id": "podcasts",
      "type": "facts",
      "label": "In this library",
      "items": [
        {
          "key": "Podcasts",
          "value": "—"
        },
        {
          "key": "Episodes",
          "value": "—"
        },
        {
          "key": "Length",
          "value": "—"
        },
        {
          "key": "Size",
          "value": "—"
        }
      ],
      "source": {
        "ref": "lib",
        "when": {
          "path": "kind",
          "equals": "podcast"
        },
        "map": {
          "items[0].value": "stats.totalItems",
          "items[1].value": "stats.numAudioTracks",
          "items[2].value": {
            "template": "{hours} h"
          },
          "items[3].value": {
            "template": "{stats.totalSize|bytes}"
          }
        }
      }
    },
    {
      "id": "recent",
      "type": "list",
      "label": "Recently added",
      "max_items": 7,
      "image_shape": "square",
      "items": [],
      "source": {
        "ref": "lib",
        "when": {
          "path": "kind",
          "not": "podcast"
        },
        "empty": {
          "text": "Nothing in this library yet",
          "when": {
            "value": {
              "count": "recent.results",
              "default": 0
            },
            "equals": 0
          }
        },
        "map": {
          "items": {
            "each": "recent.results",
            "fields": {
              "key": "id",
              "text": "media.metadata.title",
              "detail": {
                "template": "{media.metadata.authorName} · {addedAt|ago}"
              },
              "image": {
                "if": {
                  "path": "media.coverPath",
                  "exists": true
                },
                "then": {
                  "template": "/api/items/{id}/cover?width=144&format=jpeg"
                },
                "else": {
                  "template": ""
                }
              },
              "icon": {
                "template": "headphones"
              },
              "url": {
                "template": "http://audiobookshelf.local:13378/item/{id}"
              }
            }
          }
        }
      }
    },
    {
      "id": "episodes",
      "type": "list",
      "label": "Recent episodes",
      "max_items": 7,
      "image_shape": "square",
      "items": [],
      "source": {
        "ref": "lib",
        "when": {
          "all": [
            {
              "path": "kind",
              "equals": "podcast"
            },
            {
              "path": "eps_sent",
              "equals": "yes"
            }
          ]
        },
        "empty": {
          "text": "No episodes yet",
          "when": {
            "value": {
              "count": "eps",
              "default": 0
            },
            "equals": 0
          }
        },
        "map": {
          "items": {
            "each": "eps",
            "fields": {
              "key": "id",
              "text": "title",
              "detail": {
                "template": "{by} · {at|ago}"
              },
              "image": {
                "if": {
                  "path": "cover",
                  "exists": true
                },
                "then": {
                  "template": "/api/items/{item}/cover?width=144&format=jpeg"
                },
                "else": {
                  "template": ""
                }
              },
              "icon": {
                "template": "antenna.radiowaves.left.and.right"
              },
              "url": {
                "template": "http://audiobookshelf.local:13378/item/{item}"
              }
            }
          }
        }
      }
    },
    {
      "id": "episodes_missing",
      "type": "text",
      "tone": "info",
      "body": "Audiobookshelf didn’t send this library’s episodes this time.",
      "source": {
        "ref": "lib",
        "when": {
          "all": [
            {
              "path": "kind",
              "equals": "podcast"
            },
            {
              "path": "eps_sent",
              "equals": "no"
            }
          ]
        }
      }
    }
  ]
}
