/**
 * Created by AlexanderC on 11/5/15.
 */

'use strict';

import Kernel from 'deep-kernel';
import DeepCore from 'deep-core';

export class Framework {
  /**
   * @param {Object} servicesMap
   * @param {String} context
   */
  constructor(servicesMap, context) {
    this._context = context;
    this._services = this._resolveServicesMap(servicesMap);
    this._version = require('../../package.json').version;
    this._kernelsMap = {};
  }

  /**
   * @returns {String}
   */
  get context() {
    return this._context;
  }

  /**
   * @returns {String}
   */
  get version() {
    return this._version;
  }

  /**
   *
   * @todo: improve it
   *
   * @param {Object} context
   * @returns {Kernel}
   *
   * @sample:
   * ```
   * exports.handler = function (event, context) {
   *   DeepFramework.KernelFromLambdaContext(context).loadFromFile("_config.json", function (deepKernel) {
   *     new Handler(deepKernel).run(event, context);
   *   });
   * };
   * KernelFromLambdaContext
   * ```
   */
  KernelFromLambdaContext(context) {
    let kernelId = '';

    if (context.hasOwnProperty('identity')
      && context.identity.hasOwnProperty('cognitoIdentityPoolId')
      && context.identity.hasOwnProperty('cognitoIdentityId')) {

      kernelId = this._context.identity.cognitoIdentityPoolId;
    }

    return this.KernelCached(kernelId);
  }

  /**
   * @param {String} id
   * @returns {Kernel}
   * @constructor
   */
  KernelCached(id) {
    if (this._kernelsMap.hasOwnProperty(id)) {
      return this._kernelsMap[id];
    }

    this._kernelsMap[id] = this._createKernel();

    return this._kernelsMap[id];
  }

  /**
   * @todo: remove method in next release (back compatibility)
   *
   * @returns {Kernel}
   * @constructor
   */
  get Kernel() {
    return this.KernelCached('');
  }

  /**
   * @returns {Function}
   * @constructor
   */
  get Core() {
    return DeepCore;
  }

  /**
   * @param {Object} servicesMap
   * @returns {Object}
   * @private
   */
  _resolveServicesMap(servicesMap) {
    let services = {};

    for (let serviceName in servicesMap) {
      if (!servicesMap.hasOwnProperty(serviceName)) {
        continue;
      }

      services[serviceName] = require(servicesMap[serviceName]);
    }

    return services;
  }

  /**
   * @returns {Kernel}
   * @private
   */
  _createKernel() {
    return new Kernel(this._services, this._context);
  }
}

@@ -436,24 +436,128 @@
  = %7B%7D;%0A  %7D%0A%0A
+  /**%0A   * @returns %7Bstring%7D%0A   */%0A  static get ANONYMOUS_IDENTITY_KEY() %7B%0A    return 'anonymous';%0A  %7D%0A%0A
   /**%0A   * @
@@ -1155,21 +1155,53 @@
 let 
-kernelId = ''
+identityId = Framework.ANONYMOUS_IDENTITY_KEY
 ;%0A%0A 
@@ -1380,22 +1380,24 @@
 %0A%0A      
-kernel
+identity
 Id = thi
@@ -1431,20 +1431,16 @@
 Identity
-Pool
 Id;%0A    
@@ -1450,33 +1450,34 @@
     return this.
-K
+_k
 ernelCached(kern
@@ -1472,22 +1472,24 @@
 lCached(
-kernel
+identity
 Id);%0A  %7D
@@ -1565,25 +1565,26 @@
 tor%0A   */%0A  
-K
+_k
 ernelCached(
@@ -1920,17 +1920,18 @@
 rn this.
-K
+_k
 ernelCac
@@ -1938,10 +1938,40 @@
 hed(
-''
+Framework.ANONYMOUS_IDENTITY_KEY
 );%0A 

Use identityId instead of identityPoolId