"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useNotificationSetting = useNotificationSetting;
var _reactHookForm = require("react-hook-form");
var _zod = require("@hookform/resolvers/zod");
var z = _interopRequireWildcard(require("zod"));
var _native = require("@react-navigation/native");
var _icons = require("../../../../../core/assets/icons");
var _BehaviourProvider = require("../../../../providers/BehaviourProvider");
var _useCommunityNotificationSettingsQuery = require("../../../../hooks/queries/useCommunityNotificationSettingsQuery");
var _styles = require("../styles");
var _hooks = require("../../../../hooks");
var _enums = require("../../../../enums");
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); }
const schema = z.object({
  isEnabled: z.boolean()
});
function useNotificationSetting({
  community
}) {
  const {
    styles,
    theme
  } = (0, _styles.useStyles)();
  const {
    accessibilityId
  } = (0, _hooks.useAmityPage)({
    pageId: _enums.PageID.community_notification_page
  });
  const {
    AmityCommunityNotificationSettingPageBehavior
  } = (0, _BehaviourProvider.useBehaviour)();
  const navigation = (0, _native.useNavigation)();
  const {
    isLoading,
    isPending,
    data: settings,
    enableNotifications,
    disableNotifications
  } = (0, _useCommunityNotificationSettingsQuery.useCommunityNotificationSettingsQuery)({
    communityId: community.communityId
  });
  const {
    watch,
    control,
    setValue
  } = (0, _reactHookForm.useForm)({
    resolver: (0, _zod.zodResolver)(schema),
    values: {
      isEnabled: (settings === null || settings === void 0 ? void 0 : settings.isEnabled) ?? false
    }
  });
  const isEnabled = watch('isEnabled');
  const handleToggle = async value => {
    try {
      value ? await enableNotifications({
        communityId: community.communityId
      }) : await disableNotifications({
        communityId: community.communityId
      });
    } catch (e) {
      setValue('isEnabled', !value);
    }
  };
  const notificationActions = [{
    visible: true,
    label: 'Posts',
    iconProps: {
      xml: (0, _icons.postNotification)(),
      noContainer: true
    },
    onPress: () => {
      if (AmityCommunityNotificationSettingPageBehavior !== null && AmityCommunityNotificationSettingPageBehavior !== void 0 && AmityCommunityNotificationSettingPageBehavior.goToPostsNotificationSettingPage) {
        return AmityCommunityNotificationSettingPageBehavior.goToPostsNotificationSettingPage({
          community
        });
      }
      navigation.navigate('CommunityPostsNotificationSetting', {
        community
      });
    }
  }, {
    visible: true,
    label: 'Comments',
    iconProps: {
      xml: (0, _icons.commentNotification)(),
      noContainer: true
    },
    onPress: () => {
      if (AmityCommunityNotificationSettingPageBehavior !== null && AmityCommunityNotificationSettingPageBehavior !== void 0 && AmityCommunityNotificationSettingPageBehavior.goToCommentsNotificationSettingPage) {
        return AmityCommunityNotificationSettingPageBehavior.goToCommentsNotificationSettingPage({
          community
        });
      }
      navigation.navigate('CommunityCommentsNotificationSetting', {
        community
      });
    }
  }, {
    visible: true,
    label: 'Stories',
    iconProps: {
      xml: (0, _icons.story)(),
      noContainer: true,
      width: 24,
      height: 24
    },
    onPress: () => {
      if (AmityCommunityNotificationSettingPageBehavior !== null && AmityCommunityNotificationSettingPageBehavior !== void 0 && AmityCommunityNotificationSettingPageBehavior.goToStoriesNotificationSettingPage) {
        return AmityCommunityNotificationSettingPageBehavior.goToStoriesNotificationSettingPage({
          community
        });
      }
      navigation.navigate('CommunityStoriesNotificationSetting', {
        community
      });
    }
  }, {
    // livestream notification setting is now visible
    visible: true,
    label: 'Live streams',
    iconProps: {
      xml: (0, _icons.livestream)(),
      noContainer: true,
      width: 24,
      height: 24
    },
    onPress: () => {
      if (AmityCommunityNotificationSettingPageBehavior !== null && AmityCommunityNotificationSettingPageBehavior !== void 0 && AmityCommunityNotificationSettingPageBehavior.goToLivestreamsNotificationSettingPage) {
        return AmityCommunityNotificationSettingPageBehavior.goToLivestreamsNotificationSettingPage({
          community
        });
      }
      navigation.navigate('CommunityLivestreamsNotificationSetting', {
        community
      });
    }
  }].filter(item => item.visible);
  return {
    theme,
    styles,
    control,
    isEnabled,
    isLoading,
    isPending,
    handleToggle,
    accessibilityId,
    notificationActions
  };
}
//# sourceMappingURL=useNotificationSetting.js.map