import { CommunityRepository } from '@amityco/ts-sdk-react-native';
import { useToast } from '../../../../../core/stores/slices/toastSlice';
import useImagePicker from '../../../../hooks/useImagePicker';
import { useForm } from 'react-hook-form';
import { useNavigation } from '@react-navigation/native';
import * as z from 'zod';
import { zodResolver } from '@hookform/resolvers/zod';
import { AmityCommunityPrivacyEnum } from '../../shared/types';
import { useStyles } from '../styles';
import { useMutation } from '@tanstack/react-query';
import useFile from '../../../../hooks/files/useFile';
const schema = z.object({
  image: z.custom().optional(),
  displayName: z.string().trim().min(1).max(30),
  description: z.string().trim().max(180).optional(),
  categories: z.array(z.custom()),
  privacy: z.enum(AmityCommunityPrivacyEnum)
});
export const useEditCommunity = community => {
  const {
    showToast
  } = useToast();
  const {
    styles,
    theme
  } = useStyles();
  const image = useFile(community.avatarFileId);
  const {
    watch,
    formState: {
      isDirty,
      isSubmitting,
      isValid
    },
    handleSubmit,
    setValue,
    control
  } = useForm({
    resolver: zodResolver(schema),
    mode: 'onChange',
    values: {
      image,
      displayName: community.displayName || '',
      description: community.description || '',
      categories: community.categories,
      privacy: community.isPublic ? AmityCommunityPrivacyEnum.PUBLIC : AmityCommunityPrivacyEnum.PRIVATE
    }
  });
  const navigation = useNavigation();
  const {
    openImageGallery,
    progress,
    openCamera
  } = useImagePicker();
  const values = watch();
  const {
    mutateAsync
  } = useMutation({
    mutationFn: payload => CommunityRepository.updateCommunity(community.communityId, payload),
    onSuccess: () => {
      navigation.navigate('CommunityProfilePage', {
        communityId: community.communityId
      });
      showToast({
        type: 'success',
        message: 'Successfully updated community profile.'
      });
    },
    onError: () => {
      showToast({
        type: 'failed',
        message: 'Failed to save your community profile. Please try again.'
      });
    }
  });
  const onSubmit = async () => {
    var _values$image;
    const isPublic = values.privacy === AmityCommunityPrivacyEnum.PUBLIC;
    await mutateAsync({
      isPublic,
      isDiscoverable: isPublic,
      requiresJoinApproval: false,
      description: values.description,
      displayName: values.displayName,
      avatarFileId: (_values$image = values.image) === null || _values$image === void 0 ? void 0 : _values$image.fileId,
      categoryIds: values.categories.map(category => category.categoryId)
    });
  };
  return {
    isDirty,
    isSubmitting,
    isValid,
    handleSubmit,
    setValue,
    control,
    onSubmit,
    openImageGallery,
    progress,
    openCamera,
    styles,
    theme,
    navigation,
    values
  };
};
//# sourceMappingURL=useEditCommunity.js.map