/**
 * JSON could not be parsed.
 */
export class JsonParseError extends Error {
	/**
	 * @param {Error} failReason
	 * @param {string} unparseableContent
	 */
	constructor(failReason, unparseableContent) {
		super();
		this.message = 'Failed to parse JSON. Reason: ' + failReason.message;
		this._error = failReason;
		this._content = unparseableContent;
	}

	/**
	 * Data which failed to JSON parse.
	 * @return {string}
	 */
	get content() {
		return this._content;
	}

	/**
	 * The reason why this failed to parse.
	 * @return {*}
	 */
	get error() {
		return this._error;
	}
}

export class ResponseError extends Error {

	/**
	 * @param {Response} response
	 * @param {string} content
	 * @param {(string|null)} content
	 */
	constructor(response, content, message = null) {
		super();
		this._response = response;
		this._content = content;
		this.message = message || 'Unaccepted status: ' + this._response.status;
	}

	/**
	 * Response content.
	 * @return {string}
	 */
	get content() {
		return this._content;
	}

	/**
	 * @return {number}
	 */
	get status() {
		return this._response.status;
	}
}

export const UNKNOWN = 'UNKNOWN';
export const UNEXPECTED_CONTENT = 'UNEXPECTED_CONTENT';

export class ApplicationError extends ResponseError {
	/**
	 * @param {Response} response
	 * @param {string} content
	 * @param {string} errorType Should match an error code above.
	 */
	constructor(response, content, version, errorType) {
		super(response, content);
		this._errorType = errorType;
		this._appVersion = version;
		this.message += '. Error type: ' + errorType;
	}

	get errType() {
		return this._errorType;
	}

	get appVersion() {
		return this._appVersion;
	}
}

/** Reasons for failure when marking in a Room Session: **/
export const ROOM_SESSION_ATTENDANCE_FULL = 'full';
export const ROOM_SESSION_ATTENDANCE_UNAVAILABLE = 'unavailable';