import * as z from 'zod';
import { useStyles } from './styles';
import Header from './components/Header';
import { Switch, View } from 'react-native';
import { useMutation } from '@tanstack/react-query';
import { zodResolver } from '@hookform/resolvers/zod';
import { Controller, useForm } from 'react-hook-form';
import { SafeAreaView } from 'react-native-safe-area-context';
import { Typography } from '../../../../core/components/Typography/Typography';
import { CommunityRepository } from '@amityco/ts-sdk-react-native';
const schema = z.object({
  storySetting: z.boolean()
});
const useCommunityStorySetting = ({
  community
}) => {
  const {
    styles,
    theme
  } = useStyles();
  const {
    control,
    handleSubmit,
    formState: {
      isDirty,
      isSubmitting,
      isValid
    }
  } = useForm({
    resolver: zodResolver(schema),
    mode: 'onChange',
    values: {
      storySetting: community.allowCommentInStory
    }
  });
  const {
    mutateAsync
  } = useMutation({
    mutationFn: payload => CommunityRepository.updateCommunity(community.communityId, {
      storySetting: {
        enableComment: payload.storySetting
      }
    })
  });
  const onSubmit = async data => {
    await mutateAsync(data);
  };
  return {
    styles,
    theme,
    control,
    handleSubmit,
    disabled: !isDirty || !isValid || isSubmitting,
    onSubmit
  };
};
const CommunityStorySetting = ({
  community
}) => {
  const {
    styles,
    control,
    theme,
    handleSubmit,
    onSubmit
  } = useCommunityStorySetting({
    community
  });
  return /*#__PURE__*/React.createElement(SafeAreaView, {
    style: styles.container,
    edges: ['top']
  }, /*#__PURE__*/React.createElement(Header, null), /*#__PURE__*/React.createElement(Controller, {
    control: control,
    name: "storySetting",
    render: ({
      field: {
        onChange,
        value
      }
    }) => /*#__PURE__*/React.createElement(View, {
      style: styles.switchContainer
    }, /*#__PURE__*/React.createElement(View, {
      style: styles.labelContainer
    }, /*#__PURE__*/React.createElement(Typography.BodyBold, {
      style: styles.colorBase
    }, "Allow comments on community stories"), /*#__PURE__*/React.createElement(Typography.Caption, {
      style: styles.colorBaseShade1
    }, "Turn on to receive comments on stories in this community.")), /*#__PURE__*/React.createElement(Switch, {
      value: value,
      thumbColor: theme.colors.background,
      ios_backgroundColor: theme.colors.baseShade3,
      trackColor: {
        false: theme.colors.baseShade3,
        true: theme.colors.primary
      },
      onValueChange: selected => {
        onChange(selected);
        handleSubmit(onSubmit)();
      }
    }))
  }));
};
export default CommunityStorySetting;
//# sourceMappingURL=StorySetting.js.map