import { defaultTemplate } from "./DefaultTemplate";
import { utils } from "@govtechsg/open-attestation";
export const repeat = times => callback => Array(times).fill(0).map((_, index) => callback(index));
export const noop = () => 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 const inIframe = () => {
  try {
    return window.self !== window.top;
  } catch (e) {
    return true;
  }
};

// FIXME: Not sure why custom type guards are used to detect v2 and v3 documents
export const isV2Document = document => {
  return !!document.$template;
};

// FIXME: Not sure why custom type guards are used to detect v2 and v3 documents
export const isV3Document = document => {
  return !!document["@context"] && !!document["openAttestationMetadata"];
};
export const isV4Document = document => utils.isRawV4Document(document);
const getTemplateName = document => {
  var _document$openAttesta;
  if (isV2Document(document) && typeof document.$template === "object") {
    return document.$template.name;
  }
  if (isV3Document(document) && (_document$openAttesta = document.openAttestationMetadata) !== null && _document$openAttesta !== void 0 && _document$openAttesta.template) {
    return document.openAttestationMetadata.template.name;
  }
  if (isV4Document(document) && document.renderMethod) {
    return document.renderMethod.name;
  }
  return "";
};
export const isV2Attachment = attachment => {
  return !!attachment.type;
};
export const getAttachmentMimeType = attachment => {
  return isV2Attachment(attachment) ? attachment.type : attachment.mimeType;
};
const truePredicate = () => true;

// TODO this function is weird, returns current template + templates for attachments
export function documentTemplates(document, templateRegistry, attachmentToComponent) {
  if (!document) return [];
  // Find the template in the template registry or use a default template
  const templateName = getTemplateName(document);
  const selectedTemplate = templateName && templateRegistry[templateName] || [defaultTemplate];

  // Add type property to differentiate between custom template tabs VS attachments tab
  const tabsRenderedFromCustomTemplates = selectedTemplate.map(template => {
    return {
      ...template,
      type: "custom-template"
    };
  }).filter(template => template.predicate ? template.predicate({
    document
  }) : truePredicate());

  // TODO: OA v4 schema does not support attachments yet
  const attachments = isV2Document(document) || isV3Document(document) ? document.attachments : [];
  const tabsRenderedFromAttachments = (attachments || []).map((attachment, index) => isV2Attachment(attachment) // v2 uses attachment.type while v3 uses attachment.mimeType
  ? {
    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
  }).filter(template => template.template);
  return [...tabsRenderedFromCustomTemplates, ...tabsRenderedFromAttachments];
}
//# sourceMappingURL=utils.js.map