text: 'use strict';
  
  Object.defineProperty(exports, "__esModule", {
  	value: true
  });
  
  var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  
  var _utils = require('./utils');
  
  function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  
  var debug = require('debug')('popura:object');
  
  var Popura = function () {
  	/**
    * @param {string} username
    * @param {string} password
    * @return {object}
    */
  	function Popura(username, password) {
  		_classCallCheck(this, Popura);
  
  		this.setUser(username, password);
  	}
  
  	/**
    * @return {string}
    */
  
  
  	_createClass(Popura, [{
  		key: 'getUser',
  		value: function getUser() {
  			return this._username;
  		}
  
  		/**
     * @param {string} username
     * @param {string} password
     */
  
  	}, {
  		key: 'setUser',
  		value: function setUser(username, password) {
  			debug('Setting \'' + username + '\' as user. Use pass = ' + Boolean(password));
  
  			this._username = username;
  			this._authToken = (0, _utils.generateAuthToken)(username, password);
  		}
  
  		/**
     * @return {Promise}
     */
  
  	}, {
  		key: 'verifyAuth',
  		value: function verifyAuth() {
  			debug('Verifying credentials for \'' + this._username + '\'');
  			return this._get('/account/verify_credentials.xml');
  		}
  
  		/**
     * @param {string} title
     * @return {Promise}
     */
  
  	}, {
  		key: 'searchAnimes',
  		value: function searchAnimes(title) {
  			return this._search('anime', title);
  		}
  
  		/**
     * @param {string} title
     * @return {Promise}
     */
  
  	}, {
  		key: 'searchMangas',
  		value: function searchMangas(title) {
  			return this._search('manga', title);
  		}
  
  		/**
     * @param {string} username
     * @return {Promise}
     */
  
  	}, {
  		key: 'getAnimeList',
  		value: function getAnimeList() {
  			var username = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this._username;
  
  			debug('Getting animelist of ' + username);
  			return this._list('anime', username);
  		}
  
  		/**
     * @param {string} username
     * @return {Promise}
     */
  
  	}, {
  		key: 'getMangaList',
  		value: function getMangaList() {
  			var username = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this._username;
  
  			debug('Getting mangalist of ' + username);
  			return this._list('manga', username);
  		}
  
  		/**
     * @param {int} id
     * @param {object} values
     * @return {Promise}
     */
  
  	}, {
  		key: 'addAnime',
  		value: function addAnime(id) {
  			var values = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  
  			if (!values.status) {
  				values.status = 1;
  			}
  
  			return this._post('/animelist/add/' + id + '.xml', { values: values, expects: (0, _utils.includesText)('Created') });
  		}
  
  		/**
     * @param {int} id
     * @param {object} values
     * @return {Promise}
     */
  
  	}, {
  		key: 'addManga',
  		value: function addManga(id) {
  			var values = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  
  			if (!values.status) {
  				values.status = 1;
  			}
  
  			return this._post('/mangalist/add/' + id + '.xml', { values: values, expects: (0, _utils.includesText)('Created') });
  		}
  
  		/**
     * @param {int} id
     * @param {object} values
     * @return {Promise}
     */
  
  	}, {
  		key: 'updateAnime',
  		value: function updateAnime(id) {
  			var values = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  
  			return this._post('/animelist/update/' + id + '.xml', { values: values, expects: (0, _utils.includesText)('Updated') });
  		}
  
  		/**
     * @param {int} id
     * @param {object} values
     * @return {Promise}
     */
  
  	}, {
  		key: 'updateManga',
  		value: function updateManga(id) {
  			var values = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  
  			return this._post('/mangalist/update/' + id + '.xml', { values: values, expects: (0, _utils.includesText)('Updated') });
  		}
  
  		/**
     * @param {int} id
     * @return {Promise}
     */
  
  	}, {
  		key: 'deleteAnime',
  		value: function deleteAnime(id) {
  			return this._post('/animelist/delete/' + id + '.xml', { expects: (0, _utils.includesText)('Deleted') });
  		}
  
  		/**
     * @param {int} id
     * @return {Promise}
     */
  
  	}, {
  		key: 'deleteManga',
  		value: function deleteManga(id) {
  			return this._post('/mangalist/delete/' + id + '.xml', { expects: (0, _utils.includesText)('Deleted') });
  		}
  
  		/**
     * @param {string} url
     * @param {object} opts
     * @return {Promise}
     */
  
  	}, {
  		key: '_get',
  		value: function _get(url, opts) {
  			debug('Requesting ' + url);
  
  			return (0, _utils.request)(this._authToken, '/api' + url, opts).then(function (_ref) {
  				var body = _ref.body;
  				return (0, _utils.xmlParser)(body);
  			}).then(_utils.cleanApiData);
  		}
  
  		/**
     * @param {string} type
     * @param {string} title
     * @return {Promise}
     */
  
  	}, {
  		key: '_search',
  		value: function _search(type, title) {
  			debug('Searching for ' + type + ' named \'' + title + '\'');
  
  			return this._get('/' + type + '/search.xml', {
  				query: { q: title }
  			})
  			// Be sure to return an array since the XML parser
  			// doesn't recognize single node XML results as array
  			.then(function (result) {
  				return Array.isArray(result) ? result : [result];
  			});
  		}
  
  		/**
     * @param {string} type
     * @param {string} username
     * @return {Promise}
     */
  
  	}, {
  		key: '_list',
  		value: function _list(type, username) {
  			debug('Requesting ' + type + 'list of ' + username);
  
  			return (0, _utils.request)(this._authToken, '/malappinfo.php', {
  				query: {
  					u: username,
  					status: 'all',
  					type: type
  				}
  			}).then(function (_ref2) {
  				var body = _ref2.body;
  				return (0, _utils.xmlParser)(body);
  			}).then(function (parsed) {
  				if (parsed.error) {
  					throw new Error(parsed.error);
  				}
  				return parsed;
  			}).then(_utils.cleanListData);
  		}
  
  		/**
     * @param {string} url
     * @param {object} opts
     * @return {Promise}
     */
  
  	}, {
  		key: '_post',
  		value: function _post(url, _ref3) {
  			var _ref3$values = _ref3.values,
  			    values = _ref3$values === undefined ? false : _ref3$values,
  			    _ref3$expects = _ref3.expects,
  			    expects = _ref3$expects === undefined ? false : _ref3$expects;
  
  			debug('Posting in MAL\'s API at ' + url);
  
  			return (0, _utils.request)(this._authToken, '/api' + url, {
  				method: 'POST',
  				body: values ? { data: (0, _utils.xmlBuilder)(values) } : false
  			}).then(function (_ref4) {
  				var _ref4$body = _ref4.body,
  				    body = _ref4$body === undefined ? '' : _ref4$body;
  
  				if (expects && !expects(body)) {
  					debug('Body did not match test function', body);
  					throw new Error('Unespected return from MAL server posting at ' + url);
  				}
  				return body;
  			});
  		}
  	}]);
  
  	return Popura;
  }();
  
  exports.default = Popura;
  module.exports = exports['default'];