import {useMemo} from 'react';
import {View} from 'react-native';
import {useTranslation} from 'react-i18next';
import {useTimeOffPanelStyle} from '@modules/timeOff/hooks';

import {BrazilRulesMessages} from './components/BrazilRulesMessages';
import {PolicyPriorityMessages} from './components/PolicyPriorityMessages';
import {PolicyValidationMessages} from './components/PolicyValidationMessages';
import {RequestSummaryMessage} from './components/RequestSummaryMessage';
import {Props} from './interfaces';
import {getInfoMessagesDerived} from './utils';

// TODO: enhance this to avoid props drilling

export function InfoMessages({
  requestedBalance,
  isAdminOnBehalf = false,
  ...rest
}: Props) {
  const {inTime, inMoney} = requestedBalance;
  const {t} = useTranslation();

  const panelStyle = useTimeOffPanelStyle({variant: 'withMessageGap'});

  const derived = useMemo(
    () => getInfoMessagesDerived({t, isAdminOnBehalf, ...rest}),
    // rest is spread for maintainability; individual fields avoid reference churn on every render
    // eslint-disable-next-line react-hooks/exhaustive-deps
    [
      t,
      isAdminOnBehalf,
      rest.policyType,
      rest.nextCycleDate,
      rest.requestedAmount,
      rest.remainingBalance,
      rest.policyIsHoursUnit,
      rest.fromDateIsBeforeNow,
      rest.projectedBalanceValue,
      rest.showNextCycleRequestWarning,
      rest.showOverlappingRequestError,
      rest.showOverlappingRequestWarning,
    ],
  );

  return (
    <View style={panelStyle}>
      <PolicyPriorityMessages
        showOnlyAdminRequestsError={rest.showOnlyAdminRequestsError}
        showPolicyMustBeUseFirstError={rest.showPolicyMustBeUseFirstError}
      />
      <RequestSummaryMessage
        inTime={inTime}
        inMoney={inMoney}
        unitSuffix={derived.unitSuffix}
        showWarning={derived.showWarning}
        requestedAmount={rest.requestedAmount}
        descriptionText={derived.descriptionText}
        policyIsHoursUnit={rest.policyIsHoursUnit}
        fromDateIsBeforeNow={rest.fromDateIsBeforeNow}
        showRequestSummaryMessage={rest.showRequestSummaryMessage}
      />
      <PolicyValidationMessages
        policyType={rest.policyType}
        unitSuffix={derived.unitSuffix}
        isAdminOnBehalf={isAdminOnBehalf}
        requestedAmount={rest.requestedAmount}
        blockedDatesError={rest.blockedDatesError}
        showBlockedDatesError={rest.showBlockedDatesError}
        showAmountAllowedError={rest.showAmountAllowedError}
        showMinimumAllowedError={rest.showMinimumAllowedError}
        showMinimumBalanceError={rest.showMinimumBalanceError}
        newHiresRequestsBanError={rest.newHiresRequestsBanError}
        showNextCycleRequestWarning={rest.showNextCycleRequestWarning}
        showOverlappingErrorEffective={derived.showOverlappingErrorEffective}
        showPendingRequestsBalanceError={rest.showPendingRequestsBalanceError}
        showFutureRequestNotAllowedError={rest.showFutureRequestNotAllowedError}
        showOverlappingWarningEffective={
          derived.showOverlappingWarningEffective
        }
      />
      <BrazilRulesMessages
        isAdminOnBehalf={isAdminOnBehalf}
        showBrazilMinDaysError={rest.showBrazilMinDaysError}
        showBrazilMinDaysWarning={rest.showBrazilMinDaysWarning}
        showBrazilMaxPeriodsError={rest.showBrazilMaxPeriodsError}
        showBrazilMaxPeriodsWarning={rest.showBrazilMaxPeriodsWarning}
        showBrazilMinMainPeriodError={rest.showBrazilMinMainPeriodError}
        showBrazilMinMainPeriodWarning={rest.showBrazilMinMainPeriodWarning}
        showBrazilCashOutExceedsOneThirdWarning={
          rest.showBrazilCashOutExceedsOneThirdWarning
        }
        showBrazilCashOutExceedsOneThirdError={
          rest.showBrazilCashOutExceedsOneThirdError
        }
      />
    </View>
  );
}
