"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _reactNativeSafeAreaContext = require("react-native-safe-area-context");
var _Header = _interopRequireDefault(require("./components/Header"));
var _styles = require("./styles");
var _Typography = require("../../../../core/components/Typography/Typography");
var _reactNative = require("react-native");
var _tsSdkReactNative = require("@amityco/ts-sdk-react-native");
var _reactHookForm = require("react-hook-form");
var z = _interopRequireWildcard(require("zod"));
var _zod2 = require("@hookform/resolvers/zod");
var _Radio = require("../../../../core/components/Radio");
var _reactQuery = require("@tanstack/react-query");
var _toastSlice = require("../../../../core/stores/slices/toastSlice");
var _native = require("@react-navigation/native");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const schema = z.object({
  postPermission: z.custom()
});
const useCommunityPostPermission = ({
  community
}) => {
  const {
    styles
  } = (0, _styles.useStyles)();
  const {
    showToast
  } = (0, _toastSlice.useToast)();
  const navigation = (0, _native.useNavigation)();
  const {
    control,
    handleSubmit,
    formState: {
      isDirty,
      isSubmitting,
      isValid
    }
  } = (0, _reactHookForm.useForm)({
    resolver: (0, _zod2.zodResolver)(schema),
    mode: 'onChange',
    values: {
      postPermission: community.postSetting
    }
  });
  const {
    mutateAsync
  } = (0, _reactQuery.useMutation)({
    mutationFn: payload => _tsSdkReactNative.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(_reactNativeSafeAreaContext.SafeAreaView, {
    style: styles.container,
    edges: ['top']
  }, /*#__PURE__*/React.createElement(_Header.default, {
    onSave: handleSubmit(onSubmit),
    disabled: disabled
  }), /*#__PURE__*/React.createElement(_reactNative.View, {
    style: styles.labelContainer
  }, /*#__PURE__*/React.createElement(_Typography.Typography.BodyBold, {
    style: styles.colorBase
  }, "Who can post on this community"), /*#__PURE__*/React.createElement(_Typography.Typography.Caption, {
    style: styles.colorBaseShade1
  }, "You can control who can create posts in your community.")), /*#__PURE__*/React.createElement(_reactHookForm.Controller, {
    control: control,
    name: "postPermission",
    render: ({
      field: {
        onChange,
        value
      }
    }) => /*#__PURE__*/React.createElement(_Radio.Radio.Group, {
      value: value,
      onChange: onChange
    }, /*#__PURE__*/React.createElement(_Radio.Radio.Option, {
      value: _tsSdkReactNative.CommunityPostSettings.ANYONE_CAN_POST
    }, /*#__PURE__*/React.createElement(_Radio.Radio.Label, null, /*#__PURE__*/React.createElement(_Typography.Typography.Body, null, "Everyone can post")), /*#__PURE__*/React.createElement(_Radio.Radio.Icon, null)), /*#__PURE__*/React.createElement(_Radio.Radio.Option, {
      value: _tsSdkReactNative.CommunityPostSettings.ADMIN_REVIEW_POST_REQUIRED
    }, /*#__PURE__*/React.createElement(_Radio.Radio.Label, null, /*#__PURE__*/React.createElement(_Typography.Typography.Body, null, "Admin review post")), /*#__PURE__*/React.createElement(_Radio.Radio.Icon, null)), /*#__PURE__*/React.createElement(_Radio.Radio.Option, {
      value: _tsSdkReactNative.CommunityPostSettings.ONLY_ADMIN_CAN_POST
    }, /*#__PURE__*/React.createElement(_Radio.Radio.Label, null, /*#__PURE__*/React.createElement(_Typography.Typography.Body, null, "Only admins can post")), /*#__PURE__*/React.createElement(_Radio.Radio.Icon, null)))
  }));
};
var _default = exports.default = CommunityPostPermission;
//# sourceMappingURL=PostPermission.js.map