import { SafeAreaView } from 'react-native-safe-area-context';
import Header from './components/Header';
import { useStyles } from './styles';
import { Typography } from '../../../../core/components/Typography/Typography';
import { View } from 'react-native';
import { CommunityPostSettings, CommunityRepository } from '@amityco/ts-sdk-react-native';
import { Controller, useForm } from 'react-hook-form';
import * as z from 'zod';
import { zodResolver } from '@hookform/resolvers/zod';
import { Radio } from '../../../../core/components/Radio';
import { useMutation } from '@tanstack/react-query';
import { useToast } from '../../../../core/stores/slices/toastSlice';
import { useNavigation } from '@react-navigation/native';
const schema = z.object({
  postPermission: z.custom()
});
const useCommunityPostPermission = ({
  community
}) => {
  const {
    styles
  } = useStyles();
  const {
    showToast
  } = useToast();
  const navigation = useNavigation();
  const {
    control,
    handleSubmit,
    formState: {
      isDirty,
      isSubmitting,
      isValid
    }
  } = useForm({
    resolver: zodResolver(schema),
    mode: 'onChange',
    values: {
      postPermission: community.postSetting
    }
  });
  const {
    mutateAsync
  } = useMutation({
    mutationFn: payload => CommunityRepository.updateCommunity(community.communityId, {
      postSetting: payload.postPermission
    }),
    onSuccess: () => {
      navigation.navigate('CommunityProfilePage', {
        communityId: community.communityId
      });
      showToast({
        type: 'success',
        message: 'Successfully updated community profile.'
      });
    },
    onError: () => {
      showToast({
        type: 'informative',
        message: 'Failed to save your community profile. Please try again.'
      });
    }
  });
  const onSubmit = async data => {
    await mutateAsync(data);
  };
  return {
    styles,
    control,
    handleSubmit,
    disabled: !isDirty || !isValid || isSubmitting,
    onSubmit
  };
};
const CommunityPostPermission = ({
  community
}) => {
  const {
    styles,
    control,
    handleSubmit,
    onSubmit,
    disabled
  } = useCommunityPostPermission({
    community
  });
  return /*#__PURE__*/React.createElement(SafeAreaView, {
    style: styles.container,
    edges: ['top']
  }, /*#__PURE__*/React.createElement(Header, {
    onSave: handleSubmit(onSubmit),
    disabled: disabled
  }), /*#__PURE__*/React.createElement(View, {
    style: styles.labelContainer
  }, /*#__PURE__*/React.createElement(Typography.BodyBold, {
    style: styles.colorBase
  }, "Who can post on this community"), /*#__PURE__*/React.createElement(Typography.Caption, {
    style: styles.colorBaseShade1
  }, "You can control who can create posts in your community.")), /*#__PURE__*/React.createElement(Controller, {
    control: control,
    name: "postPermission",
    render: ({
      field: {
        onChange,
        value
      }
    }) => /*#__PURE__*/React.createElement(Radio.Group, {
      value: value,
      onChange: onChange
    }, /*#__PURE__*/React.createElement(Radio.Option, {
      value: CommunityPostSettings.ANYONE_CAN_POST
    }, /*#__PURE__*/React.createElement(Radio.Label, null, /*#__PURE__*/React.createElement(Typography.Body, null, "Everyone can post")), /*#__PURE__*/React.createElement(Radio.Icon, null)), /*#__PURE__*/React.createElement(Radio.Option, {
      value: CommunityPostSettings.ADMIN_REVIEW_POST_REQUIRED
    }, /*#__PURE__*/React.createElement(Radio.Label, null, /*#__PURE__*/React.createElement(Typography.Body, null, "Admin review post")), /*#__PURE__*/React.createElement(Radio.Icon, null)), /*#__PURE__*/React.createElement(Radio.Option, {
      value: CommunityPostSettings.ONLY_ADMIN_CAN_POST
    }, /*#__PURE__*/React.createElement(Radio.Label, null, /*#__PURE__*/React.createElement(Typography.Body, null, "Only admins can post")), /*#__PURE__*/React.createElement(Radio.Icon, null)))
  }));
};
export default CommunityPostPermission;
//# sourceMappingURL=PostPermission.js.map