text: import explorer from '~/api/explorer';
  import {padZero} from '~/assets/utils';
  import {REWARD_CHART_TYPES} from '~/assets/variables';
  
  
  /**
   * @typedef {Object} Status
   * @property {number} marketCap - in $
   * @property {number} bipPriceUsd
   * @property {number} bipPriceBtc
   * @property {number} bipPriceChange - in %
   * @property {number} latestBlockHeight - block count
   * @property {number} averageBlockTime - in seconds
   * @property {number} totalTransactions - tx count
   * @property {number} transactionsPerSecond - tps
   */
  
  /**
   * @return {Promise<Status>}
   */
  export function getStatus() {
      return explorer.get('status')
          .then((response) => response.data);
  }
  
  export function getTxChartData() {
      return explorer.get('txCountChartData')
          .then((response) => {
              let chartData = response.data.data;
              if (!Array.isArray(chartData)) {
                  throw new Error('Not valid response from api');
                  //chartData = [];
              }
  
              const COUNT_DISPLAY_DAYS = 15;
  
              let lastData = chartData.length > COUNT_DISPLAY_DAYS ? chartData.slice(0, COUNT_DISPLAY_DAYS - 1) : chartData;
  
              // prepend empty days if they are not present in the API response
              let daysToAdd = new Array(COUNT_DISPLAY_DAYS - lastData.length);
              if (daysToAdd.length) {
                  const DAY_MS = 24 * 60 * 60 * 1000;
                  const dataDate = lastData.length ? new Date(lastData[0].date) : new Date();
                  const firstDate = dataDate - daysToAdd.length * DAY_MS;
                  for (let i = 0; i < daysToAdd.length; i++) {
                      const iterationDate = new Date(firstDate + i * DAY_MS);
                      daysToAdd[i] = {
                          date: iterationDate.getUTCFullYear() + '-' + padZero(iterationDate.getUTCMonth() + 1) + '-' + padZero(iterationDate.getUTCDate()),
                          txCount: 0,
                      };
                  }
                  lastData = daysToAdd.concat(lastData);
              }
  
  
              // format data for line chart.js
              return lastData.reduce((accum, item) => {
                  accum.data.push(item.txCount);
                  accum.labels.push(item.date);
                  return accum;
              }, {data: [], labels: []});
          });
  }
  
  /**
   * @typedef {Object} BlockListInfo
   * @property {Array<Block>} data
   * @property {Object} meta - pagination
   */
  
  /**
   * @param {Object} [params]
   * @param {number} params.page
   * @return {Promise<BlockListInfo>}
   */
  export function getBlockList(params) {
      return explorer.get('blocks', {
              params,
          })
          .then((response) => response.data);
  }
  
  /**
   * @typedef {Object} BlockInfo
   * @property {Block} data
   * @property {Object} meta
   * @property {number} meta.latestBlockHeight
   */
  
  /**
   * @param {number} height
   * @return {Promise<Block>}
   */
  export function getBlock(height) {
      return explorer.get('block/' + height)
          .then((response) => response.data.data);
  }
  
  /**
   * @typedef {Object} TransactionListInfo
   * @property {Array<Transaction>} data
   * @property {Object} meta - pagination
   */
  
  /**
   * @param {Object} [params]
   * @param {number} [params.block]
   * @param {number} [params.address]
   * @param {number} [params.page]
   * @return {Promise<TransactionListInfo>}
   */
  export function getTransactionList(params) {
      return explorer.get('transactions', {params})
          .then((response) => response.data);
  }
  
  /**
   * @typedef {Object} RewardListInfo
   * @property {Array<Reward>} data
   * @property {Object} meta - pagination
   */
  
  /**
   * @param {Object} [params]
   * @param {number} [params.address]
   * @param {number} [params.page]
   * @return {Promise<RewardListInfo>}
   */
  export function getRewardList(params) {
      return explorer.get('events/rewards', {params})
          .then((response) => response.data);
  }
  
  /**
   * @typedef {Object} SlashListInfo
   * @property {Array<Slash>} data
   * @property {Object} meta - pagination
   */
  
  /**
   * @param {Object} [params]
   * @param {number} [params.address]
   * @param {number} [params.page]
   * @return {Promise<SlashListInfo>}
   */
  export function getSlashList(params) {
      return explorer.get('events/slashes', {params})
          .then((response) => response.data);
  }
  
  /**
   * @param {string} address
   * @param {string} type
   * @return {Promise<Array>}
   */
  export function getRewardChartData(address, type = REWARD_CHART_TYPES.MONTH) {
      let params;
      if (type === REWARD_CHART_TYPES.MONTH) {
          params = {scale: 'day'};
      } else if (type === REWARD_CHART_TYPES.WEEK) {
          params = {
              scale: 'day',
              startTime: (new Date(Date.now() - 6 * 24 * 60 * 60 *  1000)).toISOString(),
          };
      } else {
          params = {
              scale: 'hour',
              startTime: (new Date(Date.now() - 23 * 60 * 60 * 1000)).toISOString(),
          };
      }
      return explorer.get('events/rewards/chart/' + address, {params})
          .then((response) => {
              let chartData = response.data.data;
              if (!Array.isArray(chartData)) {
                  throw new Error('Not valid response from api');
              }
  
              // format data for line chart.js
              return chartData.reduce((accum, item) => {
                  accum.data.push(item.amount);
                  accum.labels.push(item.time.replace(' ', 'T') + ':00');
                  return accum;
              }, {data: [], labels: []});
          });
  }
  
  /**
   * @param {string} hash
   * @return {Promise<Transaction>}
   */
  export function getTransaction(hash) {
      return explorer.get('transaction/' + hash)
          .then((response) => response.data.data);
  }
  
  export function getAddress(address) {
      return explorer.get('address/' + address)
          .then((response) => response.data.data);
  }
  
  export function getWebSocketConnectData() {
      return explorer.get('settings/get-ws-data')
          .then((response) => response.data.data);
  }
  
  /**
   * @typedef {Object} Block
   * @property {number} height
   * @property {string} timestamp
   * @property {number} txCount
   * @property {number} size
   * @property {string} hash
   * @property {number} reward
   * @property {number} blockTime
   * @property {string} timestamp
   * @property {Array<Validator>} validators
   */
  
  /**
   * @typedef {Object} Validator
   * @property {number} id
   * @property {string} name
   * @property {string} address
   * @property {string} publicKey
   */
  
  /**
   * @typedef {Object} Transaction
   * @property {number} txn
   * @property {string} hash
   * @property {string} status
   * @property {number} nonce
   * @property {number} block
   * @property {string} from
   * @property {string} timestamp
   * @property {number} fee
   * @property {number} type
   * @property {Object} data
   * -- type: TX_TYPES.SEND
   * @property {string} [data.to]
   * @property {string} [data.coin]
   * @property {number} [data.amount]
   * -- type: TX_TYPES.CONVERT
   * @property {string} [data.coin_to_sell]
   * @property {string} [data.coin_to_buy]
   * @property {number} [data.value_to_sell]
   * @property {number} [data.value_to_buy]
   * -- type: TX_TYPES.CREATE_COIN
   * @property {string} [data.name]
   * @property {string} [data.symbol]
   * @property {number} [data.initial_amount]
   * @property {number} [data.initial_reserve]
   * @property {number} [data.constant_reserve_ratio]
   * -- type: TX_TYPES.DECLARE_CANDIDACY
   * @property {string} [data.address]
   * @property {string} [data.pub_key]
   * @property {number} [data.commission]
   * @property {string} [data.coin]
   * @property {number} [data.stake]
   * -- type: TX_TYPES.DELEGATE
   * @property {string} [data.pub_key]
   * @property {string} [data.coin]
   * @property {number} [data.stake]
   * -- type: TX_TYPES.UNBOND
   * @property {string} [data.pub_key]
   * @property {string} [data.coin]
   * @property {number} [data.value]
   * -- type: TX_TYPES.REDEEM_CHECK
   * @property {string} [data.raw_check]
   * @property {string} [data.proof]
   * - type: TX_TYPES.SET_CANDIDATE_ONLINE, TX_TYPES.SET_CANDIDATE_OFFLINE
   * @property {string} [data.pub_key]
   */
  
  
  /**
   * @typedef {Object} Reward
   * @property {number} block
   * @property {string} timestamp
   * @property {string} role
   * @property {string} address
   * @property {string} validator
   * @property {number} amount
   */
  
  /**
   * @typedef {Object} Slash
   * @property {number} block
   * @property {string} timestamp
   * @property {string} address
   * @property {string} validator
   * @property {number} amount
   * @property {string} coin
   */
  
  