"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.addPostReaction = addPostReaction;
exports.createPostToFeed = createPostToFeed;
exports.deletePostById = deletePostById;
exports.editPost = editPost;
exports.getGlobalFeed = getGlobalFeed;
exports.getPostById = getPostById;
exports.isReportTarget = isReportTarget;
exports.removePostReaction = removePostReaction;
exports.reportTargetById = reportTargetById;
exports.unReportTargetById = unReportTargetById;
var _tsSdkReactNative = require("@amityco/ts-sdk-react-native");
var _reactNative = require("react-native");
var _constants = require("../constants");
function extractLinks(text) {
  if (!text) return [];
  const urlRegex = (0, _constants.createUrlRegex)();
  const links = [];
  let match;
  while ((match = urlRegex.exec(text)) !== null) {
    links.push({
      index: match.index,
      length: match[0].length,
      url: match[0],
      renderPreview: true
    });
  }
  return links;
}
function getGlobalFeed({
  limit = 20,
  callback
}) {
  return _tsSdkReactNative.FeedRepository.getCustomRankingGlobalFeed({
    limit
  }, callback);
}
async function addPostReaction(postId, reactionName) {
  const reactionObject = new Promise(async (resolve, reject) => {
    try {
      const isPostReactionAdded = await _tsSdkReactNative.ReactionRepository.addReaction('post', postId, reactionName);
      resolve(isPostReactionAdded);
    } catch (error) {
      reject(error);
    }
  });
  return reactionObject;
}
async function removePostReaction(postId, reactionName) {
  const reactionObject = new Promise(async (resolve, reject) => {
    try {
      const isPostReactionRemoved = await _tsSdkReactNative.ReactionRepository.removeReaction('post', postId, reactionName);
      resolve(isPostReactionRemoved);
    } catch (error) {
      reject(error);
    }
  });
  return reactionObject;
}
function getPostById(postId) {
  const communityObject = new Promise((resolve, reject) => {
    let object;
    const unsubscribe = _tsSdkReactNative.PostRepository.getPost(postId, ({
      data: postInfo,
      loading,
      error
    }) => {
      if (error) {
        reject(error);
      }
      if (!loading) {
        object = postInfo;
      }
    });
    resolve({
      data: object,
      unsubscribe
    });
  });
  return communityObject;
}
async function createPostToFeed(targetType, targetId, content, postType, mentionees, mentionPosition) {
  const detectedLinks = extractLinks(content.text);
  let postParam = {
    targetType: targetType,
    targetId: targetId,
    mentionees: mentionees.length > 0 ? [{
      type: 'user',
      userIds: mentionees
    }] : [],
    metadata: {
      mentioned: mentionPosition
    },
    ...(detectedLinks.length > 0 && {
      links: detectedLinks
    })
  };
  if (postType === 'text') {
    const newPostParam = {
      data: {
        text: content.text
      },
      ...postParam
    };
    postParam = newPostParam;
  } else if (postType === 'image') {
    const formattedFileIds = content.fileIds.map(id => {
      return {
        type: _tsSdkReactNative.PostContentType.IMAGE,
        fileId: id
      };
    });
    const newPostParam = {
      data: {
        text: content.text
      },
      attachments: formattedFileIds,
      ...postParam
    };
    postParam = newPostParam;
  } else if (postType === 'video') {
    const formattedFileIds = content.fileIds.map(id => {
      return {
        type: _tsSdkReactNative.PostContentType.VIDEO,
        fileId: id
      };
    });
    const newPostParam = {
      data: {
        text: content.text
      },
      attachments: formattedFileIds,
      ...postParam
    };
    postParam = newPostParam;
  }
  const createPostObject = new Promise(async (resolve, reject) => {
    try {
      const {
        data: post
      } = await _tsSdkReactNative.PostRepository.createPost(postParam);
      resolve(post);
    } catch (error) {
      if (error.message.includes(_constants.text_contain_blocked_word)) {
        _reactNative.Alert.alert('', _constants.text_contain_blocked_word);
      }
      reject(error);
    }
  });
  return createPostObject;
}
async function editPost(postId, content, postType, mentionees, mentionPosition) {
  const detectedLinks = extractLinks(content.text);
  let postParam = {
    mentionees: mentionees.length > 0 ? [{
      type: 'user',
      userIds: mentionees
    }] : [],
    metadata: {
      mentioned: mentionPosition
    },
    ...(detectedLinks.length > 0 && {
      links: detectedLinks
    })
  };
  if (postType === 'text') {
    const newPostParam = {
      data: {
        text: content.text,
        attachments: []
      },
      ...postParam
    };
    postParam = newPostParam;
  } else if (postType === 'image') {
    const formattedFileIds = content.fileIds.map(id => {
      return {
        type: _tsSdkReactNative.PostContentType.IMAGE,
        fileId: id
      };
    });
    const newPostParam = {
      data: {
        text: content.text
      },
      attachments: formattedFileIds,
      ...postParam
    };
    postParam = newPostParam;
  } else if (postType === 'video') {
    const formattedFileIds = content.fileIds.map(id => {
      return {
        type: _tsSdkReactNative.PostContentType.VIDEO,
        fileId: id
      };
    });
    const newPostParam = {
      data: {
        text: content.text
      },
      attachments: formattedFileIds,
      ...postParam
    };
    postParam = newPostParam;
  }
  const editPostObject = new Promise(async (resolve, reject) => {
    try {
      const {
        data: post
      } = await _tsSdkReactNative.PostRepository.editPost(postId, postParam);
      resolve(post);
    } catch (error) {
      if (error.message.includes(_constants.text_contain_blocked_word)) {
        _reactNative.Alert.alert('', _constants.text_contain_blocked_word);
      }
      reject(error);
    }
  });
  return editPostObject;
}
async function deletePostById(postId) {
  const isDeletedObject = new Promise(async (resolve, reject) => {
    try {
      const softDeleted = await _tsSdkReactNative.PostRepository.deletePost(postId, false);
      if (softDeleted) {
        resolve(true);
      }
    } catch (error) {
      reject(error);
    }
  });
  return isDeletedObject;
}
async function reportTargetById(targetType, postId) {
  const isReport = new Promise(async (resolve, reject) => {
    try {
      const didCreatePostReport = await (0, _tsSdkReactNative.createReport)(targetType, postId);
      if (didCreatePostReport) {
        resolve(didCreatePostReport);
      }
    } catch (error) {
      reject(error);
    }
  });
  return isReport;
}
async function isReportTarget(targetType, targetId) {
  const isReport = new Promise(async (resolve, reject) => {
    try {
      const isReportByMe = await (0, _tsSdkReactNative.isReportedByMe)(targetType, targetId);
      if (isReportByMe) {
        resolve(isReportByMe);
      }
    } catch (error) {
      reject(error);
    }
  });
  return isReport;
}
async function unReportTargetById(targetType, targetId) {
  const isReport = new Promise(async (resolve, reject) => {
    try {
      const didDeletePostReport = await (0, _tsSdkReactNative.deleteReport)(targetType, targetId);
      if (didDeletePostReport) {
        resolve(didDeletePostReport);
      }
    } catch (error) {
      reject(error);
    }
  });
  return isReport;
}
//# sourceMappingURL=feed.js.map