import { useMemo } from 'react';
import { useNavigation } from '@react-navigation/native';
import { useForm } from 'react-hook-form';
import { zodResolver } from '@hookform/resolvers/zod';
import * as z from 'zod';
import { CommunityNotificationEventNameEnum } from '@amityco/ts-sdk-react-native';
import { useStyles } from '../styles';
import { useAmityPage } from '../../../../hooks';
import { PageID } from '../../../../enums';
import { useCommunityNotificationSettingsQuery } from '../../../../hooks/queries/useCommunityNotificationSettingsQuery';
import { useToast } from '../../../../../core/stores/slices/toastSlice';
import { TOAST } from '../../../../../core/constants';
import { NOTIFICATION_RADIO_OPTIONS } from '../../shared/constants';
import { buildNotificationEvent, getNotificationEventValue } from '../../shared/utils';
const schema = z.object({
  reactPosts: z.enum(Object.values(NOTIFICATION_RADIO_OPTIONS)),
  newPosts: z.enum(Object.values(NOTIFICATION_RADIO_OPTIONS))
});
export function usePostsNotificationSetting({
  community
}) {
  const {
    styles
  } = useStyles();
  const {
    showToast
  } = useToast();
  const {
    accessibilityId
  } = useAmityPage({
    pageId: PageID.community_posts_notification_page
  });
  const navigation = useNavigation();
  const {
    isLoading,
    isPending,
    data: settings,
    enableNotifications
  } = useCommunityNotificationSettingsQuery({
    communityId: community.communityId
  });
  const formValues = useMemo(() => settings ? {
    reactPosts: getNotificationEventValue(settings.events, CommunityNotificationEventNameEnum.POST_REACTED),
    newPosts: getNotificationEventValue(settings.events, CommunityNotificationEventNameEnum.POST_CREATED)
  } : undefined, [settings]);
  const {
    control,
    handleSubmit,
    formState: {
      isDirty,
      isValid,
      isSubmitting
    }
  } = useForm({
    resolver: zodResolver(schema),
    mode: 'onChange',
    values: formValues
  });
  const notifications = [{
    title: 'React posts',
    description: 'Receive notifications when someone make a reaction to your posts in this community.',
    radio: {
      name: 'reactPosts',
      items: [{
        label: NOTIFICATION_RADIO_OPTIONS.everyone,
        value: NOTIFICATION_RADIO_OPTIONS.everyone
      }, {
        label: NOTIFICATION_RADIO_OPTIONS.onlyModerator,
        value: NOTIFICATION_RADIO_OPTIONS.onlyModerator
      }, {
        label: NOTIFICATION_RADIO_OPTIONS.off,
        value: NOTIFICATION_RADIO_OPTIONS.off
      }]
    }
  }, {
    title: 'New posts',
    description: 'Receive notifications when someone create new posts in this community.',
    radio: {
      name: 'newPosts',
      items: [{
        label: NOTIFICATION_RADIO_OPTIONS.everyone,
        value: NOTIFICATION_RADIO_OPTIONS.everyone
      }, {
        label: NOTIFICATION_RADIO_OPTIONS.onlyModerator,
        value: NOTIFICATION_RADIO_OPTIONS.onlyModerator
      }, {
        label: NOTIFICATION_RADIO_OPTIONS.off,
        value: NOTIFICATION_RADIO_OPTIONS.off
      }]
    }
  }];
  const onSubmit = async data => {
    try {
      await enableNotifications({
        communityId: community.communityId,
        events: [buildNotificationEvent(CommunityNotificationEventNameEnum.POST_REACTED, data.reactPosts), buildNotificationEvent(CommunityNotificationEventNameEnum.POST_CREATED, data.newPosts)]
      });
      navigation.navigate('CommunityProfilePage', {
        communityId: community.communityId
      });
      showToast({
        type: 'success',
        message: TOAST.COMMUNITY.PROFILE.UPDATE.SUCCESS
      });
    } catch {
      showToast({
        type: 'informative',
        message: TOAST.COMMUNITY.PROFILE.UPDATE.FAILED
      });
    }
  };
  return {
    styles,
    control,
    isDirty,
    onSubmit,
    isLoading,
    handleSubmit,
    notifications,
    accessibilityId,
    disabled: !isDirty || !isValid || isSubmitting || isPending
  };
}
//# sourceMappingURL=usePostsNotificationSetting.js.map