import { useCallback, useEffect, useState } from 'react';
import { DeviceEventEmitter } from 'react-native';
import { EventTypes } from '../../../constants';
import { BifoldError } from '../../../types/error';
import { useAgent } from '@credo-ts/react-hooks';
import { useTranslation } from 'react-i18next';
import { getCredentialsForProofRequest } from '../resolverProof';
import { acquirePreAuthorizedAccessToken, receiveCredentialFromOpenId4VciOffer, resolveOpenId4VciOffer } from '../offerResolve';
export const useOpenID = ({
  openIDUri,
  openIDPresentationUri
}) => {
  const [openIdRecord, setOpenIdRecord] = useState();
  const {
    agent
  } = useAgent();
  const {
    t
  } = useTranslation();
  const resolveOpenIDCredential = useCallback(async uri => {
    if (!agent) {
      return;
    }
    try {
      const resolvedCredentialOffer = await resolveOpenId4VciOffer({
        agent: agent,
        uri: uri
      });
      const tokenResponse = await acquirePreAuthorizedAccessToken({
        agent,
        resolvedCredentialOffer
      });
      return await receiveCredentialFromOpenId4VciOffer({
        agent,
        resolvedCredentialOffer,
        accessToken: tokenResponse
      });
    } catch (err) {
      //TODO: Sppecify different error
      const error = new BifoldError(t('Error.Title1043'), t('Error.Message1043'), (err === null || err === void 0 ? void 0 : err.message) ?? err, 1043);
      DeviceEventEmitter.emit(EventTypes.ERROR_ADDED, error);
    }
  }, [agent, t]);
  const resolveOpenIDPresentationRequest = useCallback(async uri => {
    if (!agent) {
      return;
    }
    try {
      const record = await getCredentialsForProofRequest({
        agent: agent,
        uri: uri
      });
      return record;
    } catch (err) {
      const error = new BifoldError(t('Error.Title1043'), t('Error.Message1043'), (err === null || err === void 0 ? void 0 : err.message) ?? err, 1043);
      DeviceEventEmitter.emit(EventTypes.ERROR_ADDED, error);
    }
  }, [agent, t]);
  useEffect(() => {
    if (!openIDPresentationUri) {
      return;
    }
    resolveOpenIDPresentationRequest(openIDPresentationUri).then(value => {
      if (value) {
        setOpenIdRecord(value);
      }
    });
  }, [openIDPresentationUri, resolveOpenIDPresentationRequest]);
  useEffect(() => {
    if (!openIDUri) {
      return;
    }
    resolveOpenIDCredential(openIDUri).then(value => {
      if (value) {
        setOpenIdRecord(value);
      }
    });
  }, [openIDUri, resolveOpenIDCredential]);
  return openIdRecord;
};
//# sourceMappingURL=openid.js.map