{
  "$schema": "./.schemas/page.schema.json",
  "title": "Library",
  "icon": "rectangle.stack",
  "sidebar": false,
  "params": {
    "id": {
      "pattern": "[A-Za-z0-9-]{1,64}",
      "example": "f137a2dd21bbc1b99aa5c0f6bf02a805"
    }
  },
  "title_from": {
    "ref": "lib",
    "value": {
      "first": "Name",
      "of": "folders",
      "where": {
        "path": "ItemId",
        "equals": "{param.id}"
      }
    }
  },
  "sources": {
    "lib": {
      "url": "http://jellyfin.local:8096",
      "secret": "jellyfin",
      "auth": {
        "type": "header",
        "name": "Authorization",
        "value": "MediaBrowser Token=\"{secret}\""
      },
      "every": "15m",
      "requests": {
        "folders": "/Library/VirtualFolders",
        "items": "/Items?ParentId={param.id}&Recursive=true&Limit=0&EnableTotalRecordCount=true&IncludeItemTypes=Movie,Series,MusicAlbum,Book,MusicVideo",
        "recent": "/Items?ParentId={param.id}&Recursive=true&SortBy=DateCreated&SortOrder=Descending&IncludeItemTypes=Movie,Episode,MusicAlbum,Book,MusicVideo&Fields=DateCreated&Limit=15"
      },
      "derive": [
        {
          "name": "folder",
          "value": {
            "first": "Locations",
            "of": "folders",
            "where": {
              "path": "ItemId",
              "equals": "{param.id}"
            }
          }
        },
        {
          "name": "kind",
          "value": {
            "first": "CollectionType",
            "of": "folders",
            "where": {
              "path": "ItemId",
              "equals": "{param.id}"
            }
          }
        }
      ]
    }
  },
  "cards": [
    {
      "id": "facts",
      "type": "facts",
      "label": "In this library",
      "items": [
        {
          "key": "Titles",
          "value": "—"
        },
        {
          "key": "Kind",
          "value": "—"
        },
        {
          "key": "Folder",
          "value": "—"
        }
      ],
      "source": {
        "ref": "lib",
        "map": {
          "items[0].value": "items.TotalRecordCount",
          "items[1].value": {
            "path": "kind",
            "values": {
              "movies": "Movies",
              "tvshows": "Shows",
              "music": "Music",
              "books": "Books",
              "musicvideos": "Music videos",
              "homevideos": "Home videos",
              "mixed": "Mixed"
            },
            "default": "—"
          },
          "items[2].value": "folder[0]"
        }
      }
    },
    {
      "id": "recent",
      "type": "list",
      "label": "Recently added",
      "max_items": 7,
      "items": [],
      "source": {
        "ref": "lib",
        "empty": {
          "text": "Nothing in this library yet",
          "when": {
            "value": {
              "count": "recent.Items",
              "default": 0
            },
            "equals": 0
          }
        },
        "map": {
          "items": {
            "each": "recent.Items",
            "fields": {
              "key": "Id",
              "text": {
                "if": {
                  "path": "Type",
                  "equals": "Episode"
                },
                "then": {
                  "if": {
                    "path": "IndexNumber",
                    "exists": true
                  },
                  "then": {
                    "template": "{SeriesName} · S{ParentIndexNumber|pad:2}E{IndexNumber|pad:2}"
                  },
                  "else": {
                    "template": "{SeriesName} · {Name}"
                  }
                },
                "else": {
                  "if": {
                    "path": "Type",
                    "equals": "Audio"
                  },
                  "then": "Name",
                  "else": "Name"
                }
              },
              "detail": {
                "template": "{DateCreated|ago}"
              },
              "image": {
                "if": {
                  "path": "Type",
                  "equals": "Episode"
                },
                "then": {
                  "if": {
                    "path": "SeriesPrimaryImageTag",
                    "exists": true
                  },
                  "then": {
                    "template": "/Items/{SeriesId}/Images/Primary?fillHeight=144&quality=85"
                  },
                  "else": {
                    "template": ""
                  }
                },
                "else": {
                  "if": {
                    "path": "Type",
                    "equals": "Audio"
                  },
                  "then": {
                    "if": {
                      "path": "AlbumPrimaryImageTag",
                      "exists": true
                    },
                    "then": {
                      "template": "/Items/{AlbumId}/Images/Primary?fillHeight=144&quality=85"
                    },
                    "else": {
                      "template": ""
                    }
                  },
                  "else": {
                    "if": {
                      "path": "ImageTags.Primary",
                      "exists": true
                    },
                    "then": {
                      "template": "/Items/{Id}/Images/Primary?fillHeight=144&quality=85"
                    },
                    "else": {
                      "template": ""
                    }
                  }
                }
              },
              "icon": {
                "path": "Type",
                "values": {
                  "Movie": "film",
                  "Episode": "tv",
                  "Series": "tv",
                  "Audio": "music.note",
                  "MusicAlbum": "music.note",
                  "Book": "book",
                  "MusicVideo": "music.note.tv"
                },
                "default": "play.rectangle"
              },
              "image_shape": {
                "if": {
                  "path": "Type",
                  "equals": "MusicAlbum"
                },
                "then": {
                  "template": "square"
                },
                "else": {
                  "template": "poster"
                }
              },
              "url": {
                "template": "http://jellyfin.local:8096/web/#/details?id={Id}"
              }
            }
          }
        }
      }
    }
  ]
}
