import constructors from './constructors'

const dataSetUtils = {
  rowState : {
    added : 1,
    deleted : 2,
    detached : 3,
    modified : 4,
    unchanged : 5
  },
  fileState:{
    error: -1,
    added: 0,
    uploading: 1,
    uploaded: 2
  },
  scrollDirection: {
    top:1,
    right:2,
    bottom:3,
    left:4
  },
  state:{
    error: -1,
    progress: 0,
    loaded: 1,
    added: 2
  },
  oprState:{
    inProgress: 0,
    finished: 1
  },
  addDataSet(ds) {
    let dsi = new constructors.csDataSet(ds)
    let asi = dsi.actions
    if(asi instanceof Array) {
      for (let i = 0; i < asi.length; i++) {
        asi[i] = constructors.csAction(asi[i])
      }
    }
    let layoutIdents = dsi.layoutIdents
    if (layoutIdents instanceof Array)
      layoutIdents.forEach((l, i, layouts) => {
        layouts[i] = constructors.csLayout(l)
      })
    let dsf = dsi.dataSetsFrom
    if(dsf instanceof Array) {
      for (let i = 0; i < dsf.length; i++) {
        dsf[i] = constructors.csDataSetLinkFrom(dsf[i])
      }
    }
    let dst = dsi.dataSetsTo
    if(dst instanceof Array) {
      for (let i = 0; i < dst.length; i++) {
        dst[i] = constructors.csDataSetLinkTo(dst[i])
      }
    }
    let ld = dsi.lookupDefs
    if(ld instanceof Array) {
      for (let i = 0; i < ld.length; i++) {
        ld[i] = constructors.csLookupDefs(ld[i])
      }
    }
    if(ds.whereFieldsIdents instanceof Array) {
      ds.whereFieldsIdents.forEach( (item) => {
        if(dsi.where[item.dataFieldIdent] === undefined) dsi.where[item.dataFieldIdent] = item.dataFieldValueDef
      })
    }
    return (dsi)
  },
  clearDataSet(payload){
    if (payload.dataSet) {
      while (payload.dataSet.rows.length > 0) payload.dataSet.rows.pop()
      while (payload.dataSet.pivotRows.length > 0)
        payload.dataSet.pivotRows.pop()
      while (payload.dataSet.pivotCols.length > 0)
        payload.dataSet.pivotCols.pop()
      for (let key in payload.dataSet.pivotValues) {
        delete payload.dataSet.pivotValues[key]
      }
      for (let key in payload.dataSet.infoFields) {
        delete payload.dataSet.infoFields[key]
      }
      for (let key in payload.dataSet.warningFields) {
        delete payload.dataSet.warningFields[key]
      }
      for (let key in payload.dataSet.errorFields) {
        delete payload.dataSet.errorFields[key]
      }
      if (payload.dataSet.refreshAggregates) {
        while (payload.dataSet.aggregates.length > 0)
          payload.dataSet.aggregates.pop()
        payload.dataSet.aggregates.push({ count: 0 })
        while (payload.dataSet.pivotRowsAggregates.length > 0)
          payload.dataSet.pivotRowsAggregates.pop()
        payload.dataSet.pivotRowsAggregates.push({ count: 0 })
        while (payload.dataSet.pivotColsAggregates.length > 0)
          payload.dataSet.pivotColsAggregates.pop()
        payload.dataSet.pivotColsAggregates.push({ count: 0 })
        payload.dataSet.pageNo = 1
        if (payload.layout) {
          payload.layout.pageNoX = 1
          payload.layout.pageNoY = 1
        }
      }
      payload.dataSet.pivotPrevRowIndex = payload.dataSet.pivotCurrentRowIndex
      payload.dataSet.pivotPrevColIndex = payload.dataSet.pivotCurrentColIndex
      payload.dataSet.pivotPrevDataFieldIdent =
        payload.dataSet.pivotCurrentDataFieldIdent
      payload.dataSet.currentRowIndex = -1
      payload.dataSet.pivotCurrentRowIndex = -1
      payload.dataSet.pivotCurrentColIndex = -1
      if (payload.layout && payload.layout.aggrFields)
        payload.dataSet.pivotCurrentDataFieldIdent =
          payload.layout.aggrFields[0].dataFieldIdent
    }
  },
  addSelectedRow(dataSet, row) {
    if (row) {
      dataSet.selectedRows.push(row)
      row._isSelected = 1
    }
  },
  deleteSelectedRow(dataSet, row) {
    if ((!dataSet.keyFieldsIdents) || (!row))
      return
    let index = dataSet.selectedRows.findIndex(selectedRow => {
      let result = true
      dataSet.keyFieldsIdents.forEach(keyField => {
        result = result && row[keyField.dataFieldIdent] === selectedRow[keyField.dataFieldIdent]
      })
      return result
    })
    if (index >= 0) {
      dataSet.selectedRows.splice(index, 1)
      row._isSelected = 0
    }
  },
  deleteAllSelectedRows(dataSet) {
    if(dataSet != null && dataSet.selectedRows != null) {
      while (dataSet.selectedRows.length > 0) {
        dataSet.selectedRows.pop()
      }
    }
  },
  getDataSetFieldsIdentsDataField(dataSet, dataFieldIdent) {
    return dataSet != null && dataSet.fieldsIdents != null ? dataSet.fieldsIdents.find(item => item.dataFieldIdent === dataFieldIdent) : null
  },
  checkDataType(value, format){
    switch(format){
    case 'integer':
      return Number(value)
    default:
      return value
    }
  }
}
export default dataSetUtils
