const threePhasePowerCalculator = require('../../../tools/three-phase-power/index.js')

const handlers = {
  setThreePhasePowerState(changedData) {
    this.setData(threePhasePowerCalculator.updateState(this.data, changedData))
  },

  onThreePhaseConnectionTap(event) {
    const connection = event.currentTarget.dataset.connection
    const connectionIndex = (this.data.threePhaseConnectionOptions || []).findIndex((item) => item.key === connection)
    if (connectionIndex < 0) return

    this.setThreePhasePowerState({
      threePhaseConnectionIndex: connectionIndex
    })
  },

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

    const changedData = {
      [field]: event.detail.value
    }
    if (threePhasePowerCalculator.ELECTRICAL_INPUT_KEYS.includes(field)) {
      changedData.threePhaseElectricalDriver = field
    }
    if (threePhasePowerCalculator.POWER_DRIVER_KEYS.includes(field)) {
      changedData.threePhasePowerDriver = field
    }

    this.setThreePhasePowerState(changedData)
  },

  clearThreePhaseInputs() {
    this.setData(threePhasePowerCalculator.clearInputs(this.data))
  }
}

module.exports = {
  handlers
}
