import { useForm } from 'react-hook-form';
import { zodResolver } from '@hookform/resolvers/zod';
import * as z from 'zod';
import { useNavigation } from '@react-navigation/native';
import { commentNotification, livestream, postNotification, story } from '../../../../../core/assets/icons';
import { useBehaviour } from '../../../../providers/BehaviourProvider';
import { useCommunityNotificationSettingsQuery } from '../../../../hooks/queries/useCommunityNotificationSettingsQuery';
import { useStyles } from '../styles';
import { useAmityPage } from '../../../../hooks';
import { PageID } from '../../../../enums';
const schema = z.object({
  isEnabled: z.boolean()
});
export function useNotificationSetting({
  community
}) {
  const {
    styles,
    theme
  } = useStyles();
  const {
    accessibilityId
  } = useAmityPage({
    pageId: PageID.community_notification_page
  });
  const {
    AmityCommunityNotificationSettingPageBehavior
  } = useBehaviour();
  const navigation = useNavigation();
  const {
    isLoading,
    isPending,
    data: settings,
    enableNotifications,
    disableNotifications
  } = useCommunityNotificationSettingsQuery({
    communityId: community.communityId
  });
  const {
    watch,
    control,
    setValue
  } = useForm({
    resolver: zodResolver(schema),
    values: {
      isEnabled: (settings === null || settings === void 0 ? void 0 : settings.isEnabled) ?? false
    }
  });
  const isEnabled = watch('isEnabled');
  const handleToggle = async value => {
    try {
      value ? await enableNotifications({
        communityId: community.communityId
      }) : await disableNotifications({
        communityId: community.communityId
      });
    } catch (e) {
      setValue('isEnabled', !value);
    }
  };
  const notificationActions = [{
    visible: true,
    label: 'Posts',
    iconProps: {
      xml: postNotification(),
      noContainer: true
    },
    onPress: () => {
      if (AmityCommunityNotificationSettingPageBehavior !== null && AmityCommunityNotificationSettingPageBehavior !== void 0 && AmityCommunityNotificationSettingPageBehavior.goToPostsNotificationSettingPage) {
        return AmityCommunityNotificationSettingPageBehavior.goToPostsNotificationSettingPage({
          community
        });
      }
      navigation.navigate('CommunityPostsNotificationSetting', {
        community
      });
    }
  }, {
    visible: true,
    label: 'Comments',
    iconProps: {
      xml: commentNotification(),
      noContainer: true
    },
    onPress: () => {
      if (AmityCommunityNotificationSettingPageBehavior !== null && AmityCommunityNotificationSettingPageBehavior !== void 0 && AmityCommunityNotificationSettingPageBehavior.goToCommentsNotificationSettingPage) {
        return AmityCommunityNotificationSettingPageBehavior.goToCommentsNotificationSettingPage({
          community
        });
      }
      navigation.navigate('CommunityCommentsNotificationSetting', {
        community
      });
    }
  }, {
    visible: true,
    label: 'Stories',
    iconProps: {
      xml: story(),
      noContainer: true,
      width: 24,
      height: 24
    },
    onPress: () => {
      if (AmityCommunityNotificationSettingPageBehavior !== null && AmityCommunityNotificationSettingPageBehavior !== void 0 && AmityCommunityNotificationSettingPageBehavior.goToStoriesNotificationSettingPage) {
        return AmityCommunityNotificationSettingPageBehavior.goToStoriesNotificationSettingPage({
          community
        });
      }
      navigation.navigate('CommunityStoriesNotificationSetting', {
        community
      });
    }
  }, {
    // livestream notification setting is now visible
    visible: true,
    label: 'Live streams',
    iconProps: {
      xml: livestream(),
      noContainer: true,
      width: 24,
      height: 24
    },
    onPress: () => {
      if (AmityCommunityNotificationSettingPageBehavior !== null && AmityCommunityNotificationSettingPageBehavior !== void 0 && AmityCommunityNotificationSettingPageBehavior.goToLivestreamsNotificationSettingPage) {
        return AmityCommunityNotificationSettingPageBehavior.goToLivestreamsNotificationSettingPage({
          community
        });
      }
      navigation.navigate('CommunityLivestreamsNotificationSetting', {
        community
      });
    }
  }].filter(item => item.visible);
  return {
    theme,
    styles,
    control,
    isEnabled,
    isLoading,
    isPending,
    handleToggle,
    accessibilityId,
    notificationActions
  };
}
//# sourceMappingURL=useNotificationSetting.js.map