"use strict";

function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.formPane = void 0;
var UI = _interopRequireWildcard(require("solid-ui"));
var _solidLogic = require("solid-logic");
var $rdf = _interopRequireWildcard(require("rdflib"));
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
/*
 **             Pane for running existing forms for any object
 **
 */

var ns = UI.ns;
var formPane = exports.formPane = {
  icon: UI.icons.iconBase + 'noun_122196.svg',
  name: 'form',
  audience: [ns.solid('PowerUser')],
  // Does the subject deserve this pane?
  label: function label(subject) {
    var n = UI.widgets.formsFor(subject).length;
    UI.log.debug('Form pane: forms for ' + subject + ': ' + n);
    if (!n) return null;
    return '' + n + ' forms';
  },
  render: function render(subject, context) {
    var kb = context.session.store;
    var dom = context.dom;
    var mention = function complain(message, style) {
      var pre = dom.createElement('p');
      pre.setAttribute('style', style || 'color: grey; background-color: white');
      box.appendChild(pre).textContent = message;
      return pre;
    };
    var complain = function complain(message, style) {
      mention(message, 'style', style || 'color: grey; background-color: #fdd;');
    };
    var complainIfBad = function complainIfBad(ok, body) {
      if (ok) {
        // setModifiedDate(store, kb, store);
        // rerender(box);   // Deleted forms at the moment
      } else complain('Sorry, failed to save your change:\n' + body);
    };

    // The question of where to store this data about subject
    // This in general needs a whole lot more thought
    // and it connects to the discoverbility through links

    // const t = kb.findTypeURIs(subject)

    var me = _solidLogic.authn.currentUser();
    var box = dom.createElement('div');
    box.setAttribute('class', 'formPane');
    if (!me) {
      mention('You are not logged in. If you log in and have ' + 'workspaces then you would be able to select workspace in which ' + 'to put this new information');
    } else {
      var ws = kb.each(me, ns.ui('workspace'));
      if (ws.length === 0) {
        mention("You don't seem to have any workspaces defined.  " + 'A workspace is a place on the web (http://..) or in ' + 'the file system (file:///) to store application data.\n');
      } else {
        // @@
      }
    }

    // Render forms using a given store

    var renderFormsFor = function renderFormsFor(store, subject) {
      kb.fetcher.nowOrWhenFetched(store.uri, subject, function (ok, body) {
        if (!ok) return complain('Cannot load store ' + store.uri + ': ' + body);

        //              Render the forms

        var forms = UI.widgets.formsFor(subject);

        // complain('Form for editing this form:');
        for (var i = 0; i < forms.length; i++) {
          var form = forms[i];
          var heading = dom.createElement('h4');
          box.appendChild(heading);
          if (form.uri) {
            var formStore = $rdf.Util.uri.document(form.uri);
            if (formStore.uri !== form.uri) {
              // The form is a hash-type URI
              var e = box.appendChild(UI.widgets.editFormButton(dom, box, form, formStore, complainIfBad));
              e.setAttribute('style', 'float: right;');
            }
          }
          var anchor = dom.createElement('a');
          anchor.setAttribute('href', form.uri);
          heading.appendChild(anchor);
          anchor.textContent = UI.utils.label(form, true);

          /*  Keep tis as a reminder to let a New one have its URI given by user
          mention("Where will this information be stored?")
          const ele = dom.createElement('input');
          box.appendChild(ele);
          ele.setAttribute('type', 'text');
          ele.setAttribute('size', '72');
          ele.setAttribute('maxlength', '1024');
          ele.setAttribute('style', 'font-size: 80%; color:#222;');
          ele.value = store.uri
          */

          UI.widgets.appendForm(dom, box, {}, subject, form, store, complainIfBad);
        }
      }); // end: when store loded
    }; // renderFormsFor

    // Figure out what store

    // Which places are editable and have stuff about the subject?

    var store = null;

    // 1. The document URI of the subject itself
    var docuri = $rdf.Util.uri.docpart(subject.uri);
    if (subject.uri !== docuri && kb.updater.editable(docuri, kb)) {
      store = subject.doc();
    } // an editable data file with hash

    store = store || kb.any(kb.sym(docuri), ns.link('annotationStore'));

    // 2. where stuff is already stored
    if (!store) {
      var docs = {};
      var docList = [];
      store.statementsMatching(subject).forEach(function (st) {
        docs[st.why.uri] = 1;
      });
      store.statementsMatching(undefined, undefined, subject).forEach(function (st) {
        docs[st.why.uri] = 2;
      });
      for (var d in docs) docList.push(docs[d], d);
      docList.sort();
      for (var i = 0; i < docList.length; i++) {
        var uri = docList[i][1];
        if (uri && store.updater.editable(uri)) {
          store = store.sym(uri);
          break;
        }
      }
    }

    // 3. In a workspace store
    // @@ TODO: Can probably remove _followeach (not done this time because the commit is a very safe refactor)
    var _followeach = function _followeach(kb, subject, path) {
      if (path.length === 0) return [subject];
      var oo = kb.each(subject, path[0]);
      var res = [];
      for (var _i = 0; _i < oo.length; _i++) {
        res = res.concat(_followeach(kb, oo[_i], path.slice(1)));
      }
      return res;
    };
    var date = '2014'; // @@@@@@@@@@@@ pass as parameter

    if (store) {
      // mention("@@ Ok, we have a store <" + store.uri + ">.");
      renderFormsFor(store, subject);
    } else {
      complain('No suitable store is known, to edit <' + subject.uri + '>.');
      var foobarbaz = UI.login.selectWorkspace(dom, function (ws) {
        mention('Workspace selected OK: ' + ws);
        var activities = store.each(undefined, ns.space('workspace'), ws);
        for (var j = 0; j < activities.length; j++) {
          var act = activities[j];
          var subjectDoc2 = store.any(ws, ns.space('store'));
          var start = store.any(ws, ns.cal('dtstart')).value();
          var end = store.any(ws, ns.cal('dtend')).value();
          if (subjectDoc2 && start && end && start <= date && end > date) {
            renderFormsFor(subjectDoc2, subject);
            break;
          } else {
            complain('Note no suitable annotation store in activity: ' + act);
          }
        }
      });
      box.appendChild(foobarbaz);
    }
    return box;
  }
};
//# sourceMappingURL=pane.js.map