/** 液位仪 cmdType 与 Web 子页映射 */
export const OPW_CMD = {
  MAIN: 'common:opw',
  QUERY: 'common:opw:query',
  PASSTHROUGH: 'common:opw:passthrough'
}

export const OPW_SUB_PAGE = {
  main: OPW_CMD.MAIN,
  query: OPW_CMD.QUERY,
  passthrough: OPW_CMD.PASSTHROUGH
}

export const OPW_CMD_SUB_PAGE = {
  [OPW_CMD.MAIN]: 'main',
  [OPW_CMD.QUERY]: 'query',
  [OPW_CMD.PASSTHROUGH]: 'passthrough'
}

export const OPW_SUB_PAGE_TITLE = {
  main: '液位仪',
  query: '液位仪查询',
  passthrough: '液位仪透传'
}

export const OPW_BUTTON = {
  QUERY_A: 'common:opw:query:a',
  QUERY_B: 'common:opw:query:b',
  QUERY_485: 'common:opw:query:485',
  VIEW_RAW_LOG: 'common:opw:query:rawLog'
}

export const OPW_BAUDRATES = ['2400', '4800', '9600', '19200', '38400', '57600', '115200']

export const OPW_TANK_COLUMNS = [
  { prop: 'tankNo', label: '罐号', width: 70 },
  { prop: 'oilWaterVolume1', label: '油水体积1', width: 100 },
  { prop: 'oilWaterVolume2', label: '油水体积2', width: 100 },
  { prop: 'remainingVolume', label: '剩余体积', width: 100 },
  { prop: 'oilHeight', label: '油高', width: 70 },
  { prop: 'waterHeight', label: '水高', width: 70 },
  { prop: 'temperature', label: '温度', width: 70 },
  { prop: 'waterVolume', label: '水体积', width: 80 },
  { prop: 'serialPort', label: '串口', width: 70 },
  { prop: 'readTime', label: '读取时间', width: 150 }
]

/** 按 key 存在性合并 WS 数据：不存在不更新，null/'' 渲染为空 */
export function applyPartialFields(target, data, fields, defaults = {}) {
  if (!data || !target) {
    return
  }
  fields.forEach((key) => {
    if (!Object.prototype.hasOwnProperty.call(data, key)) {
      return
    }
    const val = data[key]
    target[key] = val == null || val === '' ? (defaults[key] != null ? defaults[key] : '') : val
  })
}
