module.exports = ((meta, options)=>{

  var name = meta.name
  const utils = meta.utils
  
  options = options || { username : `guest`, reposerver : `https://git.bbh.org.in`, password : `noneforguest` }

  // Default set of users in main repos.
  var users = [
      { username : `${options.username}`, password : `${options.password}` }
    , { username : `guest`}
    // , { username : `baptistdev`, password : 'encrypted' }
    // , { username : `guest`, password : 'encrypted' }
  ]

  // ${options.reposerver} should be used to lookup current config.

  // User can always add more branches and remotes as needed. Mainifest defines and occupies 
      // a dictionary of well defined remote names
  var remotes = { // these are both fetch and push remotes. Use push - remotes to override push.
        'chess' : {
          priority  : 0,
            // PB : TODO -- Handle cases where a repository url can be accessed by multiple users...
            server : `${options.reposerver}`, user : { username : options.username || ''}, path : ``
          , get url(){ return `${this.server}/${this.owner}`} // fetch
          , push : 'no-pushing'
          , title : 'chess'
          , get username(){ return this.user.username }
          , owner : 'chess'
        }
      , 'chess-public' :  { priority  : 1,
              server : `https://git.bbh.org.in`, user : { username : options.username || ''}, path : ``
            , get url(){ return `${this.server}/${this.owner}`} 
            , title : 'chess-public' // PB : TODO -- rename...
            , accessibility : ['public']
            , push : 'no-pushing'
            , get username(){ return this.user.username }
            , owner : 'chess'
          }
        // PB : TODO -- Load private repositories from private config...
      , 'chess-private' : { priority  : 2,
              server : `http://git.bbh`, user : { username : options.username || ''}, path : ``
            , get url(){ return `${this.server}/${this.owner}`} 
            , title : 'chess-private'
            , accessibility : ['private']
            , push : 'no-pushing', private : true
            , get username(){ return this.user.username }
            , owner : 'chess'
          }
      , 'chess-github' : { priority  : 3,
              server : `https://github.com`, user : { username : 'baptistdev'}, path : ``
            , get url(){ return `${this.server}/${this.owner}`} // fetch
            , title : 'chess-github'
            , accessibility : ['public']
            , push : 'no-pushing' //, external : true, public : true
            , get username(){ return this.user.username }
            , owner : 'chess'
          }
    }
      
    // Multiple urls dont tell us the current origin which may be 
    // different based on currently available/accessible based on device and client.
    // We just treat them as different remotes and merge as needed.
  

  if(options.username) {
    utils.assign_core( { arraymergetype : utils.assign_core.DISTINCT_UNION }
      , remotes
      , { 'userfork' : {
                priority  : 1, 
                server : `${options.reposerver}`, user : { username : `${options.username}`}, path : ``
              , get url(){ return `${this.server}/${this.user.username}`} // fetch
              , title : 'userfork'
              , get username(){ return this.user.username }
              , get owner(){ return this.username }
              , pushable : true
            }
          , 'userfork-public' : {
              priority  : 1, 
              server : `https://git.bbh.org.in`, user : {username : `${options.username}`}, path : ``
              , get url(){ return `${this.server}/${this.user.username}`}  
              // PB : TODO - Other users may have access to this users repo. However that needs to be defined as a new remote
              , title : 'userfork-public'
              , accessibility : ['public']  /*public : true, external: true */
              , get username(){ return this.user.username }
              , get owner(){ return this.username }
              , pushable : true
            } 
            // PB : TODO -- Load private repositories from private config...
          , 'userfork-private' : { priority  : 2,
                server : `http://git.bbh`, user : { username : options.username || ''}, path : ``
                  , get url(){ return `${this.server}/${this.user.username}`} 
                  , title : 'userfork-private'
                  , accessibility : ['private']
                  , get username(){ return this.user.username }
                  , get owner(){ return this.username }
                }
            , 'userfork-unc' : {
              priority  : 3, 
              server : `//172.16.0.27/repos`, user : { username : `${options.username}`}, path : ``
                , get url(){ return `${this.server}/${this.user.username}`} // fetch
                , title : 'userfork-unc'
                , accessibility : ['unc']
                // , unc : true, private : true
                , get username(){ return this.user.username }
                , get owner(){ return this.username }
                , pushable : true
              }
        }
    )
  }
    // , { `${options.username}` : `https://git.bbh.org.in/${options.username}/elxr.git` }
  

    // PB : TODO -- 
    // , 'fetch-remotes' : [] // Multiple fetch remotes are not supported by git.
      // We therefore need to use 
      // - a pullall 
      // - or branch alias for multiple remote branch tracking branch strategy.
      // 		-- This is however limited to corresponding branch names 

    // tracking branches.
    // We at least need one branch for each remote that we wish to track.
    // , 'tracking-branches' : [
    //   { master : ['origin/master', 'chess/master'] }
    // ]

  // This is a list of all known repositories.
  var reposerverinstances = {
    //   'http://git.bbh' : { users, remotes }
    // , 'https://git.bbh.org.in' : { users, remotes, external : true }
    // , '//172.16.0.27/repos' : { users, remotes }
    // , 'https://github.com' : {
    //       // We host a miniaml set of repositories in github. 
    //       get users() { return [{ username : `${this.username}` }]}, username : `baptistdev`
    //       , remotes : { 
    //         'baptistdev-public' : { 
    //           server : `https://github.com`, user : 'bAptistdev', path : ``, get url(){ return `${this.server}/${this.user}/${this.path}`} // fetch
    //         , push : 'no-pushing' }
    //       // , Add other remotes here. 
    //     }
    //     , external : true, public : true 
    // }
  }

  Object.keys(remotes).forEach(
    (k) => {
      var v = remotes[k]
      
      // var reposerverinstances = {}
      reposerverinstances[v.server] = reposerverinstances[v.server] || { users : [], remotes : {} };
      utils.assign_core( { arraymergetype : utils.assign_core.DISTINCT_UNION }
        , reposerverinstances[v.server].users, v.users || [v.user] )
      
      // if(v.server === options.reposerver) v.upstream = true; 
      reposerverinstances[v.server].remotes[k] = v; // Assign ??
    }
  )

  return {
      // We need currently active ( as preferred by client ) repositories and remotes...
      // 
        reposervers : Object.keys(reposerverinstances)
      , reposerverinstances
      , remotes
      , repos : [
        { repo : 'elxr' }
      ]
  }
})