import { trimColType } from '@/utils/buildSentence'
import { sqlFunc } from '@/utils/configs'

const store = {
  debug: process.env.NODE_ENV === 'development',
  state: {
    dimensions: [],
    caculCols: [],
    caculCols2: [],
    allCols: []
  },
  // dimensions actions
  addDimensionAction(col) {
    if (this.debug) console.log('adddimensionAction triggered with', col)
    col.isDimension = true
  },
  deleteDimensionAction(col) {
    if (this.debug) console.log('deleteDimensionAction triggered with', col)
    const index = this.state.dimensions.findIndex(c => c.Column === col.Column)
    this.state.dimensions[index].isDimension = false
    this.state.dimensions.splice(index, 1)
  },
  setDimensionsAction(dimensions) {
    if (this.debug) console.log('setDimensionsAction triggered')
    this.state.dimensions = dimensions
  },
  // caculCols actions
  addCaculColAction(col) {
    if (this.debug) console.log('addCaculColAction triggered with', col)
    const colType = trimColType(col.Type)
    const index = this.state.caculCols.findIndex(item => item.Column === col.Column)
    const caculCol = {
      Column: col.Column,
      Comment: col.Comment,
      calculFunc: colType.availableFunc[0],
      Type: col.Type,
      availableFunc: colType.availableFunc.map(func => {
        return {
          name: sqlFunc[func],
          func
        }
      })
    }
    this.state.caculCols.splice(index, 1, caculCol)
  },
  addCaculColAction2(col) {
    if (this.debug) console.log('addCaculColAction2 triggered with', col)
    const colType = trimColType(col.Type)
    const index = this.state.caculCols2.findIndex(item => item.Column === col.Column)
    const caculCol2 = {
      Column: col.Column,
      Comment: col.Comment,
      calculFunc: colType.availableFunc[0],
      Type: col.Type,
      availableFunc: colType.availableFunc.map(func => {
        return {
          name: sqlFunc[func],
          func
        }
      })
    }
    this.state.caculCols2.splice(index, 1, caculCol2)
  },
  deleteCaculColAction(col) {
    if (this.debug) console.log('deleteCaculColAction triggered with', col)
    const index = this.state.caculCols.findIndex(c => c.Column === col.Column)
    this.state.caculCols.splice(index, 1)
  },
  deleteCaculColAction2(col) {
    if (this.debug) console.log('deleteCaculColAction2 triggered with', col)
    const index2 = this.state.caculCols2.findIndex(c => c.Column === col.Column)
    this.state.caculCols2.splice(index2, 1)
  },
  setCaculColsAction(caculCols) {
    if (this.debug) console.log('setCaculColsAction triggered')
    this.state.caculCols = caculCols
  },
  setCaculColsAction2(caculCols) {
    if (this.debug) console.log('setCaculColsAction2 triggered')
    this.state.caculCols2 = caculCols
  },
  // allCols action
  setAllColsAction(allCols) {
    if (this.debug) console.log('setAllColsAction triggered width', allCols)
    this.state.allCols = allCols
  }
}

export default store
