const smdCodeCalculator = require('../../../tools/smd-code/index.js')

const handlers = {
  setSmdCodeState(changedData) {
    this.setData(smdCodeCalculator.updateState(this.data, changedData))
  },

  onSmdKindTap(event) {
    const kind = event.currentTarget.dataset.kind
    const kindIndex = (this.data.smdKindOptions || []).findIndex((item) => item.key === kind)
    if (kindIndex < 0) return

    this.setSmdCodeState({
      smdFormatIndex: 0,
      smdFormatKey: '',
      smdKindIndex: kindIndex
    })
  },

  onSmdFormatTap(event) {
    const format = event.currentTarget.dataset.format
    const formatIndex = (this.data.smdFormatOptions || []).findIndex((item) => item.key === format)
    if (formatIndex < 0) return

    this.setSmdCodeState({
      smdFormatIndex: formatIndex,
      smdFormatKey: format
    })
  },

  onSmdCodeInput(event) {
    this.setSmdCodeState({
      smdCodeText: event.detail.value
    })
  },

  clearSmdCodeInput() {
    this.setSmdCodeState({
      smdCodeText: ''
    })
  }
}

module.exports = {
  handlers
}
