import { useCallback, useEffect, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { InlineErrorType } from '../components/inputs/InlineErrorText';
import { TOKENS, useServices } from '../container-api';
import { createPINValidations } from '../utils/PINValidation';
const initialModalState = {
  visible: false,
  title: '',
  message: ''
};
export const usePINValidation = (PIN, PINTwo) => {
  const {
    t
  } = useTranslation();
  const [{
    PINSecurity
  }, inlineMessages] = useServices([TOKENS.CONFIG, TOKENS.INLINE_ERRORS]);
  const [inlineMessageField1, setInlineMessageField1] = useState();
  const [inlineMessageField2, setInlineMessageField2] = useState();
  const [modalState, setModalState] = useState(initialModalState);
  const clearModal = useCallback(() => {
    setModalState(initialModalState);
  }, []);
  const [PINValidations, setPINValidations] = useState(createPINValidations(PIN, PINSecurity.rules));
  useEffect(() => {
    setInlineMessageField1(undefined);
    setInlineMessageField2(undefined);
    setPINValidations(createPINValidations(PIN, PINSecurity.rules));
  }, [PIN, PINTwo, PINSecurity.rules]);
  const attentionMessage = useCallback((title, message, pinOne) => {
    if (inlineMessages.enabled) {
      const config = {
        message: message,
        inlineType: InlineErrorType.error,
        config: inlineMessages
      };
      if (pinOne) {
        setInlineMessageField1(config);
      } else {
        setInlineMessageField2(config);
      }
    } else {
      setModalState({
        visible: true,
        title: title,
        message: message,
        onModalDismiss: clearModal
      });
    }
  }, [inlineMessages, clearModal]);
  const validatePINEntry = useCallback((PINOne, PINTwo) => {
    for (const validation of PINValidations) {
      if (validation.isInvalid) {
        attentionMessage(t('PINCreate.InvalidPIN'), t(`PINCreate.Message.${validation.errorName}`, validation === null || validation === void 0 ? void 0 : validation.errorTextAddition), true);
        return false;
      }
    }
    if (PINOne !== PINTwo) {
      attentionMessage(t('PINCreate.InvalidPIN'), t('PINCreate.PINsDoNotMatch'), false);
      return false;
    }
    return true;
  }, [PINValidations, t, attentionMessage]);
  return {
    PINValidations,
    validatePINEntry,
    inlineMessageField1,
    inlineMessageField2,
    modalState,
    setModalState,
    clearModal,
    PINSecurity
  };
};
//# sourceMappingURL=usePINValidation.js.map