import { translate } from 'i18n-calypso';
import { registerHandlers } from 'calypso/state/data-layer/handler-registry';
import { http } from 'calypso/state/data-layer/wpcom-http/actions';
import { dispatchRequest } from 'calypso/state/data-layer/wpcom-http/utils';
import { errorNotice } from 'calypso/state/notices/actions';
import { READER_UNFOLLOW_TAG_REQUEST } from 'calypso/state/reader/action-types';
import { receiveUnfollowTag as receiveUnfollowTagAction } from 'calypso/state/reader/tags/items/actions';

export function requestUnfollow( action ) {
	return http( {
		path: `/read/tags/${ action.payload.slug }/mine/delete`,
		method: 'POST',
		apiVersion: '1.1',
		onSuccess: action,
		onFailure: action,
	} );
}

/**
 * Normalize response from the api. The only thing we care about is the removed_tag so only keep that.
 * @param  {Object} apiResponse api response from the unfollow
 * @returns {number} the ID of the tag that was removed
 */
export const fromApi = ( apiResponse ) => {
	if ( apiResponse.subscribed ) {
		throw new Error(
			`failed to unsubscribe to tag with response: ${ JSON.stringify( apiResponse ) }`
		);
	}

	return apiResponse.removed_tag;
};

export function receiveUnfollowTag( action, removedTagId ) {
	return receiveUnfollowTagAction( {
		payload: removedTagId,
	} );
}

export function receiveError( action, error ) {
	const errorText = translate( 'Could not unfollow tag: %(tag)s', {
		args: { tag: action.payload.slug },
	} );

	if ( process.env.NODE_ENV === 'development' ) {
		// eslint-disable-next-line no-console
		console.error( errorText, error );
	}
	return errorNotice( errorText );
}

registerHandlers( 'state/data-layer/wpcom/read/tags/mine/delete/index.js', {
	[ READER_UNFOLLOW_TAG_REQUEST ]: [
		dispatchRequest( {
			fetch: requestUnfollow,
			onSuccess: receiveUnfollowTag,
			onError: receiveError,
			fromApi,
		} ),
	],
} );
