export default {
  namespaced: true,
  state: () => ({
    connectionIdent: 'main',
    pushPublicKey:'BFtmEj4x1sDOFNlAolLxtqXzD3nU3nB71cNgFWU8plFpNxMqDdj3dVg2aQMwii9nxWGQk9_k0MVKvyb2LHPpZnY',
    pushPrivateKey:'978hS41k7skk96L1v4P-UXzFhNSdfrU7IQWkTj-R6ac',
    connections: {
      main:
      {
        csCompaniesG: 'C512F508-92F1-4F5D-933E-AC4F475829E5',
        csAppNameSpacesG: 'E4B58826-69B9-4180-8A58-953B13AB2C77',
        csB2BPortalsG: '6bc1bf6e-b57c-4579-8494-84a382908afa',
        mainAppWindowIdent: 'csB2BMain',
        viewHTMLFromFile: true,
        useLocalServer: false,
        csAPIDataServerURL: 'https://jupiter.certusoft.pl/csAPIServer',
        csAPIDefinitionServerURL: 'https://jupiter.certusoft.pl/csAPIServer',
        csAPIDataServerURLLocal: 'https://jupiter.certusoft.pl/csAPIServer',
        csAPIDefinitionServerURLLocal: 'https://jupiter.certusoft.pl/csAPIServer',
        virtualDirectory: '',
        csLoginProvider: null,
        startupURL: null,
        startupCookies: null,
        theme: 'B2B',
        workAsPlugin: false,
        csAPISocketServerURL: 'https://jupiter.certusoft.pl',
        csAPISocketServerPath: '/csAPIServer/socket.io'
      }
    }
  }),
  getters: {
    connection: (state) => {
      return state.connections[state.connectionIdent] || {}
    },
    mainAppWindowIdent: (state, getters) => {
      return getters.connection.mainAppWindowIdent || 'csERPMain'
    },
    homeAppWindowIdent: (state, getters) => {
      return getters.connection.homeAppWindowIdent || 'csB2BHome'
    },
    viewHTMLFromFile: (state, getters) => {
      return getters.connection.viewHTMLFromFile
    },
    csAppNameSpacesG: (state, getters) => {
      return getters.connection.csAppNameSpacesG || 'E4B58826-69B9-4180-8A58-953B13AB2C77'
    },
    workAsPlugin: (state, getters) => {
      return getters.connection.workAsPlugin
    },
    getVirtualDirectory: (state, getters) => {
      return getters.connection.virtualDirectory || ''
    },
    getSkin: (state, getters) => {
      return getters.connection.skin || 'B2B'
    },
    getLoginProvider: (state, getters) => {
      return getters.connection.csLoginProvider
    },
    getStartupURL: (state, getters) => {
      return getters.connection.startupURL
    },
    getStartupCookies: (state, getters) => {
      return getters.connection.startupCookies
    },
    getAPIDataServerURL: (state, getters) => (serverSide) => {
      return getters.connection.useLocalServer || serverSide ? getters.connection.csAPIDataServerURLLocal : getters.connection.csAPIDataServerURL
    },
    getAPISocketServerURL: (state, getters) => () => {
      return getters.connection.csAPISocketServerURL
    },
    getAPISocketServerPath: (state, getters) => () => {
      return getters.connection.csAPISocketServerPath
    },
    getAPIImageServerURL: (state, getters) => (ImageIdent, FileName, serverSide) => {
      return getters.getAPIDataServerURL(serverSide) + '/content/ImageIdent/' + ImageIdent + '/' + FileName
    },
    getAPIDataMethodURL: (state, getters) => (method, serverSide) => {
      return getters.getAPIDataServerURL( serverSide ) + '/' + method
    },
    getAPIDefinitionMethodURL: (state, getters) => (method, serverSide) => {
      return (getters.connection.useLocalServer || serverSide ? getters.connection.csAPIDefinitionServerURLLocal : getters.connection.csAPIDefinitionServerURL) + '/' + method
    },
    getPushPublicKey: (state) => {
      return state.pushPublicKey || null
    },
    getPushPrivateKey: (state) => {
      return state.pushPrivateKey || null
    },
    getSupportedLanguages: (state, getters) => () => {
      return getters.connection.languages || ['PL', 'EN']
    },
    getDefLang: (state, getters) => () => {
      return getters.connection.defLang || 'PL'
    }
  },
  actions: {
    addConnections: ({ commit }, payload) => {
      commit('ADD_CONNECTIONS', payload)
    },
    addStartupUrl: ({commit}, payload) =>{
      commit('ADD_STARTUP_URL', payload)
    },
    addStartupCookies: ({commit}, payload) =>{
      commit('ADD_STARTUP_COOKIES', payload)
    }
  },
  mutations: {
    ADD_CONNECTIONS (state, payload) {
      state.connectionIdent = payload.connectionIdent
      state.connections = payload.connections
      state.pushPublicKey = payload.pushPublicKey
      state.pushPrivateKey = payload.pushPrivateKey
    },
    ADD_STARTUP_URL (state, payload) {
      if (state.connections && state.connections[state.connectionIdent])
        state.connections[state.connectionIdent].startupURL = payload.startupURL
    },
    ADD_STARTUP_COOKIES (state, payload) {
      if (state.connections && state.connections[state.connectionIdent])
        state.connections[state.connectionIdent].startupCookies = payload.cookies
    }
  }
}
