import {scandApiRequest} from '@helpers/apiHelpers.js'
import {Adapter} from '1-toolkits-helpers'

const {convertList} = Adapter

const machinesConfig = {
  zav_nomer: 'zavNomer',
  type: 'type',
  railway_name: 'railwayName',
  org_name: 'orgName',
  nomer_zn8: 'nomerZn8',
  machine_type: 'machineType',
  machine_id: 'machineId',
  imei: 'imei',
  device_number: 'deviceNumber',
  device_id: 'deviceId' 
}

const lastMachinesConfig = {
  pack_number: 'packNumber',
  type: 'type',
  machine_type: 'machineType',
  pack_dt: 'packDt',
  zav_nomer: 'zavNomer',
  machine_id: 'machineId',
  device_id: 'deviceId',
  imei: 'imei',
  railway_name: 'railwayName',
  org_name: 'orgName',
  nomer_zn8: 'nomerZn8',
  device_number: 'deviceNumber',
}

class AdapterOfMachines {

  /**
   *
   * @param {String} url
   */
  constructor(url) {
    this.url = url
  }

  /**
   * 
   * @returns {Promise<Array>}
   */
  async fetchModalMachines() {
    let result = await scandApiRequest(this.url, 'ScandApi.LiveMonitor.Machines', 'select_machines', [])
    result = convertList(result, {
      config: machinesConfig
    })
    return result
  }

  /**
   * 
   * @param {Array} packsNumbers
   * @returns {Promise<Array>}
   */
  async fetchLastMachinesByPackNum(packsNumbers) {
    let result = await scandApiRequest(this.url, 'ScandApi.LiveMonitor.Packs', 'get_last_machines_by_packs', packsNumbers)
    result = convertList(result, {
      config: lastMachinesConfig
    })
    return result
  }

}

export default AdapterOfMachines