"use strict";Object.defineProperty(exports, "__esModule", { value: true });exports.bootGui = void 0;var path = _interopRequireWildcard(require("path"));

var carlo = _interopRequireWildcard(require("carlo"));
var _rpc = require("carlo/rpc");

var _actualCode = require("../actual-code");
var _storage = require("../storage");


var _unified = require("../source/unified");function _interopRequireWildcard(obj) {if (obj && obj.__esModule) {return obj;} else {var newObj = {};if (obj != null) {for (var key in obj) {if (Object.prototype.hasOwnProperty.call(obj, key)) {var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {};if (desc.get || desc.set) {Object.defineProperty(newObj, key, desc);} else {newObj[key] = obj[key];}}}}newObj.default = obj;return newObj;}}function _defineProperty(obj, key, value) {if (key in obj) {Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true });} else {obj[key] = value;}return obj;}

const outDir = path.join(__dirname, '..', 'app');

const actualCodeCarloPlugin = () => () => {
  const createOutput = (
  root,
  codeBlocks) =>
  async results => {
    codeBlocks.reverse().forEach(codeBlock => {
      if (results && codeBlock.hash in results) {
        const value = results[codeBlock.hash].reduce(
        (acc, res) => acc + res.data,
        '');

        const node = { type: 'code', value };
        codeBlock.parent.children = [
        ...codeBlock.parent.children.slice(0, codeBlock.index + 1),
        node,
        ...codeBlock.parent.children.slice(codeBlock.index + 1)];

      }
    });
    return (0, _unified.stringifyHtml)(root);
  };
  return {
    name: 'actual-code carlo app',
    resultProcessor: async (root, codeBlocks) => {
      return {
        output: createOutput(root, codeBlocks) };

    } };

};

class Backend {

  constructor(storage) {_defineProperty(this, "_storage", void 0);
    this._storage = storage;
  }

  initActualCode(id) {
    const actualCode = new _actualCode.ActualCode(id, this._storage);

    actualCode.registerPlugin(actualCodeCarloPlugin());
    return _rpc.rpc.handle(actualCode);
  }}


const bootGui = async opt => {
  const storage = await (0, _storage.createStorage)();
  const backend = new Backend(storage);
  // reporter.log('GUI mode')

  const cApp = await carlo.launch();
  cApp.serveFolder(outDir);
  const window = cApp.mainWindow();
  // const page = window.pageForTest()
  // page.on('console', message => {
  //   console.log(message.type())
  //   console.log(message.text())
  //   console.log(message.args())
  // })

  // cApp.serveHandler(request => {
  //   reporter.debug(`REQ ${request.url()}`)
  //   request.continue()
  // })

  await cApp.exposeFunction('stringifyHtml', async (vfile) =>
  (0, _unified.stringifyHtml)(vfile));

  await cApp.exposeFunction('getFileList', () => storage.find());
  await cApp.load('index.html', _rpc.rpc.handle(backend));
};exports.bootGui = bootGui;