import { __rest } from "tslib";
import { discussionsApiRequestV2 } from "../discussions-api-request";
import { SearchPostsFormat } from "../enums/searchPostsFormat";
// When the `PostRelation.CHANNEL` relation is provided, the API will include
// a `channel: IChannel` property on the post, but it removes the `channelId: string`
// property, which is less than ideal. this simply a stop-gap measure to make sure the
// `channelId: string` is not lost on posts until the API is updated to not strip that
// property when it sets `channel: IChannel` on the result.
const transformPostResult = (post) => {
    return post.channel ? Object.assign(Object.assign({}, post), { channelId: post.channel.id }) : post;
};
// Transforms a page of post search results such that the `channelId: string` is not
// lost when `PostRelation.CHANNEL` is provided.
const transformPostSearchResults = (results) => {
    return Object.assign(Object.assign({}, results), { items: results.items.map((post) => transformPostResult(post)) });
};
/**
 * search posts V2
 *
 * @export
 * @param {ISearchPostsParams} options
 * @return {*}  {Promise<IPagedResponse<IPost>>}
 */
export function searchPostsV2(options) {
    const url = `/posts/search`;
    return discussionsApiRequestV2(url, Object.assign(Object.assign({}, options), { data: Object.assign({}, options.data), httpMethod: "POST" })).then(transformPostSearchResults);
}
/**
 * searches for posts and resolves a promise with CSV string representing the results V2
 *
 * @export
 * @param {IExportPostsParams} options
 * @return {*}  {Promise<string>}
 */
export function exportPostsV2(options) {
    const url = `/posts/search`;
    return discussionsApiRequestV2(url, Object.assign(Object.assign({}, options), { data: Object.assign(Object.assign({}, options.data), { f: SearchPostsFormat.CSV }), httpMethod: "POST" }));
}
/**
 * create post
 *
 * @export
 * @param {ICreatePostParamsV2} options
 * @return {*}  {Promise<IPost>}
 */
export function createPostV2(options) {
    const url = `/posts`;
    return discussionsApiRequestV2(url, Object.assign({ httpMethod: "POST" }, getCreateUpdateRequestParamsV2(options)));
}
/**
 * create reply to post
 *
 * @export
 * @param {string} parentId
 * @param {ICreateReplyParams} options
 * @return {*}  {Promise<IPost>}
 */
export function createReplyV2(options) {
    const url = `/posts/${options.postId}/reply`;
    return discussionsApiRequestV2(url, Object.assign({ httpMethod: "POST" }, getCreateUpdateRequestParamsV2(options)));
}
/**
 * fetch post
 *
 * @export
 * @param {IFetchPostParams} params
 * @return {*}  {Promise<IPost>}
 */
export function fetchPostV2(params) {
    const url = `/posts/${params.postId}`;
    params.httpMethod = "GET";
    return discussionsApiRequestV2(url, params).then(transformPostResult);
}
/**
 * remove post
 *
 * @export
 * @param {IRemovePostParams} options
 * @return {*}  {Promise<IRemovePostResponse>}
 */
export function removePostV2(options) {
    const url = `/posts/${options.postId}`;
    options.httpMethod = "DELETE";
    return discussionsApiRequestV2(url, options);
}
/**
 * update post
 * NOTE: this method currently only update post.title and post.body
 *
 * @export
 * @param {IUpdatePostParams} options
 * @return {*}  {Promise<IPost>}
 */
export function updatePostV2(options) {
    const url = `/posts/${options.postId}`;
    return discussionsApiRequestV2(url, Object.assign({ httpMethod: "PATCH" }, getCreateUpdateRequestParamsV2(options)));
}
/**
 * update post status
 * NOTE: this method will only update a post's status
 *
 * @export
 * @param {IUpdatePostStatusParams} options
 * @return {*}  {Promise<IPost>}
 */
export function updatePostStatusV2(options) {
    const url = `/posts/${options.postId}/status`;
    options.httpMethod = "PATCH";
    return discussionsApiRequestV2(url, options);
}
/**
 * Builds the necessary request options for post/reply create/update requests
 * @param mentionUrl
 */
function getCreateUpdateRequestParamsV2(params) {
    const { mentionUrl } = params, requestOptions = __rest(params, ["mentionUrl"]);
    if (mentionUrl) {
        requestOptions.headers = Object.assign(Object.assign({}, requestOptions.headers), { "mention-url": mentionUrl });
    }
    return requestOptions;
}
//# sourceMappingURL=posts.js.map