import * as jsyaml from 'js-yaml';

import { pyodideLoaded, loadedEnvironments, mode, addInitializer } from '../stores';
import { loadPackage, loadFromFile } from '../interpreter';

// Premise used to connect to the first available pyodide interpreter
let pyodideReadyPromise;
let environments;
let currentMode;

pyodideLoaded.subscribe(value => {
  pyodideReadyPromise = value;
});

loadedEnvironments.subscribe(value => {
  environments = value;
});

mode.subscribe(value => {
  currentMode = value;
});

export class PyEnv extends HTMLElement {
  shadow: ShadowRoot;
  wrapper: HTMLElement;
  code: string;
  environment: any;

  constructor() {
    super();

    this.shadow = this.attachShadow({ mode: 'open'});
    this.wrapper = document.createElement('slot');
  }

  connectedCallback() {
    this.code = this.innerHTML;
    this.innerHTML = '';

    let env = [];
    let paths = [];

    this.environment = jsyaml.load(this.code);
    if (this.environment === undefined)
       return

    for (let entry of this.environment) {
      if (typeof entry == "string" ){
        env.push(entry);
      }
      else if (entry.hasOwnProperty('paths')){
        for (let path of entry.paths) {
          paths.push(path);
        }
      }
    }

    async function loadEnv() {
      let pyodide = await pyodideReadyPromise;
      await loadPackage(env, pyodide);
      console.log("enviroment loaded")
    }

    async function loadPaths() {
      let pyodide = await pyodideReadyPromise;
      for (let singleFile of paths) {
        await loadFromFile(singleFile, pyodide);
      }
      console.log("paths loaded")
    }
    addInitializer(loadEnv);
    addInitializer(loadPaths);
    console.log("enviroment loading...", env)

  }
}
