<commit_before>/**
 * @license
 * Copyright (c) 2016 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
 */

import '../elements/data-explorer.js';
import Xen from '../xen/xen.js';

// notes:
// Xen memoizes template parsing when given a <template> instead of String
// Xen.Template.html converts a String to a <template>
// we assign to `html` so syntax-highlighters treat the literals as HTML
const html = Xen.Template.html;

const template = html`
  <style>
    [toolbar] {
      margin-bottom: 8px;
      padding-left: 12px;
    }
    [toolbar] > * {
      margin-right: 16px;
    }
    [banner] {
      padding: 8px 0 8px 16px;
      background-color: whitesmoke;
    }
    [store] {
      padding: 8px 0 8px 16px;
      border-bottom: 1px dashed silver;
    }
  </style>

  <div toolbar>
    <button on-click="_onUpdate">Update</button>
  </div>

  <div banner>Arc Stores</div>
  <div>{{arcStores}}</div>

  <div banner>Context Stores</div>
  <div>{{contextStores}}</div>
`;

const storeTemplate = html`
  <div store>
    <data-explorer object="{{data}}"></data-explorer>
  </div>
`;

const nameSort = (a, b) => a.name > b.name ? 1 : a.name < b.name ? -1 : 0;

//const simpleNameOfType = type => type.getEntitySchema().names[0];

const nameOfType = type => {
  let typeName = type.getEntitySchema().names[0];
  if (type.isCollection) {
    typeName = `[${typeName}]`;
  }
  return typeName;
};

class StoreExplorer extends Xen.Base {
  static get observedAttributes() { return ['arc', 'context']; }
  get template() {
    return template;
  }
  _wouldChangeProp() {
    return true;
  }
  _willReceiveProps(props, state) {
    state.needsQuery = true;
  }
  _update({arc, context}, state) {
    if (state.needsQuery) {
      if (arc) {
        state.needsQuery = false;
        this._queryArcStores(arc);
      }
      if (context) {
        state.needsQuery = false;
        this._queryContextStores(context);
      }
    }
  }
  _render(props, {arcStores, contextStores}) {
    return {
      arcStores: {
        template: storeTemplate,
        models: arcStores
      },
      contextStores: {
        template: storeTemplate,
        models: contextStores
      }
    };
  }
  async _queryContextStores(context) {
    const find = async (manifest) => {
      let stores = [];
      for (const store of manifest.stores) {
        const activeStore = await store.activate();
        stores.push([activeStore, [...manifest.storeTagsById[store.id]]]);
      }
      if (manifest.imports) {
        for (const imp of manifest.imports) {
          stores = stores.concat(await find(imp));
        }
      }
      return stores;
    };
    const stores = await this._digestStores(await find(context));
    this._setState({contextStores: stores.sort(nameSort)});
  }
  async _queryArcStores(arc) {
    const find = async (arc) => {
      const stores = [];
      for (const store of arc.stores) {
        const activeStore = await store.activate();
        stores.push([activeStore, [...arc.storeTagsById[store.id]]]);
      }
      return stores;
    };
    const stores = await this._digestStores(await find(arc), true);
    this._setState({arcStores: stores.sort(nameSort)});
  }
  async _digestStores(stores, hideNamed) {
    const result = [];
    if (stores) {
      for (const [store, tags] of stores) {
        //if (store.name === null) {<commit_msg>fix web-shell: update store-explorer with new active store api (#6314)<commit_after>/**
 * @license
 * Copyright (c) 2016 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
 */

import '../elements/data-explorer.js';
import Xen from '../xen/xen.js';

// notes:
// Xen memoizes template parsing when given a <template> instead of String
// Xen.Template.html converts a String to a <template>
// we assign to `html` so syntax-highlighters treat the literals as HTML
const html = Xen.Template.html;

const template = html`
  <style>
    [toolbar] {
      margin-bottom: 8px;
      padding-left: 12px;
    }
    [toolbar] > * {
      margin-right: 16px;
    }
    [banner] {
      padding: 8px 0 8px 16px;
      background-color: whitesmoke;
    }
    [store] {
      padding: 8px 0 8px 16px;
      border-bottom: 1px dashed silver;
    }
  </style>

  <div toolbar>
    <button on-click="_onUpdate">Update</button>
  </div>

  <div banner>Arc Stores</div>
  <div>{{arcStores}}</div>

  <div banner>Context Stores</div>
  <div>{{contextStores}}</div>
`;

const storeTemplate = html`
  <div store>
    <data-explorer object="{{data}}"></data-explorer>
  </div>
`;

const nameSort = (a, b) => a.name > b.name ? 1 : a.name < b.name ? -1 : 0;

//const simpleNameOfType = type => type.getEntitySchema().names[0];

const nameOfType = type => {
  let typeName = type.getEntitySchema().names[0];
  if (type.isCollection) {
    typeName = `[${typeName}]`;
  }
  return typeName;
};

class StoreExplorer extends Xen.Base {
  static get observedAttributes() { return ['arc', 'context']; }
  get template() {
    return template;
  }
  _wouldChangeProp() {
    return true;
  }
  _willReceiveProps(props, state) {
    state.needsQuery = true;
  }
  _update({arc, context}, state) {
    if (state.needsQuery) {
      if (arc) {
        state.needsQuery = false;
        this._queryArcStores(arc);
        if (context) {
          state.needsQuery = false;
          this._queryContextStores(context, arc.storageService);
        }
      }
    }
  }
  _render(props, {arcStores, contextStores}) {
    return {
      arcStores: {
        template: storeTemplate,
        models: arcStores
      },
      contextStores: {
        template: storeTemplate,
        models: contextStores
      }
    };
  }
  async _queryContextStores(context, storageService) {
    const find = async (manifest) => {
      let stores = [];
      for (const store of manifest.stores) {
        const activeStore = await storageService.getActiveStore(store);
        stores.push([activeStore, [...manifest.storeTagsById[store.id]]]);
      }
      if (manifest.imports) {
        for (const imp of manifest.imports) {
          stores = stores.concat(await find(imp));
        }
      }
      return stores;
    };
    const stores = await this._digestStores(await find(context));
    this._setState({contextStores: stores.sort(nameSort)});
  }
  async _queryArcStores(arc) {
    const find = async (arc) => {
      const stores = [];
      for (const store of arc.stores) {
        const activeStore = await arc.getActiveStore(store);
        stores.push([activeStore, [...arc.storeTagsById[store.id]]]);
      }
      return stores;
    };
    const stores = await this._digestStores(await find(arc), true);
    this._setState({arcStores: stores.sort(nameSort)});
  }
  async _digestStores(stores, hideNamed) {
    const result = [];
    if (stores) {
      for (const [store, tags] of stores) {
        //if (store.name === null) {
{'fraction_non_alphanumeric': 0.13684640522875818, 'fraction_numerical': 0.007352941176470588, 'mean_word_length': 2.6853988961364776, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 57, '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': '5039908', 'n_tokens_mistral': 2453, 'n_tokens_neox': 2318, 'n_words': 603}