<commit_before>/**
 * @license
 * Copyright (c) 2017 Google Inc. All rights reserved.
 * This code may only be used under the BSD style license found at
 * http://polymer.github.io/LICENSE.txt
 * Code distributed by Google as part of this project is also
 * subject to an additional IP rights grant found at
 * http://polymer.github.io/PATENTS.txt
 */
"use strict";

import assert from '../platform/assert-web.js';
import Slot from './slot.js';
import DomSlot from './dom-slot.js';
import {DomContext} from './dom-context.js';
import DescriptionDomFormatter from './description-dom-formatter.js';

class SlotComposer {
  constructor(options) {
    assert(options.affordance, "Affordance is mandatory");
    assert(options.rootContext, "Root context is mandatory");

    this._containerKind = options.containerKind;
    this._affordance = options.affordance;
    this._slotClass = this.getSlotClass();
    assert(this._slotClass);

    this._contextById = this._slotClass.findRootSlots(options.rootContext) || {};
    if (Object.keys(this._contextById).length == 0) {
      // fallback to single "root" slot using the rootContext.
      this._contextById["root"] = options.rootContext;
    }

    this._suggestionsContext = options.suggestionsContext;

    this._slots = [];
  }
  get affordance() { return this._affordance; }
  getSlotClass() {
    switch(this._affordance) {
      case "dom":
      case "dom-touch":
      case "vr":
        return DomSlot;
      case "mock":
        return Slot;
      default:
        assert("unsupported affordance ", this._affordance);
    }
  }
  _getSuggestionContext() {
    switch(this._affordance) {
      case "dom":
      case "dom-touch":
      case "vr":
        return DomContext;
      default:
        assert("unsupported affordance ", this._affordance);
    }
  }
  _getDescriptionFormatter() {
    switch(this._affordance) {
      case "dom":
      case "dom-touch":
      case "vr":
        return DescriptionDomFormatter;
      default:
        assert("unsupported affordance ", this._affordance);
    }
  }

  async setSuggestions(suggestions) {
    // TODO(mmandlis): slot composer should not be familiar with suggestions concept - they should just be slots.
    if (!this._suggestionsContext) {
      return;
    }

    this._suggestionsContext.clear();
    suggestions.forEach(async suggestion => {
      let suggestionContent =
        await suggestion.description.getRecipeSuggestion(suggestion.description.arc.recipes[0].particles, this._getDescriptionFormatter());

      this._getSuggestionContext().createContext(
          this._suggestionsContext.createSuggestionElement({hash: suggestion.hash, plan: suggestion.plan}),
          suggestionContent);<commit_msg>Dont return undefined description when recipe is unnamed<commit_after>/**
 * @license
 * Copyright (c) 2017 Google Inc. All rights reserved.
 * This code may only be used under the BSD style license found at
 * http://polymer.github.io/LICENSE.txt
 * Code distributed by Google as part of this project is also
 * subject to an additional IP rights grant found at
 * http://polymer.github.io/PATENTS.txt
 */
"use strict";

import assert from '../platform/assert-web.js';
import Slot from './slot.js';
import DomSlot from './dom-slot.js';
import {DomContext} from './dom-context.js';
import DescriptionDomFormatter from './description-dom-formatter.js';

class SlotComposer {
  constructor(options) {
    assert(options.affordance, "Affordance is mandatory");
    assert(options.rootContext, "Root context is mandatory");

    this._containerKind = options.containerKind;
    this._affordance = options.affordance;
    this._slotClass = this.getSlotClass();
    assert(this._slotClass);

    this._contextById = this._slotClass.findRootSlots(options.rootContext) || {};
    if (Object.keys(this._contextById).length == 0) {
      // fallback to single "root" slot using the rootContext.
      this._contextById["root"] = options.rootContext;
    }

    this._suggestionsContext = options.suggestionsContext;

    this._slots = [];
  }
  get affordance() { return this._affordance; }
  getSlotClass() {
    switch(this._affordance) {
      case "dom":
      case "dom-touch":
      case "vr":
        return DomSlot;
      case "mock":
        return Slot;
      default:
        assert("unsupported affordance ", this._affordance);
    }
  }
  _getSuggestionContext() {
    switch(this._affordance) {
      case "dom":
      case "dom-touch":
      case "vr":
        return DomContext;
      default:
        assert("unsupported affordance ", this._affordance);
    }
  }
  _getDescriptionFormatter() {
    switch(this._affordance) {
      case "dom":
      case "dom-touch":
      case "vr":
        return DescriptionDomFormatter;
      default:
        assert("unsupported affordance ", this._affordance);
    }
  }

  async setSuggestions(suggestions) {
    // TODO(mmandlis): slot composer should not be familiar with suggestions concept - they should just be slots.
    if (!this._suggestionsContext) {
      return;
    }

    this._suggestionsContext.clear();
    suggestions.forEach(async suggestion => {
      let suggestionContent =
        await suggestion.description.getRecipeSuggestion(suggestion.description.arc.recipes[0].particles, this._getDescriptionFormatter());

      if (!suggestionContent) {
        suggestionContent = 'No suggestion content was generated (unnamed recipe and no describable particles)';
      }

      this._getSuggestionContext().createContext(
          this._suggestionsContext.createSuggestionElement({hash: suggestion.hash, plan: suggestion.plan}),
          suggestionContent);
{'fraction_non_alphanumeric': 0.11303732531399258, 'fraction_numerical': 0.0021227666725632407, 'mean_word_length': 3.219402985074627, 'pattern_counts': {'":': 20, '<': 3, '<?xml version=': 0, '>': 5, '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': '2816791', 'n_tokens_mistral': 1616, 'n_tokens_neox': 1559, 'n_words': 396}