const Client = require('../Client')

/**
 * @typedef {object} AppsResult
 * @property {Array<{object}>} apps
 */

/**
 * Agent class
 */
class AgentCLI extends Client {
  agentBasePath = () => `v1/instances/${this.instance}/agent/v1`

  ready = async () => await this._fetch('GET', `${this.agentBasePath()}/app/ready/`, {})

  /**
   * List files on device
   * @returns {Promise<Array<{object}>>}
   */
  listFiles = async () => {
    const response = (await this._fetch('GET', `${this.agentBasePath()}/file/device/`, {}))
    // Trim whitespace from file names
    return response.entries.map(f => { return { ...f, name: f.name.trim() } })
  }

  /**
   * List apps on device
   * @returns {Promise<Array<{apps: Array<{object}>}>>}
   */
  listApps = async (options) => {
    const { loadIcons } = options
    const response = (await this._fetch('GET', `${this.agentBasePath()}/app/apps?loadIcons=${loadIcons ? 1 : 0}`, {}))
    return response ? response.apps : []
  }
}

module.exports = AgentCLI
