"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["default"] = void 0;
var UI = _interopRequireWildcard(require("solid-ui"));
var $rdf = _interopRequireWildcard(require("rdflib"));
var mime = _interopRequireWildcard(require("mime-types"));
var DOKIELI_TEMPLATE = _interopRequireWildcard(require("./new.js"));
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; }
/*   Human-readable editable "Dokieli" Pane
 **
 **  This outline pane contains the document contents for a Dokieli document
 ** The dokeili system allows the user to edit a document including anotations
 ** review.   It does not use turtle, but RDF/a
 */
// const DOKIELI_TEMPLATE_URI = 'https://dokie.li/new' // Copy to make new dok
// Distributed with this library
var _default = exports["default"] = {
  icon: UI.icons.iconBase + 'dokieli-logo.png',
  // @@ improve? more like doccument?

  name: 'Dokieli',
  mintClass: UI.ns.solid('DokieliDocument'),
  // @@ A better class?

  label: function label(subject, context) {
    var kb = context.session.store;
    var ns = UI.ns;
    var allowed = [
    // 'text/plain',
    'text/html', 'application/xhtml+xml'
    // 'image/png', 'image/jpeg', 'application/pdf',
    // 'video/mp4'
    ];
    var hasContentTypeIn = function hasContentTypeIn(kb, x, displayables) {
      var cts = kb.fetcher.getHeader(x, 'content-type');
      if (cts) {
        for (var j = 0; j < cts.length; j++) {
          for (var k = 0; k < displayables.length; k++) {
            if (cts[j].indexOf(displayables[k]) >= 0) {
              return true;
            }
          }
        }
      }
      return false;
    };

    // This data coul d come from a fetch OR from ldp comtaimner
    var hasContentTypeIn2 = function hasContentTypeIn2(kb, x, displayables) {
      var t = kb.findTypeURIs(subject);
      for (var k = 0; k < displayables.length; k++) {
        if ($rdf.Util.mediaTypeClass(displayables[k]).uri in t) {
          return true;
        }
      }
      return false;
    };
    if (!subject.uri) return null; // no bnodes

    var t = kb.findTypeURIs(subject);
    if (t[ns.link('WebPage').uri]) return 'view';
    if (hasContentTypeIn(kb, subject, allowed) || hasContentTypeIn2(kb, subject, allowed)) {
      return 'Dok';
    }
    return null;
  },
  // Create a new folder in a Solid system, with a dokieli editable document in it
  mintNew: function mintNew(context, newPaneOptions) {
    var kb = context.session.store;
    var newInstance = newPaneOptions.newInstance;
    if (!newInstance) {
      var uri = newPaneOptions.newBase;
      if (uri.endsWith('/')) {
        uri = uri.slice(0, -1);
        newPaneOptions.newBase = uri;
      }
      newInstance = kb.sym(uri);
    }
    var contentType = mime.lookup(newInstance.uri);
    if (!contentType || !contentType.includes('html')) {
      newInstance = $rdf.sym(newInstance.uri + '.html');
    }
    newPaneOptions.newInstance = newInstance; // Save for creation system

    console.log('New dokieli will make: ' + newInstance);
    var htmlContents = DOKIELI_TEMPLATE;
    var filename = newInstance.uri.split('/').slice(-1)[0];
    filename = decodeURIComponent(filename.split('.')[0]);
    var encodedTitle = filename.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
    htmlContents = htmlContents.replace('<title>', '<title>' + encodedTitle);
    htmlContents = htmlContents.replace('</article>', '<h1>' + encodedTitle + '</h1></article>');
    console.log('@@ New HTML for Dok:' + htmlContents);
    return new Promise(function (resolve) {
      kb.fetcher.webOperation('PUT', newInstance.uri, {
        data: htmlContents,
        contentType: 'text/html'
      }).then(function () {
        console.log('new Dokieli document created at ' + newPaneOptions.newInstance);
        resolve(newPaneOptions);
      })["catch"](function (err) {
        console.log('Error creating dokelili dok at ' + newPaneOptions.newInstance + ': ' + err);
      });
    });
  },
  // Derived from: humanReadablePane .. share code?
  render: function render(subject, context) {
    var myDocument = context.dom;
    var div = myDocument.createElement('div');
    var kb = context.session.store;

    //  @@ When we can, use CSP to turn off scripts within the iframe
    div.setAttribute('class', 'docView');
    var iframe = myDocument.createElement('IFRAME');
    iframe.setAttribute('src', subject.uri); // allow-same-origin
    iframe.setAttribute('class', 'doc');
    var cts = kb.fetcher.getHeader(subject.doc(), 'content-type');
    var ct = cts ? cts[0] : null;
    if (ct) {
      console.log('dokieliPane: c-t:' + ct);
    } else {
      console.log('dokieliPane: unknown content-type?');
    }

    // @@ NOte beflow - if we set ANY sandbox, then Chrome and Safari won't display it if it is PDF.
    // https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe
    // You can;'t have any sandbox and allow plugins.
    // We could sandbox only HTML files I suppose.
    // HTML5 bug: https://lists.w3.org/Archives/Public/public-html/2011Jun/0330.html

    // iframe.setAttribute('sandbox', 'allow-same-origin allow-forms'); // allow-scripts ?? no documents should be static

    iframe.setAttribute('style', 'resize = both; height: 40em; width:40em;'); // @@ improve guess
    //        iframe.setAttribute('height', '480')
    //        iframe.setAttribute('width', '640')
    var tr = myDocument.createElement('tr');
    tr.appendChild(iframe);
    div.appendChild(tr);
    return div;
  }
}; // ends
//# sourceMappingURL=dokieliPane.js.map