'use strict'; const Parser = require('../contents'); const History = require('./History'); const { observe } = require('../../utils/Utils'); /** @namespace */ class Library { #actions; #page; /** * @param {object} response - API response. * @param {import('../../core/Actions')} actions */ constructor(response, actions) { this.#actions = actions; this.#page = Parser.parseResponse(response); const tab = this.#page.contents.tabs.get({ selected: true }); const shelves = tab.content.contents.map((section) => section.contents[0]); const stats = this.#page.contents.secondary_contents.items.get({ type: 'ProfileColumnStats' }).items; const user_info = this.#page.contents.secondary_contents.items.get({ type: 'ProfileColumnUserInfo' }); this.profile = { stats, user_info }; this.sections = observe(shelves.map((shelf) => ({ title: shelf.title.toString(), items: shelf.content.items, type: shelf.icon_type, getAll: () => this.#getAll(shelf) }))); } async #getAll(shelf) { if (!shelf.menu?.top_level_buttons) throw new Error('The ' + shelf.title.text + ' section doesn\'t have more items'); const button = await shelf.menu.top_level_buttons.get({ text: 'See all' }); const page = await button.endpoint.call(this.#actions); switch (shelf.icon_type) { case 'WATCH_HISTORY': return new History(page, this.#actions); case 'WATCH_LATER': // TODO break; case 'LIKE': // TODO break; case 'CONTENT_CUT': // TODO break; default: } } get history() { return this.sections.get({ type: 'WATCH_HISTORY' }) } get watch_later() { return this.sections.get({ type: 'WATCH_LATER' }); } get liked_videos() { return this.sections.get({ type: 'LIKE' }); } get playlists() { return this.sections.get({ type: 'PLAYLISTS' }); } get clips() { return this.sections.get({ type: 'CONTENT_CUT' }); } get page() { return this.#page; } } module.exports = Library;