"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useHeader = useHeader;
var _react = require("react");
var _objects = require("../../../../../../hooks/objects");
var _time = require("../../../../../../../core/utils/time");
var _styles = require("../styles");
var _native = require("@react-navigation/native");
var _BehaviourProvider = require("../../../../../../providers/BehaviourProvider");
var _hooks = require("../../../../../../hooks");
var _useAuth = _interopRequireDefault(require("../../../../../../../core/hooks/useAuth"));
var _useFollowUser = require("../../../../../../hooks/queries/useFollowUser");
var _useSocialSettings = _interopRequireDefault(require("../../../../../../../core/hooks/useSocialSettings"));
var _bottomSheetSlice = require("../../../../../../../core/stores/slices/bottomSheetSlice");
var _toastSlice = require("../../../../../../../core/stores/slices/toastSlice");
var _icons = require("../../../../../../../core/assets/icons");
var _constants = require("../../../../../../../core/constants");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function useHeader(user) {
  const BADGE_SIZE = 20;
  const BADGE_GAP = 6;
  const navigation = (0, _native.useNavigation)();
  const {
    AmityUserProfileHeaderComponentBehavior
  } = (0, _BehaviourProvider.useBehaviour)();
  const {
    unBlockUser
  } = (0, _hooks.useBlockUser)();
  const {
    client
  } = (0, _useAuth.default)();
  const {
    socialSettings
  } = (0, _useSocialSettings.default)();
  const isPrivateNetwork = (socialSettings === null || socialSettings === void 0 ? void 0 : socialSettings.userPrivacySetting) === 'private';
  const {
    openBottomSheet,
    closeBottomSheet,
    bottomSheetHeight
  } = (0, _bottomSheetSlice.useBottomSheet)();
  const {
    showToast
  } = (0, _toastSlice.useToast)();
  const {
    followUser,
    unfollowUser,
    isLoading: isFollowLoading
  } = (0, _useFollowUser.useFollowUser)();
  const {
    followInfo
  } = (0, _objects.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: (0, _icons.unfollow)()
    },
    testID: 'unfollow-button',
    accessibilityLabel: 'Unfollow',
    onPress: () => {
      closeBottomSheet();
      handleUnfollow();
    }
  }];
  const [badgePosition, setBadgePosition] = (0, _react.useState)(null);
  const {
    styles,
    theme
  } = (0, _styles.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: _constants.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: (0, _time.formatCount)((followInfo === null || followInfo === void 0 ? void 0 : followInfo.followingCount) ?? 0),
    followerCount: (0, _time.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