const refrigerationCalculator = require('../../../tools/refrigeration/index.js')

const handlers = {
  setCoolingState(changedData) {
    this.setData(refrigerationCalculator.updateState(this.data, changedData))
  },

  onCoolingModeTap(event) {
    const mode = event.currentTarget.dataset.mode
    const modeIndex = (this.data.coolingModeOptions || []).findIndex((item) => item.key === mode)
    if (modeIndex < 0) return

    this.setCoolingState({
      coolingModeIndex: modeIndex
    })
  },

  onCoolingModeChange(event) {
    this.setCoolingState({
      coolingModeIndex: Number(event.detail.value)
    })
  },

  onCoolingInput(event) {
    const field = event.currentTarget.dataset.field
    if (!field) return

    this.setCoolingState({
      [field]: event.detail.value
    })
  },

  clearCoolingInputs() {
    this.setData(refrigerationCalculator.clearInputs(this.data))
  }
}

module.exports = {
  handlers
}
