import core from 'core-js';

export class NavigationInstruction {
  constructor(fragment, queryString, params, queryParams, config, parentInstruction) {
    const allParams = Object.assign({}, queryParams, params);

    this.fragment = fragment;
    this.queryString = queryString;
    this.params = params || {};
    this.queryParams = queryParams;
    this.config = config;
    this.lifecycleArgs = [allParams, config, this];
    this.viewPortInstructions = {};
    this.parentInstruction = parentInstruction;

    if (parentInstruction) {
      this.params.$parent = parentInstruction.params;
    }
  }

  addViewPortInstruction(viewPortName, strategy, moduleId, component) {
    return this.viewPortInstructions[viewPortName] = {
      name: viewPortName,
      strategy: strategy,
      moduleId: moduleId,
      component: component,
      childRouter: component.childRouter,
      lifecycleArgs: this.lifecycleArgs.slice()
    };
  }

  getWildCardName() {
    let wildcardIndex = this.config.route.lastIndexOf('*');
    return this.config.route.substr(wildcardIndex + 1);
  }

  getWildcardPath() {
    let wildcardName = this.getWildCardName();
    let path = this.params[wildcardName] || '';

    if (this.queryString) {
      path += '?' + this.queryString;
    }

    return path;
  }

  getBaseUrl() {
    if (!this.params) {
      return this.fragment;
    }

    let wildcardName = this.getWildCardName();
    let path = this.params[wildcardName] || '';

    if (!path) {
      return this.fragment;
    }

    return this.fragment.substr(0, this.fragment.lastIndexOf(path));
  }
}

@@ -150,71 +150,8 @@
 ) %7B%0A
-    const allParams = Object.assign(%7B%7D, queryParams, params);%0A%0A
     
@@ -172,24 +172,24 @@
 = fragment;%0A
+
     this.que
@@ -310,60 +310,8 @@
 ig;%0A
-    this.lifecycleArgs = %5BallParams, config, this%5D;%0A
     
@@ -399,92 +399,534 @@
     
-if (parentInstruction) %7B%0A      this.params.$parent = parentInstruction.params;%0A    %7D
+let ancestorParams = %5B%5D;%0A    let current = this;%0A    do %7B%0A      let currentParams = Object.assign(%7B%7D, current.params);%0A      if (current.config.hasChildRouter) %7B%0A        // remove the param for the injected child route segment%0A        delete currentParams%5Bcurrent.getWildCardName()%5D;%0A      %7D%0A%0A      ancestorParams.unshift(currentParams);%0A      current = current.parentInstruction;%0A    %7D while(current);%0A%0A    let allParams = Object.assign(%7B%7D, queryParams, ...ancestorParams);%0A    this.lifecycleArgs = %5BallParams, config, this%5D;
 %0A  %7D

merge ancestor instruction params into current params