"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _core = require("@credo-ts/core");
var _legacy = require("@hyperledger/aries-oca/build/legacy");
var _react = _interopRequireWildcard(require("react"));
var _containerApi = require("../../container-api");
var _theme = require("../../contexts/theme");
var _CredentialCard = _interopRequireDefault(require("./CredentialCard10"));
var _CredentialCard2 = _interopRequireDefault(require("./CredentialCard11"));
var _OpenIDCredentialRecordProvider = require("../../modules/openid/context/OpenIDCredentialRecordProvider");
var _display = require("../../modules/openid/display");
var _utils = require("../../modules/openid/utils/utils");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (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; }
const CredentialCard = ({
  credential,
  credDefId,
  schemaId,
  proof,
  displayItems,
  credName,
  hasAltCredentials,
  handleAltCredChange,
  style = {},
  onPress = undefined,
  credentialErrors,
  brandingOverlay
}) => {
  // add ability to reference credential by ID, allows us to get past react hook restrictions
  const [bundleResolver] = (0, _containerApi.useServices)([_containerApi.TOKENS.UTIL_OCA_RESOLVER]);
  const {
    ColorPallet
  } = (0, _theme.useTheme)();
  const [overlay, setOverlay] = (0, _react.useState)({});
  const {
    resolveBundleForCredential
  } = (0, _OpenIDCredentialRecordProvider.useOpenIDCredentials)();
  const [extraOverlayAttribute, setExtraOverlayAttribute] = (0, _react.useState)();
  (0, _react.useEffect)(() => {
    if (brandingOverlay) {
      setOverlay(brandingOverlay);
      return;
    }
    const resolveOverlay = async w3cCred => {
      const brandingOverlay = await resolveBundleForCredential(w3cCred);
      setOverlay(brandingOverlay);
    };
    if (credential instanceof _core.W3cCredentialRecord || credential instanceof _core.SdJwtVcRecord || credential instanceof _core.MdocRecord) {
      resolveOverlay(credential);
      const credentialDisplay = (0, _display.getCredentialForDisplay)(credential);
      if (credentialDisplay.display.primary_overlay_attribute) {
        const attributeValue = (0, _utils.getAttributeField)(credentialDisplay, credentialDisplay.display.primary_overlay_attribute);
        setExtraOverlayAttribute(attributeValue);
      }
    }
  }, [credential, brandingOverlay, resolveBundleForCredential]);
  const getCredOverlayType = type => {
    const isBranding10 = bundleResolver.getBrandingOverlayType() === _legacy.BrandingOverlayType.Branding10;
    if (proof) {
      return /*#__PURE__*/_react.default.createElement(_CredentialCard2.default, {
        displayItems: displayItems,
        style: isBranding10 ? {
          backgroundColor: ColorPallet.brand.secondaryBackground
        } : undefined,
        credName: credName,
        credDefId: credDefId,
        schemaId: schemaId,
        credential: credential,
        handleAltCredChange: handleAltCredChange,
        hasAltCredentials: hasAltCredentials,
        proof: true,
        elevated: true,
        credentialErrors: credentialErrors ?? [],
        brandingOverlayType: bundleResolver.getBrandingOverlayType()
      });
    }
    if (credential) {
      if (type === _legacy.BrandingOverlayType.Branding01) {
        return /*#__PURE__*/_react.default.createElement(_CredentialCard.default, {
          credential: credential,
          style: style,
          onPress: onPress
        });
      } else {
        return /*#__PURE__*/_react.default.createElement(_CredentialCard2.default, {
          credential: credential,
          style: style,
          onPress: onPress,
          credentialErrors: credentialErrors ?? [],
          brandingOverlayType: bundleResolver.getBrandingOverlayType(),
          elevated: bundleResolver.getBrandingOverlayType() === _legacy.BrandingOverlayType.Branding11
        });
      }
    } else {
      return /*#__PURE__*/_react.default.createElement(_CredentialCard2.default, {
        credDefId: credDefId,
        schemaId: schemaId,
        credName: credName,
        displayItems: displayItems,
        style: style,
        onPress: onPress,
        credentialErrors: credentialErrors ?? [],
        brandingOverlayType: bundleResolver.getBrandingOverlayType()
      });
    }
  };
  if (credential instanceof _core.W3cCredentialRecord || credential instanceof _core.SdJwtVcRecord || credential instanceof _core.MdocRecord) {
    return /*#__PURE__*/_react.default.createElement(_CredentialCard2.default, {
      credential: undefined,
      style: style,
      onPress: onPress,
      brandingOverlay: overlay,
      credentialErrors: credentialErrors ?? [],
      proof: proof,
      elevated: proof,
      displayItems: displayItems,
      hideSlice: true,
      hasAltCredentials: hasAltCredentials,
      handleAltCredChange: handleAltCredChange,
      extraOverlayParameter: extraOverlayAttribute,
      brandingOverlayType: bundleResolver.getBrandingOverlayType()
    });
  } else {
    return getCredOverlayType(bundleResolver.getBrandingOverlayType());
  }
};
var _default = exports.default = CredentialCard;
//# sourceMappingURL=CredentialCard.js.map