import { createReport, deleteReport, FeedRepository, isReportedByMe, PostContentType, PostRepository, ReactionRepository } from '@amityco/ts-sdk-react-native';
import { Alert } from 'react-native';
import { createUrlRegex, text_contain_blocked_word } from '../constants';
function extractLinks(text) {
  if (!text) return [];
  const urlRegex = 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;
}
export function getGlobalFeed({
  limit = 20,
  callback
}) {
  return FeedRepository.getCustomRankingGlobalFeed({
    limit
  }, callback);
}
export async function addPostReaction(postId, reactionName) {
  const reactionObject = new Promise(async (resolve, reject) => {
    try {
      const isPostReactionAdded = await ReactionRepository.addReaction('post', postId, reactionName);
      resolve(isPostReactionAdded);
    } catch (error) {
      reject(error);
    }
  });
  return reactionObject;
}
export async function removePostReaction(postId, reactionName) {
  const reactionObject = new Promise(async (resolve, reject) => {
    try {
      const isPostReactionRemoved = await ReactionRepository.removeReaction('post', postId, reactionName);
      resolve(isPostReactionRemoved);
    } catch (error) {
      reject(error);
    }
  });
  return reactionObject;
}
export function getPostById(postId) {
  const communityObject = new Promise((resolve, reject) => {
    let object;
    const unsubscribe = PostRepository.getPost(postId, ({
      data: postInfo,
      loading,
      error
    }) => {
      if (error) {
        reject(error);
      }
      if (!loading) {
        object = postInfo;
      }
    });
    resolve({
      data: object,
      unsubscribe
    });
  });
  return communityObject;
}
export 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: 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: 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 PostRepository.createPost(postParam);
      resolve(post);
    } catch (error) {
      if (error.message.includes(text_contain_blocked_word)) {
        Alert.alert('', text_contain_blocked_word);
      }
      reject(error);
    }
  });
  return createPostObject;
}
export 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: 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: 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 PostRepository.editPost(postId, postParam);
      resolve(post);
    } catch (error) {
      if (error.message.includes(text_contain_blocked_word)) {
        Alert.alert('', text_contain_blocked_word);
      }
      reject(error);
    }
  });
  return editPostObject;
}
export async function deletePostById(postId) {
  const isDeletedObject = new Promise(async (resolve, reject) => {
    try {
      const softDeleted = await PostRepository.deletePost(postId, false);
      if (softDeleted) {
        resolve(true);
      }
    } catch (error) {
      reject(error);
    }
  });
  return isDeletedObject;
}
export async function reportTargetById(targetType, postId) {
  const isReport = new Promise(async (resolve, reject) => {
    try {
      const didCreatePostReport = await createReport(targetType, postId);
      if (didCreatePostReport) {
        resolve(didCreatePostReport);
      }
    } catch (error) {
      reject(error);
    }
  });
  return isReport;
}
export async function isReportTarget(targetType, targetId) {
  const isReport = new Promise(async (resolve, reject) => {
    try {
      const isReportByMe = await isReportedByMe(targetType, targetId);
      if (isReportByMe) {
        resolve(isReportByMe);
      }
    } catch (error) {
      reject(error);
    }
  });
  return isReport;
}
export async function unReportTargetById(targetType, targetId) {
  const isReport = new Promise(async (resolve, reject) => {
    try {
      const didDeletePostReport = await deleteReport(targetType, targetId);
      if (didDeletePostReport) {
        resolve(didDeletePostReport);
      }
    } catch (error) {
      reject(error);
    }
  });
  return isReport;
}
//# sourceMappingURL=feed.js.map