import constructors from '../../../classes/constructors'
import constans from '../../../classes/constans'
import dataSetUtils from '../../../classes/dataSetUtils'

export default {
  csAppWindows: state => state.csAppWindows,
  activeWindowIdent: state => state.activeWindowIdent,

  activeMetaInfo: state => state.activeMetaInfo?.metaInfo,
  activeMetaInfoPath: state => state.activeMetaInfo?.path,

  pathMetaInfo: state => (path) => {
    let metaInfo = state.paths[path]?.metaInfo
    return metaInfo || null
  },

  urlLock: state => state.urlLock,
  devAppName: state => state.devAppName,
  isDev: state => state.dev,
  getRoute: (state, getters, rootState) => {
    return rootState.route
  },
  deviceType: (state, getters, rootState, rootGetters)  => {
    return rootGetters['csDeviceDetection/deviceType']
  },
  getFullPathHref: (state, getters, rootState) => {
    if (rootState.route == null)
      return
    let params = '?headerVisible=0'
    let fullPath = rootState.route.path
    let keys = Object.keys(rootState.route.query)
    if (keys)
      keys.forEach(item => {
        if (item !== 'headerVisible')
          params += '&' + item + '=' + rootState.route.query[item]
      })
    fullPath += params
    return fullPath
  },
  getAudio: (state) => (url) => {
    return state.audio[url]
  },
  mainAppWindowIdent: (state, getters, rootState, rootGetters) => {
    return rootGetters['csServerSettings/mainAppWindowIdent']
  },
  viewHTMLFromFile: (state, getters, rootState, rootGetters) => {
    return rootGetters['csServerSettings/viewHTMLFromFile']
  },
  workAsPlugin: (state, getters, rootState, rootGetters) => {
    return rootGetters['csServerSettings/workAsPlugin']
  },
  csAppNameSpacesG: (state, getters, rootState, rootGetters) => {
    return rootGetters['csServerSettings/csAppNameSpacesG']
  },
  getStartupURL: (state, getters, rootState, rootGetters) => {
    return rootGetters['csServerSettings/getStartupURL']
  },
  getStartupCookies: (state, getters, rootState, rootGetters) => {
    return rootGetters['csServerSettings/getStartupCookies']
  },
  getAPIConnectionData: (state, getters, rootState, rootGetters) => {
    let connection = rootGetters['csServerSettings/connection']
    return {
      csCompaniesG:
        connection && connection.csCompaniesG
          ? connection.csCompaniesG
          : null,
      csB2BPortalsG:
        connection && connection.csB2BPortalsG
          ? connection.csB2BPortalsG
          : null,
      csNGMasterMenuDefG:
        connection && connection.csNGMasterMenuDefG
          ? connection.csNGMasterMenuDefG
          : null,
      languageSuffix: rootState.route && rootState.route.params['languageSuffix']
        ? rootState.route.params['languageSuffix'].toUpperCase()
        : getters.getLanguageSuffix ||
        rootGetters['csServerSettings/getDefLang']
    }
  },
  getAppWindowIdentFromPath: state => path => {
    let appWindow = state.paths[path]
    return appWindow !== null && appWindow !== undefined
      ? appWindow.appWindowIdent
      : null
  },
  getAppWindowIdent: state => appWindowUniqIdent => {
    let appWindow = state.csAppWindows[appWindowUniqIdent]
    return appWindow ? appWindow.appWindowIdent : null
  },
  getParamsFromPath: state => path => {
    let appWindow = state.paths[path]
    return appWindow ? appWindow.params : null
  },
  getCommandsFromPath: state => path => {
    let appWindow = state.paths[path]
    return appWindow ? appWindow.commands : null
  },
  getHeaderVisible: (state, getters, rootState) => () => {
    return rootState.route.query.headerVisible === '0' ? false : true
  },
  getSessionId: (state) => () => {
    return state.sessionId
  },
  getLanguageSuffix: state => () => {
    return state.languageSuffix
  },
  getRefreshParams: state => () => {
    return state.refreshParams
  },
  getPortals: state => () => {
    return state.portals
  },
  getAppWindow: (state, getters) => (appWindowIdent, path) => {
    return state.csAppWindows[
      getters.getAppWindowIdentFromPath(path) || appWindowIdent
    ]
  },
  getAppWindowViewHTML: (state, getters) => (appWindowIdent, path) => {
    let appWindow = getters.getAppWindow(appWindowIdent, path)
    return appWindow ? appWindow.viewHTML : null
  },
  getAppWindowStyleCSS: (state, getters) => (appWindowIdent, path) => {
    let appWindow = getters.getAppWindow(appWindowIdent, path)
    if (!appWindow || !appWindow.styleCSS)
      return null
    return '<style>' +  appWindow.styleCSS + '</style>'
  },
  getActionViewHTML: (state, getters) => (appWindowIdent, dataSetIdent, actionIdent) => {
    let action = getters.getDataSetAction(getters.getDataSet(appWindowIdent, dataSetIdent), actionIdent)
    return action ? action.viewHTML : null
  },
  getActionStyleCSS: (state, getters) => (appWindowIdent, dataSetIdent, actionIdent) => {
    let action = getters.getDataSetAction(getters.getDataSet(appWindowIdent, dataSetIdent), actionIdent)
    return action ? '<style>' + action.styleCSS + '</style>' : null
  },
  checkAppWindowQueryDataSetsTo: (state, getters) => appWindowIdent => {
    let appWindow = getters.getAppWindow(appWindowIdent)
    if (!appWindow)
      return false
    let queryDataSetsTo = appWindow.queryDataSetsTo
    let from = appWindow.query
    let to = getters.getRoute.query
    if (!queryDataSetsTo) return false
    for (let i = 0; i < queryDataSetsTo.length; i++) {
      if (
        from[queryDataSetsTo[i].linkedParamName] !=
        to[queryDataSetsTo[i].linkedParamName]
      ) {
        return true
      }
    }
    return false
  },
  getAppWindowUniqIdentFromAppWindowIdent: (state, getters) => (
    appWindowIdent,
    query
  ) => {
    let appWindow = getters.getAppWindow(appWindowIdent)
    if (appWindow)
      return getters.getAppWindowUniqIdentFromAppWindow(appWindow, query)
    return null
  },
  gAWUI: (state, getters) => (
    appWindowIdent,
    query
  ) => {
    return getters.getAppWindowUniqIdentFromAppWindowIdent(appWindowIdent, query)
  },
  getAppWindowUniqIdentFromAppWindow: () => (appWindow, query) => {
    let queryDataSetsTo = appWindow.queryDataSetsTo
    let result = appWindow.appWindowIdent
    if (!queryDataSetsTo || !query) return result
    queryDataSetsTo.forEach(async item => {
      if (item.isIdent === 1) {
        result += '_' + item.linkedParamName
        if (query[item.linkedParamName] !== undefined)
          result += '-' + query[item.linkedParamName]
      }
    })
    return result
  },
  getAppWindowQueryNotIsIdent: () => (appWindow, query) => {
    let result = null
    if (!appWindow) return result
    let queryDataSetsTo = appWindow.queryDataSetsTo
    result = appWindow.appWindowIdent
    if (!queryDataSetsTo || !query) return result
    queryDataSetsTo.forEach(async item => {
      if (item.isIdent === 0) {
        result += '_' + item.linkedParamName
        if (query[item.linkedParamName] !== undefined)
          result += '-' + query[item.linkedParamName]
      }
    })
    return result
  },
  getAppWindowFromDefinition: (state, getters) => (
    appWindowIdent,
    definition,
    lookupDef
  ) => {
    let appWindow = constructors.csAppWindow({
      appWindowIdent: appWindowIdent,
      lookupDef: lookupDef
    })
    if (definition.appWindowIdent)
      appWindow.appWindowIdent = definition.appWindowIdent
    if (definition.csAppNameSpacesG)
      appWindow.csAppNameSpacesG = definition.csAppNameSpacesG
    if (!getters.viewHTMLFromFile){
      appWindow.viewHTML = definition.viewHTML
      appWindow.styleCSS = definition.styleCSS
    } 
    if (definition.dataSetsMaxOrd)
      appWindow.dataSetsMaxOrd = definition.dataSetsMaxOrd
    if (definition.appWindowTranslatedDesc)
      appWindow.appWindowTranslatedDesc = definition.appWindowTranslatedDesc
    if (definition.params) appWindow.params = definition.params
    if (definition.commands) appWindow.commands = definition.commands
    if (definition.queryDataSetsTo)
      appWindow.queryDataSetsTo = definition.queryDataSetsTo
    definition.usrSettings = definition.usrSettings || {dataSets: []}
    definition.usrSettings.dataSets = definition.usrSettings.dataSets || []
    let usrSettings = {}
    definition.usrSettings.dataSets.forEach(ds =>{
      usrSettings.dataSets = {
        ...usrSettings.dataSets,
        [ds.dataSetIdent]: getters.getDataSetUsrSettingsFromDefinition(ds)
      }
    })
    appWindow.usrSettings = usrSettings
    definition.dataSets = definition.dataSets || []
    definition.dataSets.forEach(dataSet => {
      if(usrSettings.dataSets && usrSettings.dataSets[dataSet.dataSetIdent].layoutIdent)
        dataSet.layoutIdent = usrSettings.dataSets[dataSet.dataSetIdent].layoutIdent
      appWindow.dataSets = {
        ...appWindow.dataSets,
        [dataSet.dataSetIdent]: dataSetUtils.addDataSet(dataSet)
      }
    })
    return getters.checkAppWindowLayoutsDeviceType(appWindow)
  },
  checkAppWindowLayoutsDeviceType: (state, getters) => appWindow => {
    if (!appWindow || !appWindow.dataSets)
      return appWindow
    let dataSets = appWindow.dataSets
    for(let dsi in dataSets){
      let ds = dataSets[dsi]
      let layouts = getters.getDataSetLayoutIdents(ds, appWindow)
      if (!layouts || layouts.length === 0)
        continue
      let layout = layouts.find(item => item.layoutIdent === ds.layoutIdent)
      if (layout)
        continue
      layout = layouts.find(l => l.default === 1)
      if (layout){
        ds.layoutIdent = layout.layoutIdent
        continue
      }
      layout = layouts[0]
      if (layout)
        ds.layoutIdent = layout.layoutIdent
    }
    return appWindow
  },
  cloneAppWindow: () => (appWindow, appWindowUniqIdent) => {
    let appWindowNew = JSON.parse(JSON.stringify(appWindow))
    appWindowNew.lookupDef = null
    appWindowNew.viewHTML = null
    appWindowNew.styleCSS = null
    appWindowNew.appWindowUniqIdent = appWindowUniqIdent
    for(const dsi in appWindowNew.dataSets){
      appWindowNew.dataSets[dsi].appWindowUniqIdent = appWindowUniqIdent
    }
    return appWindowNew
  },
  getDataSetUsrSettingsFromDefinition: () => definition =>{
    let settings = {layoutIdents: [], layoutIdent: null}
    if (!definition.layoutIdents)
      return settings
    definition.layoutIdents.forEach(li => {
      let ul = constructors.csLayout(li.layout)
      ul.usrDef = 1
      settings.layoutIdents.push(ul)
    })
    settings.layoutIdent = definition.layoutIdent
    return settings
  },
  getDataSetUsrSettings: () => (appWindow, dataSetIdent) => {
    return appWindow.usrSettings.dataSets ? appWindow.usrSettings.dataSets[dataSetIdent] : null
  },
  getDataSetUsrLayouts: (state, getters) => (appWindow, dataSetIdent) => {
    let settings = getters.getDataSetUsrSettings(appWindow, dataSetIdent)
    return settings ? settings.layoutIdents : []
  },
  getDataSetUsrLayout: (state, getters) => (appWindow, dataSetIdent, layoutIdent) =>{
    return getters.getDataSetUsrLayouts(appWindow, dataSetIdent).find(l => l.layoutIdent === layoutIdent)
  },
  getAppWindowModalWindows: (state, getters) => (appWindowIdent) => {
    let appWindow = getters.getAppWindow(appWindowIdent)
    return appWindow && appWindow.modalWindows ? appWindow.modalWindows : null
  },
  getAppWindowLookupDef: (state, getters) => appWindowIdent => {
    let appWindow = getters.getAppWindow(appWindowIdent)
    return appWindow !== null &&
      appWindow !== undefined &&
      appWindow.lookupDef !== undefined
      ? appWindow.lookupDef
      : null
  },
  getAppWindowParams: (state, getters) => appWindowIdent => {
    let appWindow = getters.getAppWindow(appWindowIdent)
    return appWindow !== null &&
      appWindow !== undefined &&
      appWindow.params !== undefined
      ? appWindow.params
      : null
  },
  getAppWindowRoutingData: (state, getters, rootState) => addRoutingData => {
    return addRoutingData && rootState.route
      ? { query: rootState.route.query, params: rootState.route.params, startupURL: getters.getStartupURL, cookies:getters.getStartupCookies }
      : null
  },
  getAppWindowLookupGetters: (state, getters) => appWindowIdent => {
    let lookupDef = getters.getAppWindowLookupDef(appWindowIdent)
    return lookupDef !== null &&
      lookupDef.lookupGetters !== null &&
      lookupDef.lookupGetters !== undefined
      ? lookupDef.lookupGetters
      : []
  },
  getAppWindowLookupSetters: (state, getters) => appWindowIdent => {
    let lookupDef = getters.getAppWindowLookupDef(appWindowIdent)
    return lookupDef !== null &&
      lookupDef.lookupSetters !== null &&
      lookupDef.lookupSetters !== undefined
      ? lookupDef.lookupSetters
      : []
  },
  getAppWindowLookupSettersRefresh: (state, getters) => appWindowIdent => {
    let lookupDef = getters.getAppWindowLookupDef(appWindowIdent)
    return lookupDef !== null &&
      lookupDef.lookupSettersRefresh !== null &&
      lookupDef.lookupSettersRefresh !== undefined
      ? lookupDef.lookupSettersRefresh
      : []
  },
  getAppWindowDataSetsMaxOrd: (state, getters) => appWindowIdent => {
    let appWindow = getters.getAppWindow(appWindowIdent)
    let dataSetsMaxOrd = 6
    if (appWindow && appWindow.dataSetsMaxOrd)
      dataSetsMaxOrd = appWindow.dataSetsMaxOrd
    return dataSetsMaxOrd
  },
  getDataSetsToRefresh: (state, getters) => (appWindowIdent, ord) => {
    let appWindow = getters.getAppWindow(appWindowIdent)
    let result = []
    if (appWindow && appWindow.dataSets) {
      for (let dataSetIdent in appWindow.dataSets) {
        let dataSet = appWindow.dataSets[dataSetIdent]
        if (
          dataSet.isSet === 0 &&
          dataSet.refReq === 1 &&
          dataSet.ord === ord
        )
          result.push(dataSet)
      }
    }
    return result
  },
  getDataSetsDependency: () => (dataSet, sourceKindFrom) => {
    let result = []
    if (dataSet === null || dataSet.dataSetsTo === null) {
      return result
    }
    dataSet.dataSetsTo.forEach(function (item) {
      if (sourceKindFrom !== 'selectedRows' && item.sourceKindFrom !== 'selectedRows' || sourceKindFrom === 'selectedRows' && item.sourceKindFrom === 'selectedRows')
        result.push({
          appWindowIdent: item['appWindowIdentTo'],
          dataSetIdent: item['dataSetIdentTo']
        })
    })
    return result
  },
  getDataSet: (state, getters) => (appWindowIdent, dataSetIdent) => {
    let appWindow = getters.getAppWindow(appWindowIdent)
    let dataSet = null
    if (appWindow) dataSet = appWindow.dataSets[dataSetIdent]
    return dataSet
  },
  getDataSetWhereList: () => (dataSet, dataFieldIdent, sourceKind = 'where', row) => {
    switch (sourceKind){
    case 'where':
      return dataSet ? dataSet.whereLists[dataFieldIdent] : null
    case 'rows':
      return row ? row._rowLists[dataFieldIdent] : null
    default: return null
    }
  },
  getDataSetLookupDef: (state, getters) => (
    appWindowIdent,
    dataSetIdent,
    dataFieldIdent
  ) => {
    let dataSet = getters.getDataSet(appWindowIdent, dataSetIdent)
    if (!dataSet) return null
    let lookupDef
    if (dataSet.lookupDefs) {
      lookupDef = dataSet.lookupDefs.find(
        item => item.dataFieldIdent === dataFieldIdent
      )
    }
    return lookupDef ? lookupDef : null
  },
  dataSetIsEOF: () => dataSet => {
    if (dataSet == null || dataSet.rows == null || dataSet.rows.length == 0) {
      return true
    }
    return dataSet.currentRowIndex >= dataSet.rows.length - 1
  },
  getDataSetRowsIsEmpty: () => dataSet => {
    return !dataSet || !dataSet.rows || dataSet.rows.length === 0 ? true : false
  },
  getDataSetPivotIsEmpty: () => dataSet => {
    return dataSet == null ||
      dataSet.pivotValues == null ||
      (dataSet.pivotCols.length === 0 && dataSet.pivotRows.length === 0)
      ? true
      : false
  },
  getDataSetActions: () => dataSet => {
    return dataSet && dataSet.actions ? dataSet.actions : []
  },
  getDataSetAction: (state, getters) => (dataSet, actionIdent) => {
    let actions = getters.getDataSetActions(dataSet)
    let action = actions.find(item => item.actionIdent === actionIdent)
    return action ? action : null
  },
  getDataSetActionState: (state, getters) => (dataSet, actionIdent, row) => {
    if (row && row._actionState[actionIdent] !== undefined) return row._actionState[actionIdent].state
    let action = getters.getDataSetAction(dataSet, actionIdent)
    return action ? action.state : 1
  },
  getDataSetActionSourcesValues: (state, getters) => (action) => {
    let sourcesValues = []
    if (action && action.sources) {
      action.sources.forEach(source => {
        let dataSetFrom = getters.getDataSet(source.appWindowIdentFrom, source.dataSetIdentFrom)
        if (dataSetFrom) {
          sourcesValues.push({
            appWindowIdentFrom: source.appWindowIdentFrom,
            dataSetIdentFrom: source.dataSetIdentFrom,
            whereFrom: source.addWhere == 1 ? getters.getDataSetWhere(dataSetFrom) : undefined,
            whereListsFrom: source.addWhere == 1 ? dataSetFrom.whereLists : undefined,
            rowFrom: source.addCurrentRow == 1 ? getters.getDataSetCurrentRow(dataSetFrom) : undefined
          })
        }
      })
    }
    return sourcesValues
  },
  getDataSetActionRow: () => (action, row) => {
    if (!action) return row
    let addCurrentRow = action.addCurrentRow
    let fieldsIdents = action.fieldsIdents
    if (addCurrentRow == 0) return null
    if (!fieldsIdents) return row
    if (fieldsIdents.length == 0) return row
    let rowValues = {}
    fieldsIdents.forEach(fieldIdent => {
      let dataFieldIdent = fieldIdent.dataFieldIdent
      let value = row ? row[dataFieldIdent] : null
      if (value) {
        rowValues = { ...rowValues, [dataFieldIdent]: value }
      }
    })
    return rowValues
  },
  getDataSetSelectedRowsFieldValues: () => (fieldsIdents, dataSet) => {
    let selectedRows = dataSet ? dataSet.selectedRows : null
    if (!fieldsIdents || !selectedRows || selectedRows.length == 0) return selectedRows
    if (fieldsIdents.length == 0) return selectedRows
    let fis
    if (dataSet.keyFieldsIdents != null)
      fis = [...new Set([...fieldsIdents, ...dataSet.keyFieldsIdents])]
    else
      fis = fieldsIdents
    let selectedRowsValues = []
    selectedRows.forEach(row => {
      let rowValues = {}
      fis.forEach(fieldIdent => {
        let dataFieldIdent = fieldIdent.dataFieldIdent
        let value = row[dataFieldIdent]
        if (value) {
          rowValues = { ...rowValues, [dataFieldIdent]: value }
        }
      })
      selectedRowsValues.push(rowValues)
    })
    return selectedRowsValues
  },
  getDataSetActionSelectedRows: (state, getters) => (action, dataSet) => {
    let selectedRows = dataSet ? dataSet.selectedRows : null
    if (!action || !dataSet || !selectedRows || selectedRows.length == 0) return null
    return getters.getDataSetSelectedRowsFieldValues(action.fieldsIdents, dataSet)
  },
  getDataSetActionWhere: () => (action, where) => {
    if (!action) return where
    return action.addWhere == 0 ? null : where
  },
  getDataSetRefConditions: () => dataSet => {
    return dataSet ? dataSet.refConditions : []
  },
  removeNullObjElems: () => (obj) => {
    if(Array.isArray(obj)) {
      let result = []
      for (let arrayItem of obj) {
        let arrayItemResult = {}
        for (let key in arrayItem) {
          if (arrayItem[key] != null) arrayItemResult = { ...arrayItemResult, [key]: arrayItem[key] }
        }
        result.push(arrayItemResult)
      }
      return result
    } else {
      let result = {}
      for (let key in obj) {
        if (obj[key] != null) result = { ...result, [key]: obj[key] }
      }
      return result
    }   
  },
  undefinedWhenObjNull: (state, getters) => (obj, removeNulls) => {
    for (let key in obj) {
      if (obj[key] != null) return removeNulls ? getters.removeNullObjElems(obj) : obj
    }
    return undefined
  },
  undefinedWhenArrayEmpty: () => (array) => {
    return array && array.length > 0 ? array : undefined
  },
  getLayout4Request: (state, getters) => (dataSet) => {
    let layout = getters.getDataSetLayout(dataSet, getters.getDataSetLayoutIdent(dataSet))
    if (!layout)
      return undefined
    let sort = getters.getDataSetSort(dataSet, layout.sortIdent)
    let result = {
      layoutIdent: layout.layoutIdent,
      pageSizeX: dataSet.pivotData ? layout.pageSizeX : undefined,
      pageSizeY: dataSet.pivotData ? layout.pageSizeY : undefined,
      pageNoX: dataSet.pivotData ? layout.pageNoX : undefined,
      pageNoY: dataSet.pivotData ? layout.pageNoY : undefined,
      colFields: dataSet.pivotData ? layout.colFields : undefined,
      rowFields: dataSet.pivotData ? layout.rowFields : undefined,
      aggrFields: layout.aggrFields,
      sortIdent: layout.sortIdent,
      sortIdents: sort && sort.isUsrDefined ? sort : undefined
    }
    return getters.undefinedWhenObjNull(result, true)
  },
  getAPIDataRequest: (state, getters) => (appWindowIdent, dataSetsList, serverSide) => {
    let request = []
    let sessionId = getters.getSessionId(state)
    let params = getters.getAppWindowParams(appWindowIdent)
    let appWindow = getters.getAppWindow(appWindowIdent)
    let refreshParamsJSON = getters.getRefreshParams()
    dataSetsList.forEach(dataSet => {
      request.push({
        serverSide: serverSide,
        appWindowIdent: appWindow.appWindowIdent,
        csAppNameSpacesG: appWindow.csAppNameSpacesG,
        dataSetIdent: dataSet.dataSetIdent,
        sessionId: sessionId,
        languageSuffix: getters.getLanguageSuffix(state),
        connection: refreshParamsJSON ? getters.getAPIConnectionData : undefined,
        route: getters.undefinedWhenObjNull(getters.getAppWindowRoutingData(dataSet.addRoutingData), true),
        appWindowUniqIdent: appWindow.appWindowIdent != appWindowIdent ? appWindowIdent : undefined,
        params: getters.undefinedWhenArrayEmpty(params),
        pageSize: dataSet.pageSize === constans.dataSet.pageSize ? undefined : dataSet.pageSize,
        pageNo: dataSet.pageNoRef === constans.dataSet.pageNo ? undefined : dataSet.pageNoRef,
        where: getters.undefinedWhenObjNull(dataSet.where, true),
        whereLists: getters.undefinedWhenObjNull(dataSet.whereLists, true),
        selectedRows: getters.undefinedWhenArrayEmpty(dataSet.selectedRows),
        order: getters.undefinedWhenObjNull(dataSet.order, true),
        sortIdent: dataSet.sortIdent,
        layout: getters.getLayout4Request(dataSet),
        refreshAggregates: dataSet.refreshAggregates,
        refCount: dataSet.refCount,
        refreshParamsJSON: refreshParamsJSON ? refreshParamsJSON : undefined
      })
    })
    return request
  },
  getAPIDataRequestConfig:(state, getters) => () =>{
    return {
      params:{
        devAppName: getters.devAppName
      }
    }
  },
  getAPIExportDataRequest: (state, getters) => (appWindowIdent, dataSetIdent, actionKind) => {
    let request = {}
    let sessionId = getters.getSessionId(state)
    let params = getters.getAppWindowParams(appWindowIdent)
    let dataSet = getters.getDataSet(appWindowIdent, dataSetIdent)
    request = {
      sessionId: sessionId,
      languageSuffix: getters.getLanguageSuffix(state),
      connection: getters.getAPIConnectionData,
      route: getters.getAppWindowRoutingData(dataSet.addRoutingData),
      csAppNameSpacesG: getters.csAppNameSpacesG,
      appWindowIdent: appWindowIdent,
      dataSetIdent: dataSet.dataSetIdent,
      params: params,
      pageSize: dataSet.pageSize,
      pageNo: dataSet.pageNo,
      where: dataSet.where,
      whereLists: dataSet.whereLists,
      selectedRows: dataSet.selectedRows,
      order: dataSet.order,
      sortIdent: dataSet.sortIdent,
      layout: getters.getDataSetLayout(dataSet, getters.getDataSetLayoutIdent(dataSet)),
      refreshAggregates: dataSet.refreshAggregates,
      refCount: dataSet.refCount,
      actionKind: actionKind
    }
    return (request)
  },
  getDataSetPageSize: () => dataSet => {
    return dataSet ? dataSet.pageSize : 20
  },
  getDataSetPageNo: () => dataSet => {
    return dataSet ? dataSet.pageNo : null
  },
  getDataSetInfoFields: () => (dataSet, row) => {
    let infoFields = row ? row._infoFields : null
    if (!infoFields) infoFields = dataSet ? dataSet.infoFields : null
    return infoFields
  },
  getDataSetWarningFields: () => (dataSet, row) => {
    let warningFields = row ? row._warningFields : null
    if (!warningFields) warningFields = dataSet ? dataSet.warningFields : null
    return warningFields
  },
  getDataSetErrorFields: () => (dataSet, row) => {
    let errorFields = row ? row._errorFields : null
    if (!errorFields) errorFields = dataSet ? dataSet.errorFields : null
    return errorFields
  },
  getFieldsType: () => messageType => {
    switch (messageType) {
    case 1:
      return 'errorFields'
    case 2:
      return 'warningFields'
    case 3:
      return 'infoFields'
    }
    return null
  },
  getRowMessageFields: () => (row, fieldsType) =>{
    if (!(row instanceof constructors.csRow) || !fieldsType)
      return null
    fieldsType = '_' + fieldsType
    return row[fieldsType]
  },
  getRowMessageField: (state, getters) => (row, fieldsType, messageField) =>{
    let rowMessageFields = getters.getRowMessageFields(row, fieldsType)
    return rowMessageFields ? rowMessageFields[messageField] : null
  },
  getDataSetMessageFields: () => (dataSet, fieldsType) =>{
    if (!(dataSet instanceof constructors.csDataSet) || !fieldsType)
      return null
    return dataSet[fieldsType]
  },
  getDataSetMessageField: (state, getters) => (dataSet, fieldsType, messageField) =>{
    let rowMessageFields = getters.getDataSetMessageFields(dataSet, fieldsType)
    return rowMessageFields ? rowMessageFields[messageField] : null
  },
  getDataSetPivotColsPageNo: (state, getters) => dataSet => {
    let layout = getters.getDataSetLayout(
      dataSet,
      getters.getDataSetLayoutIdent(dataSet)
    )
    return dataSet && layout ? layout.pageNoX : 1
  },
  getDataSetPivotRowsPageNo: (state, getters) => dataSet => {
    let layout = getters.getDataSetLayout(
      dataSet,
      getters.getDataSetLayoutIdent(dataSet)
    )
    return dataSet && layout ? layout.pageNoY : 1
  },
  getDataSetRowsCount: () => dataSet => {
    return dataSet != null &&
      dataSet.aggregates != null &&
      dataSet.aggregates.length > 0
      ? dataSet.aggregates[0].count || 0
      : 0
  },
  getDataSetPivotRowsCount: () => dataSet => {
    return dataSet != null &&
      dataSet.pivotRowsAggregates != null &&
      dataSet.pivotRowsAggregates.length > 0
      ? dataSet.pivotRowsAggregates[0].count
      : 0
  },
  getDataSetPivotColsCount: () => dataSet => {
    return dataSet != null &&
      dataSet.pivotColsAggregates != null &&
      dataSet.pivotColsAggregates.length > 0
      ? dataSet.pivotColsAggregates[0].count
      : 0
  },
  getDataSetPagesCount: () => dataSet => {
    return dataSet != null &&
      dataSet.aggregates != null &&
      dataSet.aggregates.length > 0 &&
      dataSet.pageSize != null
      ? Math.ceil(dataSet.aggregates[0].count / dataSet.pageSize)
      : 0
  },
  getDataSetPivotColsPagesCount: (state, getters) => dataSet => {
    let pageSizeX = getters.getDataSetPivotColsPageSize(dataSet)
    return dataSet != null &&
      dataSet.pivotColsAggregates != null &&
      dataSet.pivotColsAggregates.length > 0 &&
      pageSizeX != null
      ? Math.ceil(dataSet.pivotColsAggregates[0].count / pageSizeX)
      : 1
  },
  getDataSetPivotRowsPagesCount: (state, getters) => dataSet => {
    let pageSizeY = getters.getDataSetPivotRowsPageSize(dataSet)
    return dataSet != null &&
      dataSet.pivotRowsAggregates != null &&
      dataSet.pivotRowsAggregates.length > 0 &&
      pageSizeY != null
      ? Math.ceil(dataSet.pivotRowsAggregates[0].count / pageSizeY)
      : 1
  },
  getDataSetBusy: (state, getters) => (appWindowIdent, dataSetIdent) => {
    let dataSet = getters.getDataSet(appWindowIdent, dataSetIdent)
    if (dataSet && dataSet.state === -1) return true
    else return false
  },
  getDataSetState: (state, getters) => (appWindowIdent, dataSetIdent) => {
    let dataSet = getters.getDataSet(appWindowIdent, dataSetIdent)
    if (dataSet) return dataSet.state
  },
  getDataSetRows: () => dataSet => {
    return dataSet ? dataSet.rows : []
  },
  getDataSetFiles: () => dataSet => {
    return dataSet ? dataSet.files : []
  },
  getDataSetFilesCount: () => dataSet => {
    return dataSet && dataSet.files ? dataSet.files.length : 0
  },
  getDataSetRowFields: (state, getters) => dataSet => {
    let layout = getters.getDataSetLayout(
      dataSet,
      getters.getDataSetLayoutIdent(dataSet)
    )
    return dataSet && layout ? layout.rowFields : []
  },
  getDataSetRowFieldsCount: (state, getters) => dataSet => {
    return getters.getDataSetRowFields(dataSet).length
  },
  getDataSetSelectedRowsCount: () => dataSet => {
    return dataSet && dataSet.selectedRows
      ? dataSet.selectedRows.length
      : 0
  },
  getDataSetRowFieldsSumWidth: (state, getters) => dataSet => {
    let width = null
    getters.getDataSetRowFields(dataSet).forEach(item => {
      width +=
        item.width !== undefined && item.width !== null ? item.width : 100
    })
    return width !== null ? width : 100
  },
  getDataSetRowFieldsLastHeight: (state, getters) => dataSet => {
    let i = getters.getDataSetRowFields(dataSet)
    return i !== null && i.length > 0 ? i[i.length - 1].height : 30
  },
  getDataSetColFieldsLastWidth: (state, getters) => dataSet => {
    let i = getters.getDataSetColFields(dataSet)
    return i !== null && i.length > 0 ? i[i.length - 1].height : 30 //  rotate
  },
  getDataSetColFields: (state, getters) => dataSet => {
    let layout = getters.getDataSetLayout(
      dataSet,
      getters.getDataSetLayoutIdent(dataSet)
    )
    return dataSet && layout ? layout.colFields : []
  },
  getDataSetVisibleColFields: (state, getters) => dataSet => {
    let layout = getters.getDataSetLayout(
      dataSet,
      getters.getDataSetLayoutIdent(dataSet)
    )
    if (dataSet && layout && layout.colFields)
      return layout.colFields.filter(el => {
        return el.isVisible === 1
      })
    else return []
  },
  getDataSetColFieldsCount: (state, getters) => dataSet => {
    let colFields = getters.getDataSetColFields(dataSet)
    return colFields !== null ? colFields.length : 0
  },
  getDataSetAggrFields: (state, getters) => dataSet => {
    let layout = getters.getDataSetLayout(
      dataSet,
      getters.getDataSetLayoutIdent(dataSet)
    )
    return dataSet && layout ? layout.aggrFields : []
  },
  getDataSetAggrFieldsMaxWidth: (state, getters) => dataSet => {
    let width = 0
    getters.getDataSetAggrFields(dataSet).forEach(item => {
      width = item.width > width ? item.width : width
    })
    return width !== 0 ? width : 100
  },
  getDataSetRowColFieldsMaxHeight: (state, getters) => dataSet => {
    let height = 0
    let rowFields = getters.getDataSetRowFields(dataSet)
    if (rowFields)
      rowFields.forEach(item => {
        height = item.height > height ? item.height : height
      })
    let colFields = getters.getDataSetColFields(dataSet)
    if (colFields)
      colFields.forEach(item => {
        height = item.height > height ? item.height : height
      })
    return height !== 0 ? height : 100
  },
  getDataSetColFieldsMaxHeight: (state, getters) => dataSet => {
    let height = 0
    let colFields = getters.getDataSetColFields(dataSet)
    if (colFields)
      colFields.forEach(item => {
        height = item.height > height ? item.height : height
      })
    return height !== 0 ? height : 100
  },
  getPivotRowKey: () => (pivotCol, pivotRow) => {
    return (
      (pivotCol !== null && pivotCol !== undefined ? 'c' + pivotCol : '') +
      (pivotRow !== null && pivotRow !== undefined ? 'r' + pivotRow : '')
    )
  },
  getPivotEmptyRow: () => (pivotCol, pivotRow) => {
    let v0 = {}
    for (let key in pivotCol) {
      if (key !== 'c') v0['c' + key] = pivotCol[key]['0']
    }
    for (let key in pivotRow) {
      if (key !== 'r') v0['r' + key] = pivotRow[key]['0']
    }
    return {
      '0': v0,
      '1': {},
      '2': { c: pivotCol, r: pivotRow }
    }
  },
  getDataSetPivotValuesFieldValue: (state, getters) => (
    dataSet,
    dataFieldIdent,
    pivotRowKey
  ) => {
    if (!pivotRowKey) {
      let pivotCurrentColIndex = dataSet.pivotCurrentColIndex
      let colFields = getters.getDataSetColFields(dataSet)
      let colIndex = colFields.findIndex(
        item => item.dataFieldIdent === dataFieldIdent
      )
      if (colIndex >= 0 && pivotCurrentColIndex >= 0)
        return dataSet.pivotCols[pivotCurrentColIndex][colIndex][0]

      let pivotCurrentRowIndex = dataSet.pivotCurrentRowIndex
      let rowFields = getters.getDataSetRowFields(dataSet)
      let rowIndex = rowFields.findIndex(
        item => item.dataFieldIdent === dataFieldIdent
      )
      if (rowIndex >= 0 && pivotCurrentRowIndex >= 0)
        return dataSet.pivotRows[pivotCurrentRowIndex][rowIndex][0]

      pivotRowKey = getters.getPivotRowKey(
        dataSet.pivotCurrentColIndex,
        dataSet.pivotCurrentRowIndex
      )
    }
    let row = dataSet.pivotValues[pivotRowKey]
    return row !== null &&
      row !== undefined &&
      row['1'] !== null &&
      row['1'] !== undefined
      ? row['1'][dataFieldIdent]
      : null
  },
  getDataSetPivotColsPageSize: (state, getters) => dataSet => {
    let layout = getters.getDataSetLayout(
      dataSet,
      getters.getDataSetLayoutIdent(dataSet)
    )
    return dataSet && layout ? layout.pageSizeX : 20
  },
  getDataSetPivotRowsPageSize: (state, getters) => dataSet => {
    let layout = getters.getDataSetLayout(
      dataSet,
      getters.getDataSetLayoutIdent(dataSet)
    )
    return dataSet && layout ? layout.pageSizeY : 20
  },
  getDataSetAggregates: () => dataSet => {
    return dataSet ? dataSet.aggregates[0] : null
  },
  getDataSetWhere: () => dataSet => {
    return dataSet ? dataSet.where : {}
  },
  getDataSetWhereFieldsIdentsDataField: () => (dataSet, dataFieldIdent) => {
    return dataSet &&
      dataSet.whereFieldsIdents
      ? dataSet.whereFieldsIdents.find(
        item => item.dataFieldIdent === dataFieldIdent
      )
      : null
  },
  getDataSetWhereFieldsIdents: () => dataSet => {
    return dataSet ?
      dataSet.whereFieldsIdents
      : null
  },
  getDataSetWhereOnCFActionIdentsOnCFActionIdent: () => (
    dataSet,
    dataFieldIdent
  ) => {
    let whereOnCFActionIdent =
      dataSet && dataSet.whereOnCFActionIdents
        ? dataSet.whereOnCFActionIdents.find(
          item => item.dataFieldIdent === dataFieldIdent
        )
        : null
    return whereOnCFActionIdent ? whereOnCFActionIdent.onCFActionIdent : null
  },
  getFieldsOnCFActionIdentsOnCFActionIdent: () => (
    dataSet,
    dataFieldIdent
  ) => {
    let fieldsOnCFActionIdent =
      dataSet && dataSet.fieldsOnCFActionIdents
        ? dataSet.fieldsOnCFActionIdents.find(
          item => item.dataFieldIdent === dataFieldIdent
        )
        : null
    return fieldsOnCFActionIdent
      ? fieldsOnCFActionIdent.onCFActionIdent
      : null
  },
  getDataSetFieldsIdentsDataField: () => (dataSet, dataFieldIdent) => {
    return dataSetUtils.getDataSetFieldsIdentsDataField(dataSet, dataFieldIdent)
  },
  getDataSetCurrentRowIndex: () => dataSet => {
    let currentRowIndex = -1
    if (dataSet == null) {
      return currentRowIndex
    }
    if (dataSet != null) {
      currentRowIndex = dataSet.currentRowIndex
      if (
        currentRowIndex === -1 &&
        dataSet.rows !== null &&
        dataSet.rows.length > 0
      ) {
        currentRowIndex = 0
      }
    }
    return currentRowIndex
  },
  getDataSetCurrentRow: (state, getters) => dataSet => {
    let currentRowIndex = getters.getDataSetCurrentRowIndex(dataSet)
    let rows = getters.getDataSetRows(dataSet)
    return rows && currentRowIndex >= 0 ? rows[currentRowIndex] : null
  },
  getDataSetRowIndex: () => (dataSet, row) => {
    if (!dataSet || !row)
      return -1
    return dataSet.rows.indexOf(row)
  },
  getAppWindowDataSetCurrentRow: (state, getters) => (
    appWindowIdent,
    dataSetIdent
  ) => {
    let dataSet = getters.getDataSet(appWindowIdent, dataSetIdent)
    let currentRowIndex = getters.getDataSetCurrentRowIndex(dataSet)
    let rows = getters.getDataSetRows(dataSet)
    return rows && currentRowIndex >= 0 ? rows[currentRowIndex] : null
  },
  gDCR: (state, getters) => (
    appWindowIdent,
    dataSetIdent
  ) => {
    return getters.getAppWindowDataSetCurrentRow(appWindowIdent, dataSetIdent)
  },
  getAppWindowDataSetLastRow: (state, getters) => (
    appWindowIdent,
    dataSetIdent
  ) => {
    let dataSet = getters.getDataSet(appWindowIdent, dataSetIdent)
    let lastRowIndex = dataSet ? getters.getDataSetRows(dataSet).length - 1 : -1
    let rows = dataSet ? getters.getDataSetRows(dataSet) : null
    return rows && lastRowIndex >= 0 ? rows[lastRowIndex] : null
  },
  gDLR: (state, getters) => (
    appWindowIdent,
    dataSetIdent
  ) => {
    return getters.getAppWindowDataSetLastRow(appWindowIdent, dataSetIdent)
  },
  gDLRFV: (state, getters) => (
    appWindowIdent,
    dataSetIdent,
    dataFieldIdent
  ) => {
    let rows = getters.gDLR(appWindowIdent, dataSetIdent)
    return rows ? rows[dataFieldIdent] : null
  },
  appWindowDataSetFindRow: (state, getters) => (
    appWindowIdent,
    dataSetIdent,
    filter
  ) => {
    let dataSet = getters.getDataSet(appWindowIdent, dataSetIdent)
    let rows = getters.getDataSetRows(dataSet)
    let rowIndex
    if (rows)
      rowIndex = rows.findIndex(
        item => {
          let result = true
          filter.forEach(filterItem => {
            result = result && item[filterItem.dataFieldIdent] === filterItem.dataFieldValue
          })
          return result
        }
      )
    return rowIndex >= 0 ? rows[rowIndex] : null
  },
  getAppWindowDataSetFindRowAndFieldValue: (state, getters) => (
    appWindowIdent,
    dataSetIdent,
    filter,
    dataFieldIdent,
    dataFieldDefValue
  ) => {
    let row = getters.appWindowDataSetFindRow(appWindowIdent, dataSetIdent, filter)
    return row && dataFieldIdent ? row[dataFieldIdent] : dataFieldDefValue
  },
  getDataSetRowsFieldValue: (state, getters) => (
    dataSet,
    dataFieldIdent,
    row
  ) => {
    if (!row) row = getters.getDataSetCurrentRow(dataSet)
    return row && dataFieldIdent ? row[dataFieldIdent] : null
  },
  getDataSetAggregatesFieldValue: (state, getters) => (
    dataSet,
    dataFieldIdent
  ) => {
    let aggregates = getters.getDataSetAggregates(dataSet)
    return aggregates !== null ? aggregates[dataFieldIdent] : null
  },
  getDataSetWhereFieldValue: (state, getters) => (
    dataSet,
    dataFieldIdent
  ) => {
    let where = getters.getDataSetWhere(dataSet)
    return where !== null ? where[dataFieldIdent] : null
  },
  getDataSetFieldValue: (state, getters) => (
    sourceKind,
    dataSet,
    dataFieldIdent,
    row,
    pivotRowKey
  ) => {
    if (!dataSet || !dataFieldIdent) return null
    switch (sourceKind) {
    case 'rows':
      return getters.getDataSetRowsFieldValue(dataSet, dataFieldIdent, row)
    case 'selectedRows':
      return getters.getDataSetSelectedRowsFieldValues([{dataFieldIdent: dataFieldIdent}], dataSet)
    case 'aggregates':
      return getters.getDataSetAggregatesFieldValue(dataSet, dataFieldIdent)
    case 'where':
      return getters.getDataSetWhereFieldValue(dataSet, dataFieldIdent)
    case 'pivotValues':
      return getters.getDataSetPivotValuesFieldValue(
        dataSet,
        dataFieldIdent,
        pivotRowKey
      )
    }
  },
  getDataSetIdentDataFieldValue: (state, getters) => (
    appWindowIdent,
    dataSetIdent,
    sourceKind,
    dataFieldIdent,
    row
  ) => {
    if (appWindowIdent && dataSetIdent && sourceKind && dataFieldIdent) {
      let dataSet = getters.getDataSet(appWindowIdent, dataSetIdent)
      return getters.getDataSetFieldValue(sourceKind, dataSet, dataFieldIdent, row)
    } else return null
  },
  gDFV: (state, getters) => (
    appWindowIdent,
    dataSetIdent,
    sourceKind,
    dataFieldIdent,
    row
  ) => {
    return getters.getDataSetIdentDataFieldValue(appWindowIdent, dataSetIdent, sourceKind, dataFieldIdent, row)
  },
  // getDataSetIdentRowsIsEmpty: (state, getters) => (
  //   appWindowIdent,
  //   dataSetIdent
  // ) => {
  //   if (appWindowIdent && dataSetIdent) {
  //     let dataSet = getters.getDataSet(appWindowIdent, dataSetIdent)
  //     return getters.getDataSetRowsIsEmpty(dataSet)
  //   } else return null
  // },
  gDIsEL /* getDataSetIdentRowsIsEmpty */: (state, getters) => (
    appWindowIdent,
    dataSetIdent
  ) => {
    if (appWindowIdent && dataSetIdent) {
      let dataSet = getters.getDataSet(appWindowIdent, dataSetIdent)
      return dataSet && dataSet.state == 1 ? getters.getDataSetRowsIsEmpty(dataSet) : null
    } else return null
  },
  getDataSetSortIdents: (state, getters) => dataSet => {
    let sortIdents = null
    let layout = getters.getDataSetLayout(
      dataSet,
      getters.getDataSetLayoutIdent(dataSet)
    )
    if (layout !== null) {
      sortIdents = layout.sortIdents
    }
    return sortIdents
  },
  getDataSetSort: (state, getters) => (dataSet, sortIdent) => {
    let layout = getters.getDataSetLayout(
      dataSet,
      getters.getDataSetLayoutIdent(dataSet)
    )
    let sortIdents = layout !== null ? layout.sortIdents : null
    return sortIdents !== null && sortIdent !== null
      ? sortIdents.find(item => item.sortIdent === sortIdent)
      : null
  },
  getDataSetSortIdent: (state, getters) => dataSet => {
    let layout = getters.getDataSetLayout(
      dataSet,
      getters.getDataSetLayoutIdent(dataSet)
    )
    return layout !== null ? layout.sortIdent : null
  },
  getDataSetSortColFields: (state, getters) => dataSet => {
    let sort = getters.getDataSetSort(
      dataSet,
      getters.getDataSetSortIdent(dataSet)
    )
    return sort != null ? sort.sortColFields : []
  },
  getUsrSortIdent: (state, getters) => dataSet => {
    let layout = getters.getDataSetLayout(
      dataSet,
      getters.getDataSetLayoutIdent(dataSet)
    )
    let sortIdents = layout !== null ? layout.sortIdents : null
    if (sortIdents === null) return null
    return sortIdents.find(item => item.isUsrDefined === 1)
  },
  getDataSetSortActiveTranslatedDesc: (state, getters) => dataSet => {
    let sortIdent = getters.getDataSetSortIdent(dataSet)
    let layout = getters.getDataSetLayout(
      dataSet,
      getters.getDataSetLayoutIdent(dataSet)
    )
    if (sortIdent === null || layout.sortIdents === null) {
      return null
    }
    let sort = getters.getDataSetSort(dataSet, sortIdent)
    return sort !== null ? sort.sortTranslatedDesc : null
  },
  getDataSetSortActiveIdent: (state, getters) => dataSet => {
    let sortTdent = getters.getDataSetSortIdent(dataSet)
    let layout = getters.getDataSetLayout(
      dataSet,
      getters.getDataSetLayoutIdent(dataSet)
    )
    if (sortTdent === null || layout.sortIdents === null) {
      return null
    }
    let sort = getters.getDataSetSort(dataSet, sortTdent)
    return sort ? sort.sortIdent : null
  },
  getDataSetFieldsIdents: () => dataSet => {
    return dataSet ? dataSet.fieldsIdents : null
  },
  getDataSetKeyFieldsIdents: () => dataSet => {
    return dataSet ? dataSet.keyFieldsIdents : []
  },
  getDataSetRowsKeyFieldsValues: (state, getters) => (dataSet, row) => {
    let keyFieldsIdents = getters.getDataSetKeyFieldsIdents(dataSet)
    if (!keyFieldsIdents || keyFieldsIdents.length === 0)
      return null
    let result = {}
    keyFieldsIdents.forEach(keyFieldsIdent => {
      let dataFieldIdent = keyFieldsIdent.dataFieldIdent
      result = {
        ...result,
        [dataFieldIdent]: row[dataFieldIdent]
      }
    })
    return result
  },
  getDataSetLayoutIdentsStandard: () => dataSet => {
    if (!dataSet || !(dataSet.layoutIdents instanceof Array))
      return null
    return dataSet.layoutIdents 
  },
  getDataSetLayoutStandard: (state, getters) => (dataSet, layoutIdent) => {
    let layoutIdents = getters.getDataSetLayoutIdentsStandard(dataSet)
    return layoutIdents !== null && layoutIdent !== null
      ? layoutIdents.find(item => item.layoutIdent === layoutIdent)
      : null
  },
  getDataSetLayoutIdents: (state, getters) => (dataSet, appWindow) => {
    if (!dataSet || !(dataSet.layoutIdents instanceof Array))
      return null
    appWindow = appWindow || getters.getAppWindow(dataSet.appWindowIdent)
    let layouts = appWindow && appWindow.usrSettings.dataSets ? appWindow.usrSettings.dataSets[dataSet.dataSetIdent].layoutIdents : null
    if(layouts instanceof Array){
      let layoutIdents = {}
      dataSet.layoutIdents.forEach(l =>{
        layoutIdents[l.layoutIdent] = l
      })
      layouts.forEach(l =>{
        if(!layoutIdents[l.layoutIdent]){
          layoutIdents[l.layoutIdent] = l
          return
        }
        if(layoutIdents[l.layoutIdent].usrDef === 0){
          layoutIdents[l.layoutIdent] = l
          return
        }
      })
      let result = []
      for(let li in layoutIdents){
        if (layoutIdents[li].device === 'all' || layoutIdents[li].device === getters.deviceType)
          result.push(layoutIdents[li])
      }
      return result
    }
    return dataSet.layoutIdents
  },
  getDataSetLayout: (state, getters) => (dataSet, layoutIdent) => {
    let layoutIdents = getters.getDataSetLayoutIdents(dataSet)
    return layoutIdents !== null && layoutIdent !== null
      ? layoutIdents.find(item => item.layoutIdent === layoutIdent)
      : null
  },
  getDataSetLayoutIdent: () => dataSet => {
    return dataSet !== undefined &&
      dataSet &&
      dataSet.layoutIdent
      ? dataSet.layoutIdent
      : null
  },
  getDataSetLayoutActiveTranslatedDesc: (state, getters) => dataSet => {
    let layout = getters.getDataSetLayout(
      dataSet,
      getters.getDataSetLayoutIdent(dataSet)
    )
    return layout !== null ? layout.layoutTranslatedDesc : null
  },
  getDataSetPageSizesIdents: () => dataSet => {
    return dataSet ? dataSet.pageSizesIdents : []
  },
  getDataSetPageSizes: (state, getters) => (dataSet, pageSizesIdent) => {
    let pageSizesIdents = getters.getDataSetPageSizesIdents(dataSet)
    return pageSizesIdents !== null &&
      pageSizesIdents !== undefined &&
      pageSizesIdent !== null
      ? pageSizesIdents.find(item => item.pageSizesIdent === pageSizesIdent)
      : null
  },
  getDataSetPageSizesIdent: () => dataSet => {
    return dataSet ? dataSet.pageSizesIdent : null
  },
  getDataSetPageSizesActiveTranslatedDesc: (state, getters) => dataSet => {
    let pageSizesIdent = getters.getDataSetPageSizesIdent(dataSet)
    if (pageSizesIdent === null || dataSet.pageSizesIdent === null) {
      return null
    }
    let pageSizes = getters.getDataSetPageSizes(dataSet, pageSizesIdent)
    return pageSizes !== null ? pageSizes.pageSizesTranslatedDesc : null
  },
  getErrors: state => () => {
    return state.errors
  },
  hasError: (state, getters) => id =>{
    return getters.getErrors().find(error => error.__uuid === id) ? true : false
  },
  getMessages: state => () => {
    return state.messages
  },
  getLog: state => () => {
    return state.log
  },
  getCurrentColFieldIndex: (state, getters) => dataSet => {
    let currentColFieldIndex = -1
    if (dataSet == null) {
      return currentColFieldIndex
    }
    let layout = getters.getDataSetLayout(
      dataSet,
      getters.getDataSetLayoutIdent(dataSet)
    )
    if (layout) {
      currentColFieldIndex = layout.currentColFieldIndex
    }
    return currentColFieldIndex
  },
  getCurrentColField: (state, getters) => dataSet => {
    let currentColFieldIndex = getters.getCurrentColFieldIndex(dataSet)
    let colFields = getters.getDataSetColFields(dataSet)
    return colFields && colFields.length > 0 && currentColFieldIndex >= 0
      ? colFields[currentColFieldIndex]
      : null
  },

  getCurrentColFieldLockedLeftIndex: (state, getters) => dataSet => {
    if (dataSet == null) return -1
    let layout = getters.getDataSetLayout(
      dataSet,
      getters.getDataSetLayoutIdent(dataSet)
    )
    if (layout) return layout.currentColFieldLockedLeftIndex
    return -1
  },
  getCurrentColFieldLockedLeft: (state, getters) => dataSet => {
    let currentColFieldLockedLeftIndex = getters.getCurrentColFieldLockedLeftIndex(dataSet)
    let colFields = getters.getDataSetColFields(dataSet)
    return colFields && colFields.length > 0 && currentColFieldLockedLeftIndex >= 0
      ? colFields[currentColFieldLockedLeftIndex]
      : null
  },
  getCurrentColFieldLockedRightIndex: (state, getters) => dataSet => {
    if (dataSet == null) return -1
    let layout = getters.getDataSetLayout(
      dataSet,
      getters.getDataSetLayoutIdent(dataSet)
    )
    if (layout) return layout.currentColFieldLockedRightIndex
    return -1
  },
  getCurrentColFieldLockedRight: (state, getters) => dataSet => {
    let currentColFieldLockedRightIndex = getters.getCurrentColFieldLockedRightIndex(dataSet)
    let colFields = getters.getDataSetColFields(dataSet)
    return colFields && colFields.length > 0 && currentColFieldLockedRightIndex >= 0
      ? colFields[currentColFieldLockedRightIndex]
      : null
  },

  getCurrentSortColFieldIndex: (state, getters) => dataSet => {
    let currentSortColFieldIndex = -1
    if (dataSet == null) {
      return currentSortColFieldIndex
    }
    let sort = getters.getDataSetSort(
      dataSet,
      getters.getDataSetSortIdent(dataSet)
    )
    if (sort) {
      currentSortColFieldIndex = sort.currentSortColFieldIndex
    }
    return currentSortColFieldIndex
  },
  getCurrentSortColField: (state, getters) => dataSet => {
    let currentSortColFieldIndex = getters.getCurrentSortColFieldIndex(
      dataSet
    )
    let sortColFields = getters.getDataSetSort(
      dataSet,
      getters.getDataSetSortIdent(dataSet)
    ).sortColFields
    return sortColFields &&
      sortColFields.length > 0 &&
      currentSortColFieldIndex >= 0
      ? sortColFields[currentSortColFieldIndex]
      : null
  },
  isFilesUploaded: () => dataSet => {
    return dataSet.files.find(file => file._status !== dataSetUtils.fileState.uploaded) ? false : true
  }
}
