import { createStore, applyMiddleware, compose } from 'redux'
import thunk from 'redux-thunk'
import promise from 'redux-promise'
import axios from 'axios'
import apiClient from './apiClient'
import apolloClient from './apolloClient'
import { intl as intlClient } from './intlReducer'
import createReducer, { sourceReqCreateReducer } from './createReducer'

const middlewares = [
  promise,
  thunk.withExtraArgument({
    intl: intlClient,
    api: apiClient,
    axios
  }),
  apolloClient().middleware()
]

export default (initialState, options) => {
  const request = {
    host: '',
    protocol: 'http'
  }

  if (options && options.req && options.req.headers) {
    const { headers } = options.req
    request.host = headers.host || request.host
    request.protocol = (headers['x-forwarded-proto'] && 'https') || request.protocol
  }

  return createStore(
    createReducer({
      sourceRequest: sourceReqCreateReducer(request)
    }),
    initialState,
    compose(
      applyMiddleware(...middlewares)
    )
  )
}

@@ -769,17 +769,16 @@
 tocol = 
-(
 headers%5B
@@ -801,20 +801,8 @@
 to'%5D
- && 'https')
  %7C%7C 

set protocol directly from x-forwarded-proto