"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _core = require("@credo-ts/core");
var _legacy = require("@bifold/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 _oca = require("../../utils/oca");
var _useCredentialErrorsFromRegistry = require("../../modules/openid/hooks/useCredentialErrorsFromRegistry");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
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 {
    ColorPalette
  } = (0, _theme.useTheme)();
  const [overlay, setOverlay] = (0, _react.useState)({});
  const {
    resolveBundleForCredential
  } = (0, _OpenIDCredentialRecordProvider.useOpenIDCredentials)();
  const [extraOverlayAttribute, setExtraOverlayAttribute] = (0, _react.useState)();
  const computedErrors = (0, _useCredentialErrorsFromRegistry.useCredentialErrorsFromRegistry)(credential, credentialErrors);
  (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) {
        var _getAttributeField;
        const attributeValue = (_getAttributeField = (0, _oca.getAttributeField)(credentialDisplay, credentialDisplay.display.primary_overlay_attribute)) === null || _getAttributeField === void 0 ? void 0 : _getAttributeField.field;
        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: ColorPalette.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: computedErrors,
      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