import wpcom from 'calypso/lib/wp';
import { POST_SAVE_FAILURE, POST_SAVE } from 'calypso/state/action-types';
import { receivePost } from 'calypso/state/posts/actions/receive-post';
import { savePostSuccess } from 'calypso/state/posts/actions/save-post-success';

import 'calypso/state/posts/init';

/**
 * Returns an action thunk which, when dispatched, triggers a network request
 * to trash the specified post.
 * @param  {number}   siteId Site ID
 * @param  {number}   postId Post ID
 * @param  {boolean}  silent Whether to stop related notices from appearing
 * @returns {Function}        Action thunk
 */
export function trashPost( siteId, postId, silent = false ) {
	return ( dispatch ) => {
		// Trashing post is almost equivalent to saving the post with status field set to `trash`
		// and the action behaves like it was doing exactly that -- it dispatches the `POST_SAVE_*`
		// actions with the right properties.
		//
		// But what we really do is to call the `wpcom.site().post().delete()` method, i.e., sending
		// a `POST /sites/:site/posts/:post/delete` request. The difference is that an explicit delete
		// will set a `_wp_trash_meta_status` meta property on the post and a later `restore` call
		// can restore the original post status, i.e., `publish`. Without this, the post will be always
		// recovered as `draft`.
		const post = { status: 'trash' };

		dispatch( {
			type: POST_SAVE,
			siteId,
			postId,
			post,
		} );

		const trashResult = wpcom.site( siteId ).post( postId ).delete();

		trashResult.then(
			( savedPost ) => {
				dispatch( savePostSuccess( siteId, postId, savedPost, post, silent ) );
				dispatch( receivePost( savedPost ) );
			},
			( error ) => {
				dispatch( {
					type: POST_SAVE_FAILURE,
					siteId,
					postId,
					error,
				} );
			}
		);

		return trashResult;
	};
}
