import React from "react";
import { showAlertDialog } from "@applicaster/zapp-react-native-utils/alertUtils";
import { TOGGLE_FLAG_MAX_ITEMS_REACHED_EVENT } from "@applicaster/zapp-react-native-utils/actionsExecutor/consts";
import { useLocalizedStrings } from "@applicaster/zapp-react-native-utils/localizationUtils";
import { useIsScreenActive } from "@applicaster/zapp-react-native-utils/reactHooks";
import { useSubscriberFor } from "@applicaster/zapp-react-native-utils/reactHooks/useSubscriberFor";

export const useEventAlerts = (screenData: ZappRiver) => {
  const localizations = useLocalizedStrings({
    localizations: screenData?.localizations || {},
  });

  const isActive = useIsScreenActive();

  const onMaxTagsReached = React.useCallback(() => {
    // We can't skip subscribe hook call, so we have to check.
    if (!isActive || !localizations?.msg_maximum_selection_reached_message) {
      return;
    }

    showAlertDialog({
      title: "",
      message: localizations.msg_maximum_selection_reached_message,
      okButtonText:
        localizations.msg_maximum_selection_reached_message_ok_button || "OK",
    });
  }, [localizations, isActive]);

  useSubscriberFor(TOGGLE_FLAG_MAX_ITEMS_REACHED_EVENT, onMaxTagsReached);
};
