<commit_before>import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';

import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/throw';
import 'rxjs/add/observable/forkJoin';

import { CompanyService } from './company.service';
import { ProductService } from './product.service';

import { Node } from '../model/node';
import { NodeType } from '../model/node-type.enum';

import { ProductUrlHelper } from '../helpers/product-url.helper';

@Injectable()
export class ProductTreeService {

  constructor(private companyService: CompanyService, private productService: ProductService) {

  }

  public createTree(companyId: number): Observable<Map<number, Node>> {
    const productData = this.productService.getProductDataByCompanyId(companyId).map(this.createProductTree.bind(this));
    const categoryData = this.companyService.getCategoriesById(companyId).map(this.createCategoryTree.bind(this));

    return Observable.forkJoin(categoryData, productData).map(this.addProductsToCategories.bind(this));
  }

  /**
   * Add the products to their respective categories.
   */
  // private addProductsToCategories(categories: Map<number, Node>, products: Map<number, Node>): Map<number, Node> {
  private addProductsToCategories(res: Array<any>): Map<number, Node> {

    const categories: Map<number, Node> = res[0];
    const products: Map<number, Node> = res[1];

    for (const productKey of Array.from(products.keys())) {
      const product = products.get(productKey);

      const parentCategoryId = ProductUrlHelper.getIdFromUrl(product.getData().category);
      const category = categories.get(parentCategoryId);

      category.children.set(product.getId(), product);
      product.parent = category;
    }

    return categories;
  }

  /**
   * Creates a category tree from the retrieved categories
   */
  private createCategoryTree(categoryData: Map<number, any>): Map<number, Node> {
    const categoryTree = new Map<number, Node>();
<commit_msg>Added comments.<commit_after>import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';

import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/throw';
import 'rxjs/add/observable/forkJoin';

import { CompanyService } from './company.service';
import { ProductService } from './product.service';

import { Node } from '../model/node';
import { NodeType } from '../model/node-type.enum';

import { ProductUrlHelper } from '../helpers/product-url.helper';

@Injectable()
export class ProductTreeService {

  constructor(private companyService: CompanyService, private productService: ProductService) {

  }

  /**
   * Create the product and category tree.
   */
  public createTree(companyId: number): Observable<Map<number, Node>> {
    const productData = this.productService.getProductDataByCompanyId(companyId).map(this.createProductTree.bind(this));
    const categoryData = this.companyService.getCategoriesById(companyId).map(this.createCategoryTree.bind(this));

    return Observable.forkJoin(categoryData, productData).map(this.addProductsToCategories.bind(this));
  }

  /**
   * Add the products to their respective categories.
   */
  private addProductsToCategories(res: Array<any>): Map<number, Node> {

    const categories: Map<number, Node> = res[0];
    const products: Map<number, Node> = res[1];

    for (const productKey of Array.from(products.keys())) {
      const product = products.get(productKey);

      const parentCategoryId = ProductUrlHelper.getIdFromUrl(product.getData().category);
      const category = categories.get(parentCategoryId);

      category.children.set(product.getId(), product);
      product.parent = category;
    }

    return categories;
  }

  /**
   * Creates a category tree from the retrieved categories
   */
  private createCategoryTree(categoryData: Map<number, any>): Map<number, Node> {
    const categoryTree = new Map<number, Node>();

{'fraction_non_alphanumeric': 0.12996750812296926, 'fraction_numerical': 0.000999750062484379, 'mean_word_length': 4.605042016806722, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7046099', 'n_tokens_mistral': 1141, 'n_tokens_neox': 1112, 'n_words': 258}