var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __spreadArray = (this && this.__spreadArray) || function (to, from) {
    for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
        to[j] = from[i];
    return to;
};
import { defaultTemplate } from "./DefaultTemplate";
export var repeat = function (times) { return function (callback) {
    return Array(times)
        .fill(0)
        .map(function (_, index) { return callback(index); });
}; };
export var noop = function () { 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 () {
    try {
        return window.self !== window.top;
    }
    catch (e) {
        return true;
    }
};
export var isV2Document = function (document) {
    return !!document.$template;
};
export var isV3Document = function (document) {
    return !!document["@context"];
};
var getTemplateName = function (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 (attachment) {
    return !!attachment.type;
};
export var getAttachmentMimeType = function (attachment) {
    return isV2Attachment(attachment) ? attachment.type : attachment.mimeType;
};
var truePredicate = function () { return 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
    var templateName = getTemplateName(document);
    var selectedTemplate = (templateName && templateRegistry[templateName]) || [
        defaultTemplate,
    ];
    // Add type property to differentiate between custom template tabs VS attachments tab
    var tabsRenderedFromCustomTemplates = selectedTemplate
        .map(function (template) {
        return __assign(__assign({}, template), { type: "custom-template" });
    })
        .filter(function (template) { return (template.predicate ? template.predicate({ document: document }) : truePredicate()); });
    var tabsRenderedFromAttachments = (document.attachments || [])
        .map(function (attachment, index) {
        return isV2Attachment(attachment)
            ? {
                id: "attachment-" + 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-" + 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(function (template) { return template.template; });
    return __spreadArray(__spreadArray([], tabsRenderedFromCustomTemplates), tabsRenderedFromAttachments);
}
//# sourceMappingURL=utils.js.map