import { useState } from 'react';
import { useFollowInfo } from '../../../../../../hooks/objects';
import { formatCount } from '../../../../../../../core/utils/time';
import { useStyles } from '../styles';
import { useNavigation } from '@react-navigation/native';
import { useBehaviour } from '../../../../../../providers/BehaviourProvider';
import { useBlockUser } from '../../../../../../hooks';
import useAuth from '../../../../../../../core/hooks/useAuth';
import { useFollowUser } from '../../../../../../hooks/queries/useFollowUser';
import useSocialSettings from '../../../../../../../core/hooks/useSocialSettings';
import { useBottomSheet } from '../../../../../../../core/stores/slices/bottomSheetSlice';
import { useToast } from '../../../../../../../core/stores/slices/toastSlice';
import { unfollow } from '../../../../../../../core/assets/icons';
import { TOAST } from '../../../../../../../core/constants';
export function useHeader(user) {
  const BADGE_SIZE = 20;
  const BADGE_GAP = 6;
  const navigation = useNavigation();
  const {
    AmityUserProfileHeaderComponentBehavior
  } = useBehaviour();
  const {
    unBlockUser
  } = useBlockUser();
  const {
    client
  } = useAuth();
  const {
    socialSettings
  } = useSocialSettings();
  const isPrivateNetwork = (socialSettings === null || socialSettings === void 0 ? void 0 : socialSettings.userPrivacySetting) === 'private';
  const {
    openBottomSheet,
    closeBottomSheet,
    bottomSheetHeight
  } = useBottomSheet();
  const {
    showToast
  } = useToast();
  const {
    followUser,
    unfollowUser,
    isLoading: isFollowLoading
  } = useFollowUser();
  const {
    followInfo
  } = useFollowInfo({
    userId: (user === null || user === void 0 ? void 0 : user.userId) ?? '',
    enabled: !!(user !== null && user !== void 0 && user.userId)
  });
  const isMyProfile = (client === null || client === void 0 ? void 0 : client.userId) === (user === null || user === void 0 ? void 0 : user.userId);
  const isBlockedByMe = (followInfo === null || followInfo === void 0 ? void 0 : followInfo.status) === 'blocked';
  const isFollowing = (followInfo === null || followInfo === void 0 ? void 0 : followInfo.status) === 'accepted';
  const isPending = (followInfo === null || followInfo === void 0 ? void 0 : followInfo.status) === 'pending';
  const pendingFollowRequestCount = (followInfo === null || followInfo === void 0 ? void 0 : followInfo.pendingCount) ?? 0;
  const showPendingBanner = isMyProfile && isPrivateNetwork && pendingFollowRequestCount > 0;
  const handleUnblock = () => {
    unBlockUser((user === null || user === void 0 ? void 0 : user.userId) ?? '', (user === null || user === void 0 ? void 0 : user.displayName) ?? '');
  };
  const handleFollow = () => {
    if (!(user !== null && user !== void 0 && user.userId)) return;
    followUser(user.userId);
  };
  const handleUnfollow = () => {
    if (!(user !== null && user !== void 0 && user.userId)) return;
    unfollowUser(user.userId, {
      confirm: isPrivateNetwork
    });
  };
  const followingActions = [{
    label: 'Unfollow',
    iconProps: {
      xml: unfollow()
    },
    testID: 'unfollow-button',
    accessibilityLabel: 'Unfollow',
    onPress: () => {
      closeBottomSheet();
      handleUnfollow();
    }
  }];
  const [badgePosition, setBadgePosition] = useState(null);
  const {
    styles,
    theme
  } = useStyles({
    badgePaddingRight: user !== null && user !== void 0 && user.isBrand ? BADGE_SIZE + BADGE_GAP : 0,
    badgeTop: (badgePosition === null || badgePosition === void 0 ? void 0 : badgePosition.top) ?? 0,
    badgeLeft: (badgePosition === null || badgePosition === void 0 ? void 0 : badgePosition.left) ?? 0
  });
  const onTextLayout = e => {
    const {
      lines
    } = e.nativeEvent;
    if (lines.length > 0) {
      const lastLine = lines[lines.length - 1];
      setBadgePosition({
        top: lastLine.y + (lastLine.height - BADGE_SIZE) / 2,
        left: lastLine.width + BADGE_GAP
      });
    }
  };
  const navigateToPendingFollowRequests = () => {
    if (AmityUserProfileHeaderComponentBehavior !== null && AmityUserProfileHeaderComponentBehavior !== void 0 && AmityUserProfileHeaderComponentBehavior.goToPendingFollowRequestPage) {
      AmityUserProfileHeaderComponentBehavior.goToPendingFollowRequestPage();
    } else {
      navigation.navigate('UserPendingFollowRequests');
    }
  };
  const navigateToRelationship = tab => {
    if (!(user !== null && user !== void 0 && user.userId)) return;
    if (!isMyProfile && isPrivateNetwork && !isFollowing) {
      showToast({
        type: 'informative',
        message: TOAST.USER.RELATIONSHIP.FOLLOW_TO_INTERACT
      });
      return;
    }
    if (AmityUserProfileHeaderComponentBehavior !== null && AmityUserProfileHeaderComponentBehavior !== void 0 && AmityUserProfileHeaderComponentBehavior.goToUserRelationshipPage) {
      AmityUserProfileHeaderComponentBehavior.goToUserRelationshipPage({
        userId: user.userId,
        selectedTab: tab
      });
    } else {
      navigation.push('UserRelationship', {
        userId: user.userId,
        selectedTab: tab
      });
    }
  };
  return {
    styles,
    theme,
    onTextLayout,
    badgePosition,
    badgeSize: BADGE_SIZE,
    navigateToRelationship,
    showBadge: user === null || user === void 0 ? void 0 : user.isBrand,
    brandBadgeAccessibilityLabel: 'Brand verified',
    followingCount: formatCount((followInfo === null || followInfo === void 0 ? void 0 : followInfo.followingCount) ?? 0),
    followerCount: formatCount((followInfo === null || followInfo === void 0 ? void 0 : followInfo.followerCount) ?? 0),
    isMyProfile,
    isBlockedByMe,
    isFollowing,
    isPending,
    isFollowLoading,
    handleUnblock,
    handleFollow,
    handleUnfollow,
    followingActions,
    openBottomSheet,
    closeBottomSheet,
    bottomSheetHeight,
    showPendingBanner,
    pendingFollowRequestCount,
    navigateToPendingFollowRequests
  };
}
//# sourceMappingURL=useHeader.js.map