(function($) {
	'use strict';

	var cfg = window.wppsChangePostTypeList || {};

	function initQuickEdit() {
		if (typeof inlineEditPost === 'undefined') {
			return;
		}

		var originalEdit = inlineEditPost.edit;
		inlineEditPost.edit = function(id) {
			originalEdit.apply(this, arguments);

			if (typeof id === 'object') {
				id = this.getId(id);
			}

			var $edit = $('#edit-' + id);
			var postType = cfg.listType || 'post';

			if (!$edit.length) {
				return;
			}

			$('select[name="wpps_post_type"], .wpps-change-post-type__quickedit', $edit).val(String(postType));
		};
	}

	if (window.inlineEditPost) {
		initQuickEdit();
	} else {
		$(initQuickEdit);
	}

	$(document).ajaxSuccess(function(event, xhr, settings) {
		if (!settings.data || String(settings.data).indexOf('action=inline-save') === -1) {
			return;
		}

		var params = new URLSearchParams(String(settings.data));
		var selected = params.get('wpps_post_type');
		var listType = cfg.listType || 'post';

		if (selected && selected !== listType) {
			window.location.reload();
		}
	});
})(jQuery);
