import _Object$keys from "@babel/runtime-corejs3/core-js-stable/object/keys";
import _Object$getOwnPropertySymbols from "@babel/runtime-corejs3/core-js-stable/object/get-own-property-symbols";
import _Object$getOwnPropertyDescriptor from "@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptor";
import _Object$getOwnPropertyDescriptors from "@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptors";
import _toConsumableArray from "@babel/runtime-corejs3/helpers/toConsumableArray";
import _defineProperty from "@babel/runtime-corejs3/helpers/defineProperty";
import _typeof from "@babel/runtime-corejs3/helpers/typeof";

function ownKeys(object, enumerableOnly) { var keys = _Object$keys(object); if (_Object$getOwnPropertySymbols) { var symbols = _Object$getOwnPropertySymbols(object); if (enumerableOnly) { symbols = _filterInstanceProperty(symbols).call(symbols, function (sym) { return _Object$getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (_Object$getOwnPropertyDescriptors) { Object.defineProperties(target, _Object$getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, _Object$getOwnPropertyDescriptor(source, key)); }); } } return target; }

import _mapInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/map";
import _fillInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/fill";
import _filterInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/filter";
import _concatInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/concat";
import { defaultTemplate } from "./DefaultTemplate";
export var repeat = function repeat(times) {
  return function (callback) {
    var _context, _context2;

    return _mapInstanceProperty(_context = _fillInstanceProperty(_context2 = Array(times)).call(_context2, 0)).call(_context, function (_, index) {
      return callback(index);
    });
  };
};
export var noop = function noop() {
  return void 0;
}; // Currently using https://stackoverflow.com/questions/326069/how-to-identify-if-a-webpage-is-being-loaded-inside-an-iframe-or-directly-into-t

export var inIframe = function inIframe() {
  try {
    return window.self !== window.top;
  } catch (e) {
    return true;
  }
};
export var isV2Document = function isV2Document(document) {
  return !!document.$template;
};
export var isV3Document = function isV3Document(document) {
  return !!document["@context"];
};

var getTemplateName = function getTemplateName(document) {
  if (isV2Document(document) && _typeof(document.$template) === "object") {
    return document.$template.name;
  }

  if (isV3Document(document) && document.openAttestationMetadata.template) {
    return document.openAttestationMetadata.template.name;
  }

  return "";
};

export var isV2Attachment = function isV2Attachment(attachment) {
  return !!attachment.type;
};
export var getAttachmentMimeType = function getAttachmentMimeType(attachment) {
  return isV2Attachment(attachment) ? attachment.type : attachment.mimeType;
};

var truePredicate = function truePredicate() {
  return true;
}; // TODO this function is weird, returns current template + templates for attachments


export function documentTemplates(document, templateRegistry, attachmentToComponent) {
  var _context3, _context4, _context5, _context6;

  if (!document) return []; // Find the template in the template registry or use a default template

  var templateName = getTemplateName(document);
  var selectedTemplate = templateName && templateRegistry[templateName] || [defaultTemplate]; // Add type property to differentiate between custom template tabs VS attachments tab

  var tabsRenderedFromCustomTemplates = _filterInstanceProperty(_context3 = _mapInstanceProperty(selectedTemplate).call(selectedTemplate, function (template) {
    return _objectSpread(_objectSpread({}, template), {}, {
      type: "custom-template"
    });
  })).call(_context3, function (template) {
    return template.predicate ? template.predicate({
      document: document
    }) : truePredicate();
  });

  var tabsRenderedFromAttachments = _filterInstanceProperty(_context4 = _mapInstanceProperty(_context5 = document.attachments || []).call(_context5, function (attachment, index) {
    return isV2Attachment(attachment) ? {
      id: "attachment-".concat(index),
      label: attachment.filename || "Unknown filename",
      type: attachment.type || "Unknown filetype",
      template: attachmentToComponent(attachment, document) // eslint-disable-line @typescript-eslint/no-non-null-assertion

    } : {
      id: "attachment-".concat(index),
      label: attachment.fileName || "Unknown filename",
      type: attachment.mimeType || "Unknown filetype",
      template: attachmentToComponent(attachment, document) // eslint-disable-line @typescript-eslint/no-non-null-assertion

    };
  })).call(_context4, function (template) {
    return template.template;
  });

  return _concatInstanceProperty(_context6 = []).call(_context6, _toConsumableArray(tabsRenderedFromCustomTemplates), _toConsumableArray(tabsRenderedFromAttachments));
}
//# sourceMappingURL=utils.js.map