import React from "react";
import { isV2Document, isV3Document, isV4Document } from "./utils";
import { JsonView } from "./JsonView";
import { jsx as ___EmotionJSX } from "@emotion/react";
const DEFAULT_ID = "default-template";
const container = {
  maxWidth: "1024px",
  marginRight: "auto",
  marginLeft: "auto",
  paddingRight: "1rem",
  paddingLeft: "1rem"
};
export function extractTemplateInfo(document) {
  if (isV2Document(document)) {
    const template = document.$template;
    if (typeof template === "string") {
      // legacy open certs. uses centralised repository.
      return undefined;
    } else if (template !== undefined) {
      var _template$url;
      return {
        name: template.name,
        type: template.type,
        url: (_template$url = template.url) !== null && _template$url !== void 0 ? _template$url : ""
      };
    }
  } else if (isV3Document(document)) {
    const template = document.openAttestationMetadata.template;
    if (template !== undefined) {
      return {
        name: template.name,
        type: template.type,
        url: template.url
      };
    }
  } else if (isV4Document(document)) {
    var _docAsAny$renderMetho;
    const docAsAny = document;
    const renderMethod = (_docAsAny$renderMetho = docAsAny.renderMethod) === null || _docAsAny$renderMetho === void 0 ? void 0 : _docAsAny$renderMetho.find(method => method.type === "SvgRenderingTemplate2023");
    if ((renderMethod === null || renderMethod === void 0 ? void 0 : renderMethod.type) === "SvgRenderingTemplate2023") {
      var _renderMethod$name;
      return {
        name: (_renderMethod$name = renderMethod.name) !== null && _renderMethod$name !== void 0 ? _renderMethod$name : "",
        type: renderMethod.type,
        url: renderMethod.id
      };
    }
  }
  return undefined;
}
const TemplateInfoComponent = _ref => {
  let {
    template
  } = _ref;
  return ___EmotionJSX("span", {
    style: {
      fontFamily: "Courier"
    },
    "data-testid": "template-info"
  }, "Template Name : \u201C", template.name, "\u201D", ___EmotionJSX("br", null), "Type: \u201C", template.type, "\u201D", ___EmotionJSX("br", null), "URL: \u201C", template.url, "\u201D");
};
export const DefaultTemplate = props => {
  return ___EmotionJSX("div", {
    id: DEFAULT_ID,
    "data-testid": DEFAULT_ID
  }, ___EmotionJSX("div", {
    style: {
      ...container,
      fontFamily: "Arial",
      wordBreak: "break-all"
    }
  }, ___EmotionJSX("div", {
    style: {
      backgroundColor: "#FDFDEA",
      borderLeft: "2px solid #8E4B10",
      padding: "16px 16px 16px 18px"
    }
  }, ___EmotionJSX("p", {
    style: {
      margin: "0px",
      lineHeight: "21px",
      fontSize: "16px",
      color: "#8E4B10",
      fontWeight: "700"
    }
  }, props.title), ___EmotionJSX("p", {
    style: {
      margin: "0px",
      lineHeight: "21px",
      fontSize: "14px",
      color: "#374151",
      marginTop: "6px"
    }
  }, props.description)), props.document && ___EmotionJSX(JsonView, {
    src: props.document
  })));
};
export const NoTemplate = props => {
  return ___EmotionJSX(DefaultTemplate, {
    title: "The contents of this document have not been formatted",
    description: ___EmotionJSX(React.Fragment, null, "As the issuer did not design a template to display its contents, this current display is intended."),
    document: props.document
  });
};
export const WrongTemplate = props => {
  const templateInfo = extractTemplateInfo(props.document);
  return ___EmotionJSX(DefaultTemplate, {
    title: "This document has display issues",
    description: ___EmotionJSX(React.Fragment, null, "An incorrect template has been used for this document. Please contact the issuer with the information below:", templateInfo && ___EmotionJSX(React.Fragment, null, ___EmotionJSX("br", null), ___EmotionJSX("br", null), ___EmotionJSX(TemplateInfoComponent, {
      template: templateInfo
    }))),
    document: props.document
  });
};
export const ConnectionFailureTemplate = props => {
  const templateInfo = props.document ? extractTemplateInfo(props.document) : undefined;
  return ___EmotionJSX(DefaultTemplate, {
    title: "This document might be having loading issues",
    description: ___EmotionJSX(React.Fragment, null, "Try refreshing the page or check your internet connection. If the issue continues, please contact the issuer with the information below:", ___EmotionJSX("br", null), ___EmotionJSX("br", null), props.document && templateInfo ? ___EmotionJSX(TemplateInfoComponent, {
      template: templateInfo
    }) : ___EmotionJSX("span", {
      style: {
        fontFamily: "Courier"
      }
    }, "Template URL: \u201C", props.source, "\u201D")),
    document: props.document
  });
};
export const TamperedSvgTemplate = props => {
  const templateInfo = extractTemplateInfo(props.document);
  return ___EmotionJSX(DefaultTemplate, {
    title: "The remote content for this document has been modified",
    description: ___EmotionJSX(React.Fragment, null, "The display for this document has been modified and no longer matches its original state. Please contact the issuer with the information below:", templateInfo && ___EmotionJSX(React.Fragment, null, ___EmotionJSX("br", null), ___EmotionJSX("br", null), ___EmotionJSX(TemplateInfoComponent, {
      template: templateInfo
    }))),
    document: props.document
  });
};
export const wrongTemplate = {
  id: DEFAULT_ID,
  label: "Default",
  template: WrongTemplate
};
export const noTemplate = {
  id: DEFAULT_ID,
  label: "Default",
  template: NoTemplate
};
//# sourceMappingURL=DefaultTemplate.js.map