{
  "$schema": "./.schemas/page.schema.json",
  "title": "Library",
  "icon": "rectangle.stack",
  "sidebar": false,
  "params": {
    "id": {
      "type": "integer",
      "example": "1"
    }
  },
  "title_from": {
    "ref": "lib",
    "value": {
      "first": "title",
      "of": "sections.MediaContainer.Directory",
      "where": {
        "path": "key",
        "equals": "{param.id}"
      }
    }
  },
  "sources": {
    "lib": {
      "url": "http://plex.local:32400",
      "secret": "plex",
      "auth": {
        "type": "header",
        "name": "X-Plex-Token"
      },
      "headers": {
        "Accept": "application/json",
        "X-Plex-Client-Identifier": "legato-local-page"
      },
      "every": "15m",
      "requests": {
        "sections": "/library/sections",
        "items": "/library/sections/{param.id}/all?X-Plex-Container-Start=0&X-Plex-Container-Size=0",
        "recent": "/library/sections/{param.id}/recentlyAdded",
        "server": "/"
      },
      "derive": [
        {
          "name": "kind",
          "value": {
            "first": "type",
            "of": "sections.MediaContainer.Directory",
            "where": {
              "path": "key",
              "equals": "{param.id}"
            }
          }
        },
        {
          "name": "folder",
          "value": {
            "first": "Location",
            "of": "sections.MediaContainer.Directory",
            "where": {
              "path": "key",
              "equals": "{param.id}"
            }
          }
        },
        {
          "name": "machine_movie",
          "value": {
            "each": "server.MediaContainer",
            "entries": true,
            "where": {
              "path": "key",
              "equals": "machineIdentifier"
            },
            "fields": {
              "type": {
                "template": "movie"
              },
              "id": "value"
            }
          }
        },
        {
          "name": "machine_show",
          "value": {
            "each": "server.MediaContainer",
            "entries": true,
            "where": {
              "path": "key",
              "equals": "machineIdentifier"
            },
            "fields": {
              "type": {
                "template": "show"
              },
              "id": "value"
            }
          }
        },
        {
          "name": "machine_season",
          "value": {
            "each": "server.MediaContainer",
            "entries": true,
            "where": {
              "path": "key",
              "equals": "machineIdentifier"
            },
            "fields": {
              "type": {
                "template": "season"
              },
              "id": "value"
            }
          }
        },
        {
          "name": "machine_episode",
          "value": {
            "each": "server.MediaContainer",
            "entries": true,
            "where": {
              "path": "key",
              "equals": "machineIdentifier"
            },
            "fields": {
              "type": {
                "template": "episode"
              },
              "id": "value"
            }
          }
        },
        {
          "name": "machine_artist",
          "value": {
            "each": "server.MediaContainer",
            "entries": true,
            "where": {
              "path": "key",
              "equals": "machineIdentifier"
            },
            "fields": {
              "type": {
                "template": "artist"
              },
              "id": "value"
            }
          }
        },
        {
          "name": "machine_album",
          "value": {
            "each": "server.MediaContainer",
            "entries": true,
            "where": {
              "path": "key",
              "equals": "machineIdentifier"
            },
            "fields": {
              "type": {
                "template": "album"
              },
              "id": "value"
            }
          }
        },
        {
          "name": "machine_track",
          "value": {
            "each": "server.MediaContainer",
            "entries": true,
            "where": {
              "path": "key",
              "equals": "machineIdentifier"
            },
            "fields": {
              "type": {
                "template": "track"
              },
              "id": "value"
            }
          }
        },
        {
          "name": "machine_a",
          "value": {
            "each": [
              "machine_movie",
              "machine_show",
              "machine_season",
              "machine_episode"
            ],
            "fields": {
              "type": "type",
              "id": "id"
            }
          }
        },
        {
          "name": "machine",
          "value": {
            "each": [
              "machine_a",
              "machine_artist",
              "machine_album",
              "machine_track"
            ],
            "fields": {
              "type": "type",
              "id": "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.MediaContainer.totalSize",
          "items[1].value": {
            "path": "kind",
            "values": {
              "movie": "Movies",
              "show": "Shows",
              "artist": "Music",
              "photo": "Photos"
            },
            "default": "—"
          },
          "items[2].value": "folder[0].path"
        }
      }
    },
    {
      "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.MediaContainer.Metadata",
              "default": 0
            },
            "equals": 0
          }
        },
        "map": {
          "items": {
            "each": "recent.MediaContainer.Metadata",
            "limit": 15,
            "lookup": {
              "from": "machine",
              "by": "type",
              "on": "type",
              "as": "m"
            },
            "fields": {
              "url": {
                "if": {
                  "path": "m.id",
                  "exists": true
                },
                "then": {
                  "template": "http://plex.local:32400/web/index.html#!/server/{m.id}/details?key=%2Flibrary%2Fmetadata%2F{ratingKey}"
                },
                "else": {
                  "template": ""
                }
              },
              "key": "ratingKey",
              "text": {
                "path": "type",
                "values": {
                  "episode": {
                    "template": "{grandparentTitle} · S{parentIndex|pad:2}E{index|pad:2}"
                  },
                  "season": {
                    "template": "{parentTitle} · {title}"
                  }
                },
                "default": {
                  "template": "{title}"
                }
              },
              "detail": {
                "template": "{addedAt|ago}"
              },
              "image": {
                "if": {
                  "all": [
                    {
                      "path": "type",
                      "equals": "episode"
                    },
                    {
                      "path": "grandparentThumb",
                      "exists": true
                    }
                  ]
                },
                "then": {
                  "template": "{grandparentThumb}"
                },
                "else": {
                  "if": {
                    "path": "thumb",
                    "exists": true
                  },
                  "then": {
                    "template": "{thumb}"
                  },
                  "else": {
                    "template": ""
                  }
                }
              },
              "image_shape": {
                "if": {
                  "path": "type",
                  "in": [
                    "album",
                    "track"
                  ]
                },
                "then": {
                  "template": "square"
                },
                "else": {
                  "template": "poster"
                }
              },
              "icon": {
                "path": "type",
                "values": {
                  "movie": "film",
                  "episode": "tv",
                  "show": "tv",
                  "season": "tv",
                  "track": "music.note",
                  "album": "music.note",
                  "artist": "music.note"
                },
                "default": "play.rectangle"
              }
            }
          }
        }
      }
    }
  ]
}
