import {
  send,
  update
} from '@/api/base'
import {
  AlarmStrategy,
  AlarmStrategies
} from '@/constant'

const AlarmLevel = {
  0: '提示性预警',
  1: '中级预警',
  2: '紧急预警'
}

export function getInformStrategy (tenant) {
  return send({
    url: '/device/informStrategy',
    method: 'GET',
    params: { tenant }
  }).then(({ data: { strategy } }) => Object.keys(strategy).sort().map(key => {
    return {
      level: key,
      name: AlarmLevel[key],
      options: getStrategyOptions(strategy[key])
    }
  }))
}

function getStrategyOptions (strategy) {
  return AlarmStrategies.map(key => {
    return {
      key,
      label: AlarmStrategy[key],
      value: strategy[key]
    }
  })
}

export function updateInformStrategy (tenant, strategy) {
  return update({
    url: '/device/informStrategy',
    method: 'POST',
    data: {
      tenant,
      strategy
    }
  })
}
